Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you define instance variables during Cucumber's Given, When, and Then step definitions

Tags:

cucumber

bdd

I know with Cucumber, you can define instance variables during a Given step definition. This instance variable becomes part of the World scope. Then you can access this instance variable during step definitions of When and Then.

Can you define instance variables also during When and Then step definitions and access them in the later When and Then step definitions?

If it's possible, is it even a common practice to define instance variables during When and Then step definitions?

Thank you.

like image 916
Zack Xu Avatar asked Dec 12 '13 14:12

Zack Xu


People also ask

Can a step definition can transfer state to a subsequent step definition of the same scenario?

A step definition can transfer state to a subsequent step definition by storing state in instance variables. Please note that if you use arrow functions, you won't be able to share state between steps!

What is used to check if all the steps have the step definition before execute?

Dry Run is nothing but checking the complete implementation of all the mentioned steps present in the Feature file. Before the execution starts . Dry Run is Checking the implementionation not about the execution of scripts.

What is the purpose of step definition file in Cucumber?

Steps definition file stores the mapping between each step of the scenario defined in the feature file with a code of function to be executed. So, now when Cucumber executes a step of the scenario mentioned in the feature file, it scans the step definition file and figures out which function is to be called.


1 Answers

Yes, you can set instance variables during any step type.

For example, given the the feature:

Feature: Instance variables

Scenario: Set instance variables during all steps
    Given a given step sets the instance variable to "1"
    Then the instance variable should be "1"
    When a when step sets the instance variable to "2"
    Then the instance variable should be "2"
    Then a then step sets the instance variable to "3"
    Then the instance variable should be "3"

And the step definitions:

Given /a given step sets the instance variable to "(.*)"/ do |value|
    @your_variable = value
end
When /a when step sets the instance variable to "(.*)"/ do |value|
    @your_variable = value
end
Then /a then step sets the instance variable to "(.*)"/ do |value|
    @your_variable = value
end
Then /the instance variable should be "(.*)"/ do |value|
    @your_variable.should == value
end

You will see that the scenario passes, which means that the when and then steps were successfully setting the instance variable.

In fact, the Given, When and Then are just aliases of each other. Just because you defined a step definition as a "Given", it can still be called as a "When" or "Then". For example, the above scenario will still pass if the step definitions used were:

Then /a (\w+) step sets the instance variable to "(.*)"/ do |type, value|
    @your_variable = value
end
Then /the instance variable should be "(.*)"/ do |value|
    @your_variable.should == value
end

Notice that the first "Then" step definition can be used by the "Given" and "When" in the scenario.

As to whether it is good practice to set instance variables in when and then steps, it is no worse than doing it in given steps. Ideally, none of your steps would use instance variables as they create step coupling. But, practically speaking, I have not run into significant issues by using the instance variables.

like image 70
Justin Ko Avatar answered Sep 28 '22 12:09

Justin Ko