简体   繁体   English

小黄瓜-在许多情况下(代码接收)包括通用步骤

[英]Gherkin - Include common steps in many scenarios (Codeception)

I'm using gherkin driven by codeception for BDD in my project. 我在我的项目中使用了由代码接收驱动的小黄瓜BDD。 I would like to test if user given with a role is able to see menu entry points which are suitable for him. 我想测试被赋予角色的用户是否能够看到适合他的菜单入口点。

At this moment I use this scenario: 目前,我使用这种情况:

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

I would like to reuse 3 steps many times: 我想多次重用3个步骤:

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

I do not want to copy&paste it every time I create new scenario. 我不想每次创建新方案时都将其复制并粘贴。 Instead I want to write it as... lets say include file (also in gherkin language), and use it in my scenarios: 相反,我想将其写为...让说包括文件(也使用小黄瓜语言),并在我的方案中使用它:

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

Is it possible? 可能吗? How I can do that? 我该怎么做?

Why couldn't you write a new step? 你为什么不能写一个新的步骤?

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

And then use this step in your feature file: 然后在功能文件中使用此步骤:

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

In addition to Kyle's answer I'd rather prefer to rewrite scenarios like in your example using table as multiline argument in the following way: 除了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

It is less imperative and more human-friendly ("I should see" in every line is too boring) so it would be easier to discuss with customer. 它不太必要,也更人性化(每行中的“我应该看到”都太无聊了),因此与客户讨论会更容易。

Note that here table used as multiline argument, not as Example. 请注意,此处的表用作多行参数,而不用作示例。 See about table as multiline argument for steps here: 有关步骤,请参阅关于表作为多行参数:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM