【问题标题】:Gherkin - Include common steps in many scenarios (Codeception)Gherkin - 包括许多场景中的常用步骤(Codeception)
【发布时间】:2017-03-14 09:28:37
【问题描述】:

我在我的项目中使用 BDD 的 codeception 驱动的小黄瓜。我想测试赋予角色的用户是否能够看到适合他的菜单入口点。

此时我使用这个场景:

Scenario: basic menu check
   User "foo" has role "basic_user"
   Given I am logged in as "foo" with password "Test123!"
   Then I should see "Project" menu point
   And I should see "Settings" menu point
   And I should see "Notifications" menu point
   And I should see "Messages" menu point
   And I should see "Logout" menu point
   Then I logout

我想多次重复使用 3 个步骤:

   And I should see "Settings" menu point
   And I should see "Notifications" menu point
   And I should see "Messages" menu point

我不想每次创建新场景时都复制粘贴它。相反,我想把它写成......让我们说包含文件(也用小黄瓜语言),并在我的场景中使用它:

Scenario: basic menu check
   ...
   Include "common_menu_check"
   ...

有可能吗?我该怎么做?

【问题讨论】:

    标签: php bdd codeception gherkin


    【解决方案1】:

    除了 Kyle 的回答之外,我更愿意通过以下方式使用 table 作为多行参数来重写您的示例中的场景:

    Scenario: basic menu check
       User "foo" has role "basic_user"
       Given I am logged in as "foo" with password "Test123!"
       Then I should see menu points:
       | Settings      |  
       | Notifications |  
       | Messages      |  
       | Logout        |  
       Then I logout
    

    它不那么强制性,更人性化(每行“我应该看到”太无聊了),因此与客户讨论会更容易。

    请注意,此处的表格用作多行参数,而不是示例。请在此处查看将表作为多行参数的步骤:

    【讨论】:

    【解决方案2】:

    为什么你不能写一个新的步骤?

    /**
     *
     * @Then /^I should see the common menu points$/
     */
    public function iShouldSeeTheCommonMenuPoints()
    {
       // CODE HERE FOR SEEING MENU ITEMS
    }
    

    然后在你的功能文件中使用这一步:

    Scenario: basic menu check
        User "foo" has role "basic_user"
    Given I am logged in as "foo" with password "Test123!"
    Then I should see the common menu points
    And I should see "Project" menu point
    And I should see "Logout" menu point
    Then I logout
    

    【讨论】:

    • 因为我想用小黄瓜语法管理iShouldSeeTheCommonMenuPoints 功能
    • 这不是应该做的事。该功能已从大多数基于黄瓜的框架中删除,因为它被认为是不好的做法。也就是说,理论上你应该可以在你定义iShouldSeeTheCommonMenuPoints()的文件中包含iShouldSeeMenuPoint()的功能,并在新的步骤中使用它。这将保留您的功能,而不会是不好的做法,并且意味着您可以使用与小黄瓜相同的功能来管理 iShouldSeeTheCommonMenuPoints
    猜你喜欢
    • 2013-01-22
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多