简体   繁体   English

黄瓜特征和步骤定义

[英]Cucumber Features and Step Definitions

I am new to Cucumber testing.我是黄瓜测试的新手。

I have created two features files:我创建了两个功能文件:

events.feature
partner.feature

and have my step definitions in a step_definitions folder:并将我的步骤定义放在 step_definitions 文件夹中:

./step_definitions/
     events.rb
     partner.rb

It seems that Cucumber looks in all the .rb files for the step information.似乎 Cucumber 在所有 .rb 文件中查找步骤信息。

Is there anyway of restricting the feature to look at a specific step definition file?有没有限制该功能查看特定步骤定义文件?

The reason as to why I want to do this, is because I am getting Ambiguous match errors, even when I use the --guess flag.我为什么要这样做的原因是,即使我使用 --guess 标志,我也会遇到不明确的匹配错误。

The reason as to why I want to do this is for the following reasons.我之所以要这样做是出于以下原因。 I am testing a CMS, and want to test each of the different content types (events & partners) in separate features.我正在测试一个 CMS,并希望在单独的功能中测试每个不同的内容类型(事件和合作伙伴)。

events.feature事件.特征

Feature: Add partner
  As an administrator I can add a new partner

  Scenario: Create partner
    Given I am logged in
    When I create a partner
    Then I should see content

partner.feature合作伙伴功能

Feature: Add event
  As an administrator I can add a new event

  Scenario: Create event
    Given I am logged in
    When I create an event
    Then I should see content

Just focusing on the 'then I should see content' which is in both scenarios, the error occurs because in the .rb files I need to include:只关注两种情况下的“然后我应该看到内容”,就会发生错误,因为在 .rb 文件中我需要包括:

partners.rb合作伙伴.rb

Then /^I should see content$/ do
  BROWSER.html.should include('SOME PARTNER CONTENT')
end

events.rb事件.rb

Then /^I should see content$/ do
  BROWSER.html.should include('SOME EVENT CONTENT')
end

which means there is an Ambiguous match of "I should see content".这意味着存在“我应该看到内容”的歧义匹配。

I understand there are different ways of structuring this, ie I could create a content feature, and use scenario outlines:我知道有不同的方法来构建它,即我可以创建一个内容功能,并使用场景大纲:

Feature: Add content
  As an administrator I can add a new content

  Scenario Outline: Create content
    Given I am logged in
    When I create an <content type>
    Then I should see <example content>

    Examples: 
    |event   |March Event | 
    |partner |Joe Blogs   | 

But I don't want to do this because I want to encapsulate each content type in their own test feature.但我不想这样做,因为我想将每个内容类型封装在它们自己的测试功能中。

So essentially I need to work out how to run specific step files according to the feature I am testing.所以基本上我需要根据我正在测试的功能计算出如何运行特定的步骤文件。

Cucumber always loads all files and I don't think that there is a way to override this behavior. Cucumber 总是加载所有文件,我认为没有办法覆盖这种行为。 Regarding your problem with ambiguous steps - the solution is easy - add parameters to your steps关于您的步骤不明确的问题 - 解决方案很简单 - 在您的步骤中添加参数

Then /^(?:|I )should see "([^"]*)"$/ do |text|
  page.should have_content(text)
end

And in scenarios just call it like this在场景中就这样称呼它

Then I should see "PARTNER CONTENT"然后我应该看到“合作伙伴内容”

  • free bonus - your scenario is now much more readable免费奖金 - 您的场景现在更具可读性

I don't see anything wrong with the alternative approach that you suggested.我认为您建议的替代方法没有任何问题。 Separating out the step definitions into logical domains makes sense.将步骤定义分成逻辑域是有意义的。 However, it seems like you may be trying to take it too far, and that's going to lead to a good deal of duplicated code and issues with ambiguous matches like you're seeing now.然而,看起来您可能试图走得太远,这将导致大量重复的代码和不明确的匹配问题,就像您现在看到的那样。 I'd recommend doing something like this:我建议做这样的事情:

Feature: Add partner
  As an administrator I can add a new partner

  Scenario: Create partner
    Given I am logged in
    When I create a partner
    Then I should see "partner content"

And, similarly, in your event feature:而且,类似地,在您的活动功能中:

...
Then I should see "event content"

Then you could the following in a separate step_definitions/common_steps.rb file:然后您可以在单独的step_definitions/common_steps.rb文件中执行以下操作:

Then /I should see "(.*)"$/ do |content|
   BROWSER.html.should include(content)
end

This step doesn't have anything partner/event specific about it.此步骤没有任何特定于它的合作伙伴/事件。 Instead, the scenarios contain the data-specific strings for your features.相反,场景包含您的功能的特定于数据的字符串。

If you are working on a Rails app, the cucumber-rails gem will actually create a bunch of common steps for web application testing for you.如果您正在开发 Rails 应用程序, cucumber-rails gem 实际上会为您创建一系列用于 Web 应用程序测试的常见步骤。 Even if you aren't using Rails, it might be useful to take a look at some of these steps .即使您没有使用 Rails,查看其中的一些步骤也可能会有所帮助。

I've been looking for this, but it appears not to be possible "out of the box".我一直在寻找这个,但似乎不可能“开箱即用”。

My solution is to differentiate steps always using some kind of additional description, such as class name, for example:我的解决方案是始终使用某种附加描述(例如类名)来区分步骤,例如:

Scenario: Buildings List
    Given I have a Building with code "B1"
    And I have a Building with code "B2"
    When I go to the list of buildings
    Then I should see "B1" building code
    And I should see "B2" building code

These "building code" descriptions are all you need not to reuse steps between different files / domains.这些“构建代码”描述是您不需要在不同文件/域之间重复使用步骤的全部内容。

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

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