Search code examples
javajunitcucumbercucumber-junitcucumber-java

How to structure sequential test with cucumber tags and JUnit?


I have a dream I would like to achive with Cucumber.

I would like to combine cucumber with selenium (<= so far so good) AND optimize the test execution with tags. In my dream I'm able to tag my szenarios with Strings which represent a database szenario. For example:

@noData
Scenario: bake a bread

@oneBread
Scenario: look at bread

@tenBreads
Scenario: give multiple breads to the poor

Some of my tests alter the data, some don't. So I thought, how about clustering all "non-altering" tests to access the given data in parallel

@viewOnly
Scenario: look at a bread

@viewOnly
Scenario: smell a bread

With this kind of clustering I aim to achive the slightest setup and working in parallel if possible.

@noData
@viewOnly
Scenario: bake a bread
          when i bake a bread
          then i see a bread

@oneBread
@viewOnly
Scenario: look at a bread
          then i see a bread

@oneBread
@viewOnly
Scenario: smell a bread
          then i see a bread

@oneBread
Scenario: slice a bread in half
          when i slice my bread
          then i got two parts of the bread

@oneBread
Scenario: butter a bread
          when i butter my bread
          then i got one bread covered in butter

With this Feature file, I'd expect that this solution would process the features in the following order:

  1. Setup Szenario in database => @noData
  2. @noData => Scenario: bake a bread

  3. Setup Szenario in database => @oneBread
  4. @oneBread => @viewOnly Scenario look at a bread

  5. @oneBread => @viewOnly Scenario smell a bread

  6. Setup Szenario in database => @oneBread
  7. @oneBread => Scenario slice a bread in half

  8. Setup Szenario in database => @oneBread
  9. @oneBread => Scenario butter a bread

Once I played with JUnit runners and tried to process the created JUnit Tests on my own. Do I have to write my own JUnit runner again? Or is it possible to fullfil my requirements with Cucumber features ?

Can someone help me with that?


Solution

  • You could do that by running separate test for each database scenario. For that use the --tags parameter as explained here https://github.com/cucumber/cucumber/wiki/Tags#running-a-subset-of-scenarios

    Another way, which I prefer, is to put similar database scenarios into feature files. For example a feature file for those how altering data. Use @Before to get the scenario's tags.

    @Before
    public void beforeTest(Scenario scenario){
    
    }
    

    hope that helps!