Run after all cucumber tests
13,851
Solution 1
You could use the standard JUnit annotations.
In your runner class write something similar to this:
@RunWith(Cucumber.class)
@Cucumber.Options(format = {"html:target/cucumber-html-report", "json-pretty:target/cucumber-json-report.json"})
public class RunCukesTest {
@BeforeClass
public static void setup() {
System.out.println("Ran the before");
}
@AfterClass
public static void teardown() {
System.out.println("Ran the after");
}
}
Solution 2
What you could do is to register event handler for TestRunFinished
event. For that you can create a custom plugin which will register your hook for this event :
public class TestEventHandlerPlugin implements ConcurrentEventListener {
@Override
public void setEventPublisher(EventPublisher eventPublisher) {
eventPublisher.registerHandlerFor(TestRunFinished.class, teardown);
}
private EventHandler<TestRunFinished> teardown = event -> {
//run code after all tests
};
}
and then you will have to register the plugin :
- if you are running cucumber CLI you can use
-p
/--plugin
option and pass fully qualified name of the java class :your.package.TestEventHandlerPlugin
- for Junit runner :
@RunWith(Cucumber.class)
@CucumberOptions(plugin = "your.package.TestEventHandlerPlugin") //set features/glue as you need.
public class TestRunner {
}
Solution 3
With TestNG suite annotations would work as well.
@BeforeSuite
public static void setup() {
System.out.println("Ran once the before all the tests");
}
@AfterSuite
public static void cleanup() {
System.out.println("Ran once the after all the tests");
}
Related videos on Youtube
Author by
user7637341
Updated on October 14, 2022Comments
-
user7637341 over 1 year
Is there a way to run a method after all of the cucumber tests have been run?
The @After annotation would run after every individual test, right? I wan't something that would only run once, but at the very end.
-
John Mercier about 4 yearsSometimes there are things that need to happen to maintain the lifecycle of tests (ie start spring, destory spring context). These actions should not be part of a feature file. The feature is the front-end of the test. It should only be concern with explaining what is happening in the test. Not what is happening in the code.
-
John Mercier about 4 yearsIs there a solution that only involves executing tests from the cucumber Main class and not from a unit test?
-
payne almost 4 yearsThis worked best for me. The
@BeforeSuite
didn't seem to trigger, though. (But I only needed the teardown).