Search code examples
annotationstagshookcucumber-jvm

Cucumber jvm - specify multiple tags to ignore in After hook


I'm writing some tests using the cucumber jvm that require a few different tear down behaviours for some features, with the majority of scenarios requiring a common tear down.

So far I have been able to successfully tell cucumber to run a common tear down (using the '@After' annotation) for most of my scenarios. Plus I have specified a tag to ignore a small subset of scenarios so I can use a specific tear down in those cases. Here is a condensed example just showing annotations:

@After("~@specificTearDown")
public void tearDown(){
  // some tear down code
}

@After("@specificTearDown")
public void specialTearDown(){
  // some code specific to a scenario
}

I am using the '~' symbol to specify tagged scenarios/features to be ignored.

This works well and I get the first tear down method called in most scenarios, with the second one running only for the tagged scenarios.

My issue is that I now have a need to specify a second specific tear down method for another scenario, but cucumber doesn't seem to like having more than 1 tag to ignore.

I've tried the following with no success...

@After("~@specificTearDown, ~@anotherSpecificTearDown")
public void tearDown(){
  // some tear down code
}

@After("@specificTearDown")
public void specialTearDown(){
  // some code specific to a scenario
}

@After("@anotherSpecificTearDown")
public void anotherSpecificTearDown(){
  // some code specific to a scenario
}

This causes cucumber to just run the first tear down method for all scenarios, which causes my tests to conflict because the other tear down methods also run for the tagged scenarios.

Any ideas how I might do this, I had thought about just having a single tag I use to specify the first tear down and tag all features with it that I don't want to tear down with that code, but that seems wrong to have to do that to me.


Solution

  • I think this should work:

    @After({"~@specificTearDown", "~@anotherSpecificTearDown"})
    public void tearDown(){
      // some tear down code
    }
    

    Could you try it? Hope it helps