How to rerun failed tests in Cucumber

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. Use this tutorial to setup the project.

Now, let us add 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 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 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 which we want to execute to rerun our failed scenarios. In the ‘features’ variable, you need mention the failedrerun.txt file and don’t forget that you must mention ‘@’ 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 below mentioned command

mvn clean test

After running the tests from commandline, 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 is failed.

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

We can see that 2 seperate reports are generated here.

The first Cucumber Report shows that out of 5 tests, 1 test is 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!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s