Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rerun the failed scenarios using Cucumber?

Tags:

cucumber

I'm using Cucumber for my tests. How do I rerun only the failed tests?

like image 710
Kalyan Avatar asked Jul 30 '12 10:07

Kalyan


People also ask

How do you rerun a failed Testcase?

By overriding retry() method of the interface in your class, you can control the number of attempts to rerun a failed test case. Now if we run TestNG. xml we see failed test case is executed one more time as we gave retry count= 1. Test case is marked as failed only after it reaches max retry count.

How do you rerun the failed test cases in JUnit?

Rerun Failed Tests with JUnit 4 ExampleWe need two classes, one of them is our Rule Class's RetryRule and the other is our Test Class's RetryRuleTest. In RetryRuleTest class, I will open www.swtestacademy.com and get its title and check it with the WRONG expected title.

How do I run failed test cases in Maven?

During development, you may re-run failing tests because they are flaky. To use this feature through Maven surefire, set the rerunFailingTestsCount property to be a value larger than 0. Tests will be run until they pass or the number of reruns has been exhausted.


2 Answers

Run Cucumber with rerun formatter:

cucumber -f rerun --out rerun.txt 

It will output locations of all failed scenarios to this file.

Then you can rerun them by using

cucumber @rerun.txt 
like image 95
Andrei Botalov Avatar answered Sep 28 '22 07:09

Andrei Botalov


Here is my simple and neat solution.

Step 1: Write your cucumber java file as mentioned below with rerun:target/rerun.txt. Cucumber writes the failed scenarios line numbers in rerun.txt as shown below.

features/MyScenaios.feature:25 features/MyScenaios.feature:45 

Later we can use this file in Step 2. Name this file as MyScenarioTests.java. This is your main file to run your tagged scenarios. If your scenarios has failed test cases, MyScenarioTests.java will write/mark them rerun.txt under target directory.

@RunWith(Cucumber.class) @CucumberOptions(     monochrome = true,     features = "classpath:features",     plugin = {"pretty", "html:target/cucumber-reports",               "json:target/cucumber.json",               "rerun:target/rerun.txt"} //Creates a text file with failed scenarios               ,tags = "@mytag"            ) public class MyScenarioTests   {  } 

Step 2: Create another scenario file as shown below. Let's say this as FailedScenarios.java. Whenever you notice any failed scenario run this file. This file will use target/rerun.txt as an input for running the scenarios.

@RunWith(Cucumber.class) @CucumberOptions(     monochrome = true,     features = "@target/rerun.txt", //Cucumber picks the failed scenarios from this file      format = {"pretty", "html:target/site/cucumber-pretty",             "json:target/cucumber.json"}   ) public class FailedScenarios {  } 

Everytime if you notice any failed scenarios run the file in Step 2

like image 20
vkrams Avatar answered Sep 28 '22 08:09

vkrams