Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 433
JonB Avatar asked Aug 03 '11 10:08

JonB


People also ask

What are feature files in Cucumber?

In Cucumber, feature files store high-level description of scenarios and steps in the Gherkin language. Gherkin uses plain English by default and promotes behavior-driven development. Feature files are usually located in the features folder under Test Resources Root. .

How do cucumbers handle multiple step definitions?

You can have all of your step definitions in one file, or in multiple files. When you start with your project, all your step definitions will probably be in one file. As your project grows, you should split your step definitions into meaningful groups in different files.

What does the steps in feature file?

Step definition maps the Test Case Steps in the feature files(introduced by Given/When/Then) to code. It which executes the steps on Application Under Test and checks the outcomes against expected results. For a step definition to be executed, it must match the given component in a feature.


1 Answers

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
like image 117
iafonov Avatar answered Oct 08 '22 11:10

iafonov