Reputation: 55
I have several features like the below to test the outcome of some data processing.
Feature: A
Scenario: A1
Given I load data A2
Then output is for A1 is output_A1
Scenario A2
Given I load data A2
Then output is for A2 is output_A2
I would like to do all data loading first and then check the output later like below because it is much faster.
Given I load data A2
Given I load data A2
Then output is for A1 is output_A1
Then output is for A2 is output_A2
Is there any way to do this behind the scenes and present the reports as in the first case?
I was thinking of some way to tell cucumber to run all Given scenarios first and all Then scenarios later.
Upvotes: 2
Views: 1688
Reputation: 4099
Cucumber is the wrong tool for this kind of thing. Instead write a unit test for the data processing.
Because unit tests are implemented in a programming language its easy to preload all the data, run things in a particular order etc.
If you must use Cucumber to do this the thing to do is to push all the programming down into the step definitions. So you end up with a scenario which is something like
Scenario: Run the A tests
When I run the A tests
Then I should get no errors
Now you can do your data loading and looping in the When step, and interrogate the saved results in the Then step.
Upvotes: 0
Reputation: 2276
You should use a Background to set up the context for your test. And you are missing the When steps in your example, by the way. In the When is where your action happens.
Feature: A
Backgroud:
Given I load data A
Scenario: A1
When whatever A1
Then output is for A1 is output_A1
Scenario: A2
When whatever A2
Then output is for A2 is output_A2
Although the behaviour you are describing can be done in a @Before (Cucumber annotation) hook if you want to load the data before each scenario, or in your Runner class, in a @BeforeClass (JUnit annotation) to load it before all the tests.
But be careful with this last option, as it is easy to misuse it. For loading your fixtures, you should use the regular given, the background or the before hook, not the @BeforeClass annotation, in order to keep your scenarios clean and independent.
@RunWith(Cucumber.class)
@CucumberOptions(format = "pretty")
public class RunCukesTest {
@BeforeClass
public void setUp(){
// Load data A2
}
}
Hope this helps.
Upvotes: 0
Reputation: 156
Cucumber doesn't actually distinguish between the Given and Then keywords so you can't tell cucumber to run the all the Givens first.
You could set up a Scenario to run before all the others:
Scenario: Load data
Given the Data Exists
Then I load all the Data
In which you load all of the data for the following scenarios
Scenario: A1
Given the A1 data is loaded
Then the output for A1 is output_A1
where the given step just checks that the data was loading
Upvotes: 2