Gherkin - 包括许多场景中的通用步骤 (Codeception)

Gherkin - Include common steps in many scenarios (Codeception)

我在我的项目中使用由用于 BDD 的代码感知驱动的小黄瓜。我想测试给定角色的用户是否能够看到适合他的菜单入口点。

此时我使用的场景是:

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"
   ...

可能吗?我该怎么做?

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

/**
 *
 * @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

除了 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

减少命令式,更人性化("I should see"每一行都太无聊了)这样更容易与客户讨论。

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