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.
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");
}
}
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 :
-p/--plugin option and pass fully qualified name of the java class : your.package.TestEventHandlerPlugin@RunWith(Cucumber.class)
@CucumberOptions(plugin = "your.package.TestEventHandlerPlugin") //set features/glue as you need.
public class TestRunner {
}
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");
}