Skip to content

How to Rerun Failed Test Cases In Cucumber Project (2024)

CucumberFramework

In automation testing efficiency is the key aspect of any framework. Selenium and Cucumber emerged as strong automated testing technologies, allowing testers to enhance their processes and identify bugs faster. However, managing test scripts can be challenging, especially when dealing with failures.In this article, we’ll explore how to rerun failed test cases in Cucumber TestNG Selenium project.

Understanding the Challenge

In test automation projects, executing hundreds or even thousands of test scripts is common. Sometimes we get inconsistent test results due to factors like an unstable environment, intermittent issues, or application updates. Sometimes few tests fail for no valid reason and work fine after rerun. This situation can be frustrating and time-consuming if the failures are not reproducible.

Occasionally, following bug fixes, it becomes necessary to selectively execute only the failed test cases to quickly validate the applied fixes. To handle such situations it is crucial to implement a retry logic that can automatically rerun the failed tests to maintain the testing momentum and ensure accurate results.

Implementing Rerun Functionality

There are two ways to achieve the rerun functionality.

  • testng-failed.xml
  • rerun plugin

TestNG provides inherent support to rerun the failed test cases. Whenever execution completes using the Testng.xml file, a testng-failed.xml file is created in the test-output folder. We can run this file by right-clicking on it just like the testng.xml file and it will execute only failed test cases.

Another approach is to use the rerun plugin option in the runner class. To use this we need to modify our existing Cucumber Runner class.

Setting Up the Project

Go to the IDE, open your test automation project and navigate to the cucumber Runner class.

What is Cucumber Runner Class?

TestRunner serves as the entry point for executing Cucumber tests in a Selenium project. This class contains @CucumberOptions annotation that is used to configure various options for cucumber tests for example the location of features file, their implementation, tags, plugins etc.

Modification of Cucumber Test Runner Class

Open the Cucumber runner class and add rerun:target/failedrerun.txt in the plugin section under Cucumber Options.”failedrerun.txt” is the file name that will be generated inside the target folder. This file will contain the path of failed scenarios. After adding the code the TestRunner class will look like this:

@CucumberOptions(
		features = { "src/test/resources/features" }, 
		glue = { "com.app.stepdefinition" }, // path of step definition
		tags = "@Testcase1",
		
		plugin = { "pretty",
		"html:./reports/cucumber-reports/cucumber-html/index.html",
	        "rerun:target/failedrerun.txt"} 
		)

public class TestRunner extends AbstractTestNGCucumberTests {}

In case of execution failure, it will generate a text file in the target folder. To see this file just refresh the project after the execution and it will be available in the target folder.

Rerun Failed Test Cases In The Cucumber
FailedScenarios

failedrerun.txt will contain the path of the feature file and line number for the scenarios that failed.

failedtc.text
ReRun.txt

Create a New Runner Class

The next step is to execute failed test scenarios. We need to create a class similar to our Cucumber TestRunner class which will contain the location of the file that we want to execute to rerun our failed scenarios.

@CucumberOptions(
		 features = { "@target/failedrerun.txt" }
	   glue = { "com.app.stepdefinition" }, // path of step definition
		
	  plugin = { "pretty",
		"html:./reports/cucumber-reports/cucumber-html/index.html",
		"rerun:target/failedrerun.txt"},
		monochrome =true,
		
		)

public class FailedRun extends AbstractTestNGCucumberTests{}

We have created another TestRunner and named it FailedRun.In this runner, we’ve specified ‘features = { “@target/failedrerun.txt” }’, which commands Cucumber to run the scenarios listed in the failedrerun.txt file which is located inside the target folder of the project. Cucumber will now rerun only the failed scenarios from the previous test run.

Modify TestNg.xml Suite

After Successful implementation of the retry mechanism either we can directly execute the failed test cases by going to the failed runner class and executing it from there or we can customize the testng.xml file to initiate the rerun of failed test cases.

<suite name="Our Suite" verbose="1" >
    <test name="App test on Chrome">
        <classes>
            <class name="com.app.runner.TestRunner" />
        </classes>
    </test>
    <test name="Rerun failed app tests">
        <classes>
            <class name="com.app.runner.FailedRun" />
        </classes>
    </test>
</suite>

We have created two tests under the test suite that will run sequentially. Therefore, in the given configuration “App test on Chrome” test will be executed first, using the class com.app.runner.TestRunner class and after the completion of the first test, the “Rerun failed app tests” test will be executed, using the class com.app.runner.FailedRun.

Conclusion

Rerun Failed Test Cases In Cucumber-Selenium framework is a critical aspect of maintaining an efficient and reliable testing process. By integrating with TestNG and implementing custom rerun logic, we can enhance the robustness of our test automation framework.

Visit this article to read how we can generate an Extent HTML report in the Selenium-Cucumber framework. You can also read official cucumber documentation as well to gain more insights about Cucumber and BDD.