How to rerun failed tests in Cucumber

Last Updated On

HOME

The previous tutorial explained the Integration of Cucumber with Selenium and TestNG. Sometimes, inconsistent test results are common as a result of an unstable environment such as network issue or Database down and soon. A few tests may fail for no apparent reason and then rerun successfully. We are sometimes required to run only failed test cases after bug fixes in order to validate fixes quickly. We will learn how to rerun failed test cases in the Cucumber with TestNG project in this post.

Cucumber provides a rerun plugin option in the Runner class that generates a file which contains the information about the failed tests.

The Cucumber Framework with Selenium and TestNG can be found here. Refer to this tutorial to setup the project – Integration of Cucumber with Selenium and TestNG.

Now, let us add a rerun plugin to the Cucumber Runner class. Here, we are creating a failedrerun.txt file that contains the information about the failed test. This file will be created under the target folder.

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
   
@CucumberOptions(tags = "", features = {"src/test/resources/features/LoginPage.feature"}, glue = {"com.example.definitions"},
                 plugin = {"rerun:target/failedrerun.txt"})
   
public class CucumberRunnerTests extends AbstractTestNGCucumberTests {
   
}

Create a Second Runner Class

The next step is to run failed test scenarios present in the text file. We need to create a class same as our runner class which will contain the location of the file that we want to execute to rerun our failed scenarios. In the ‘features’ variable, you need to mention the failedrerun.txt file, and don’t forget that you must mention the ‘@’ symbol before the file path.

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
   
@CucumberOptions(tags = "", features = {"@target/failedrerun.txt"}, glue = {"com.example.definitions"},
                 plugin = {})
   
public class FailedRunnerTests extends AbstractTestNGCucumberTests {
   
}

Run the tests using the below-mentioned command

mvn clean test

After running the tests from the command line, first, all the tests will be executed. If any test fails, a failedrerun.txt file will be generated that contains the details about the failed tests.

In the below screenshot, we can see that a scenario starting at line 7 has failed.

Once the first round of execution ends, Cucumber Runner goes to the second runner and runs the failed tests that are mentioned in failedrerun.txt.

We can see that 2 separate reports are generated here.

The first Cucumber Report shows that out of 5 tests, 1 test failed.

The second Cucumber Report shows that the one failed test is rerun again, and it again failed.

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

Hooks in Cucumber
Data Driven Testing using Scenario Outline in Cucumber
Integration Testing of Springboot with Cucumber and JUnit4
Background in Cucumber
Allure Report with Cucumber5, Selenium and TestNG

2 thoughts on “How to rerun failed tests in Cucumber

  1. mvn clean test
    If i run above command, two runner files will run automatically. or for second runner i need to run manually

    Like

Leave a comment