黄瓜特征和步骤定义

发布于 2024-11-28 04:28:48 字数 1645 浏览 0 评论 0原文

我是黄瓜测试的新手。

我创建了两个功能文件:

events.feature
partner.feature

并将我的步骤定义放在 step_definitions 文件夹中:

./step_definitions/
     events.rb
     partner.rb

Cucumber 似乎在所有 .rb 文件中查找步骤信息。

是否有限制该功能查看特定步骤定义文件?

我之所以要这样做,是因为即使我使用 --guess 标志,我也会遇到不明确的匹配错误。

我之所以要这样做,是出于以下原因。我正在测试 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

只需关注两种情况下的“然后我应该看到内容”,就会发生错误,因为在 .rb 文件中我需要包含:

partners.rb

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

events.rb

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

这意味着存在不明确的匹配“我应该看到内容”。

我知道有不同的构建方式,即我可以创建内容功能,并使用场景大纲:

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   | 

但我不想这样做,因为我想将每个内容类型封装在自己的测试功能中。

所以本质上我需要弄清楚如何根据我正在测试的功能运行特定的步骤文件。

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/
     events.rb
     partner.rb

It seems that Cucumber looks in all the .rb files for the step information.

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.

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.

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:

partners.rb

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

events.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, i.e. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

四叶草在未来唯美盛开 2024-12-05 04:28:48

Cucumber 总是加载所有文件,我认为没有办法覆盖这种行为。关于您的步骤不明确的问题 - 解决方案很简单 - 在您的步骤中添加参数 在

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

场景中只需这样称呼它

然后我应该看到“合作伙伴内容”

  • 免费奖金 - 您的场景现在更具可读性

Cucumber always loads all files and I don't think that there is a way to override this behavior. 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
毁梦 2024-12-05 04:28:48

我不认为您建议的替代方法有什么问题。将步骤定义分成逻辑域是有意义的。但是,您似乎可能试图做得太过分,这将导致大量重复的代码以及像您现在所看到的不明确匹配的问题。我建议这样做:

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"

同样,在您的事件功能中:

...
Then I should see "event content"

然后您可以在单独的 step_definitions/common_steps.rb 文件中执行以下操作:

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

此步骤没有任何合作伙伴/事件具体说一下。相反,场景包含您的功能的特定于数据的字符串。

如果您正在开发 Rails 应用程序,则 cucumber-rails gem 实际上会创建一堆常见的为您提供 Web 应用程序测试的步骤。即使您不使用 Rails,查看其中的一些内容也可能会很有用 步骤

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:

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. Even if you aren't using Rails, it might be useful to take a look at some of these steps.

等待我真够勒 2024-12-05 04:28:48

我一直在寻找这个,但似乎不可能“开箱即用”。

我的解决方案是始终使用某种附加描述(例如类名)来区分步骤:

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

这些“构建代码”描述是您所需要的,无需在不同文件/域之间重用步骤。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文