Php 小黄瓜-包括许多场景中的常见步骤(Codeception)

Php 小黄瓜-包括许多场景中的常见步骤(Codeception),php,bdd,codeception,gherkin,Php,Bdd,Codeception,Gherkin,我在我的项目中使用了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

我在我的项目中使用了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"
   ...

可能吗?我怎么能做到呢?

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

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

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

/**
 *
 * @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
它不那么必要,更人性化(“我应该明白”每一行都太无聊了),所以更容易与客户讨论

请注意,这里的表用作多行参数,而不是示例。有关步骤,请参见此处的“关于表作为多行参数”:

  • 共同概念:
  • 黄瓜:
  • 行为:

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

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
它不那么必要,更人性化(“我应该明白”每一行都太无聊了),所以更容易与客户讨论

请注意,这里的表用作多行参数,而不是示例。有关步骤,请参见此处的“关于表作为多行参数”:

  • 共同概念:
  • 黄瓜:
  • 行为:

因为我想管理
我应该看到,使用小黄瓜语法的CommonMenuPoints
功能是不应该做的事情。该功能已从大多数基于cucumber的框架中删除,因为它被认为是不好的做法。也就是说,理论上,您应该能够在您定义的文件中包含
iShouldSeeMenuPoint()
函数,并在新步骤中使用它。这将保留您的功能,而不是不好的做法,这意味着你将能够管理
我应该从小黄瓜可能来自的相同功能中查看CommonMenuPoints
。因为我想管理
我应该使用小黄瓜语法查看CommonMenuPoints
功能,这不是应该做的事情。该功能已从大多数基于cucumber的框架中删除,因为它被认为是不好的做法。也就是说,理论上,您应该能够在您定义的文件中包含
iShouldSeeMenuPoint()
函数,并在新步骤中使用它。这将保留您的功能,而不是不好的做法,并意味着您将能够管理
I从小黄瓜将来自的相同功能中查看CommonMenuPoints
然后我将看到菜单点:
。这是否遵循了
示例
的概念?在这种情况下,它不是小黄瓜示例,而是步骤的表参数。参见(黄瓜和贝哈特)这是一个很好的补充。将它们结合使用还意味着更高的人类可读性,而且复制粘贴的需要也会减少<代码>然后我应该看到常见的菜单点,我应该看到菜单点:|项目| |注销|
会很好看(当然,当在功能文件中正确格式化时)
然后我应该看到菜单点:
。这是否遵循了
示例
的概念?在这种情况下,它不是小黄瓜示例,而是步骤的表参数。参见(黄瓜和贝哈特)这是一个很好的补充。将它们结合使用还意味着更高的人类可读性,而且复制粘贴的需要也会减少<代码>然后我应该看到常见的菜单点,我应该看到菜单点:|项目| |注销|
会很好看(当然,在功能文件中正确格式化时)