I have a Java project that uses Cucumber 7, Maven surefire, and JUnit 4. It is a project requirement to be able to rerun a failed feature file immediately after it fails (not rerun after the entire test run as done normally). So the flow would be something like:
- Run BeforeAll()
- Run Before()
- Run test
- If test fails, rerun until MAX_RETRY(2) is reached
- Run After()
- Do Steps 2-5 until all tests are run
- Run AfterAll()
I've seen people using Cucumber listeners for similar-ish problems but that is not an option for me as I'm on 7.3.3 and the EventListener is deprecated (AFAIK). Also read something about an extended Cucumber runner but is outdated. This is the closest response I found but uses Junit5. I've tried using the rerunFailingTestsCount
parameter through Maven Surefire but that reruns the features after the entire test suite is run. If you have any other suggestions, I would be very grateful. Thanks!
This is my current Cucumber Runner (without rerun implemented). I run the main() to execute my tests.
@RunWith(Cucumber.class)
@CucumberOptions(
tags="@xxx",
plugin = {"pretty"},
features = {"classpath:features"},
glue = {"StepDefinitions"}
)
public class Runner {
private static String[] options = {
"classpath:Features",
"--glue", "StepDefinitions",
"--tags", "@xxx",
"--plugin", "pretty",
};
public static void main(String[] args) {
Stream<String> cucumberOptions = Stream.concat(Stream.of(defaultOptions), Stream.of(args));
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
byte exitstatus = Main.run(cucumberOptions.toArray(String[]::new), contextClassLoader);
// other stuff done for custom pdf reports
}
}