Kalyan
Kalyan

Reputation: 529

How to rerun the failed scenarios using Cucumber?

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

Upvotes: 46

Views: 91138

Answers (5)

Randy Eppinger
Randy Eppinger

Reputation: 1114

I know this is old, but I found my way here first and then later found a much more up to date answer (not the accepted, Cyril Duchon-Doris' answer): https://stackoverflow.com/a/41174252/215789

Since cucumber 3.0 you can use --retry to specify how many times to retry a scenario that failed. https://cucumber.io/blog/open-source/announcing-cucumber-ruby-3-0-0/

Just tack it on to your cucumber command: cucumber ... --retry 2

Upvotes: 3

Shinigamiyuu
Shinigamiyuu

Reputation: 45

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'json:target/cucumber-reports/json/cucumber.json',
                    '--plugin', "rerun:target/rerun.txt",
                    '--glue', 'steps',
                    'src/test/resources']
        }
    }
}

task cucumberRerunFailed() {
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'json:target/cucumber-reports/json/cucumber.json',
                    '@target/rerun.txt']
        }
    }
}

Upvotes: 1

Vikram
Vikram

Reputation: 7517

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

Upvotes: 14

Popescu M
Popescu M

Reputation: 72

You need at least version 1.2.0 in order to use the @target/rerun.txt new feature. After that just create a runner that runs at the end and uses this file. Also, if you are using Jenkins, you can put a tag on the random failures features so the build doesn't fails unless after being ran twice.

Upvotes: 0

Andrei Botalov
Andrei Botalov

Reputation: 21096

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

Upvotes: 63

Related Questions