Our tests store large amounts of information about each executed test sorted in some directories. It would be practical if all results from the same run are stored together in for example a directory named by date/id. That way it's easy to look through historic records and keep track of what is what. Currently this is a manual job, where the output directory has to be renamed after every run to keep records.
We could easily script this outside of cucumber to achieve automation, however I'm wondering if this can be done through the cucumber framework. One simple way (if possible) is to create a runId variable and pass it between all tests in the same run. However from what I understand it's not possible to transfer objects/variables between scenarios and features. Dependency-injection only seems to allow the same objects between steps. Is there perhaps another place in the cucumber framework we can initiate a runId variable and pass it down to all tests? Or perhaps there is already something like this in cucumber that I can get from somewhere in the framework?
For example if I have step definitions:
public class StepSy {
File runDirectory;
DataObject data;
@Before
public void before() {
runDirectory = new File(someObjectPassedDown.getRunId());
}
@Given("^Condition (.*)$")
public void condition() {
/*Some conditions*/
}
@When("^I do (.*)$")
public void perform(String toDo) {
/*Some action*/
}
@Then("^I expect (.*)$")
public void expect(String expectedValue) {
/*Some assertion*/
}
@After
public void after(Scenario scenario) {
data.writeTo(runDirectory, scenario.getName());
}
}
And a feature:
Feature: Tests
Scenario: Test01
Given Condition A
When I do B
Then I expect C
Scenario: Test02
Given Condition D
When I do E
Then I expect F
I could run this feature twice and I would get two directories:
MyTestRuns
├── Run_17.05.2017_130156
│ ├── Test01
│ └── Test02
├── Run_16.05.2017_163402
│ ├── Test01
│ └── Test02
If I got your question correctly, you want an unique directory for each run. Setup a static flag in the class which contains the state of the directory initialization. It will make sure the directory is created when the first scenario is run and not anymore. And create a static method for generating the run id...
public class StepSy {
static File runDirectory;
private static boolean fileInitFlag = false;
DataObject data;
@Before
public void before() {
if(!fileInitFlag) {
runDirectory = new File(FileIdGenerator.getRunId());
fileInitFlag= true;
}
}
@After
public void after(Scenario scenario) {
data.writeTo(runDirectory, scenario.getName());
}
}