0

How can I make only failed scenarios to be run again automatically on failure ?

Here is some clue on what I am doing:

  • Pass TestRunner class from command line through cucumber-testng.xml file at run-time.
  • I am able to see rerun.txt file after scenario failed, with feature/GM/TK/payment.feature:71 (pointing to failed scenario) but failed scenario is not automatically re-run.

The "TestRunner" java file

@RunWith(Cucumber.class)
@CucumberOptions(strict = true, 
    features = { "src/test/resources/" },  //feature file location
    glue = { "com/test/stepdefs", "com.test.cucumber.hooks" },  //hooks and stepdef location
    plugin = { "json:target/cucumber-report-composite.json", "pretty", "rerun:target/rerun.txt"}
)
public class CucumberTestRunner extends AbstractTestNGCucumberTests
{
}

The "RunFailedTest" Class to re-run from rerun.txt file

@RunWith(Cucumber.class)
@CucumberOptions(
    strict = false,
    features = { "@target/rerun.txt"  },  //rerun location
    glue = { "com/test/stepdefs", "com.test.cucumber.hooks" }, //hooks and stepdef location
    plugin = {"pretty", "html:target/site/cucumber-pretty", "json:target/cucumber.json"}
)
class RunFailedTest
{
}
Stphane
  • 3,368
  • 5
  • 32
  • 47
kapil daksh
  • 1
  • 1
  • 2
  • Are you looking to dynamically identify the failed tests to rerun, or are you just manually separating them out into another file? It looks like the latter, in which case you should consider using [tags](https://github.com/cucumber/cucumber/wiki/Tags) and specifying them in your CucumberOptions. – jsheeran Feb 07 '18 at 09:58
  • Possible duplicate of [How to rerun the failed scenarios using Cucumber?](https://stackoverflow.com/questions/11719898/how-to-rerun-the-failed-scenarios-using-cucumber) – Marit Feb 08 '18 at 12:04
  • jsheeran yeah dynamically failed scenario gets stored in rerun.txt and automatically run instantly as fail accoutered once first execution done – kapil daksh Feb 10 '18 at 04:44
  • Why do you have both junit and testng in CucumberTestRunner? And only junit in the RunFailedTest? How are you running the test? – Grasshopper Feb 19 '18 at 13:42

2 Answers2

0

you can achieve it by using gherkin with qaf it generates testng XML configuration for failed scenarios that you can use for rerun. It also support scenario rerun on fail by setting retry.count property.

user861594
  • 5,733
  • 3
  • 29
  • 45
0

using Cucumber + Maven + TestNG first, you don't need "@RunWith(Cucumber.class)" as you have mentioned in your question, if you are using TestNG, only "@CucumberOptions" is required. When you start your test execution, all scenario failures will be written to file "target/rerun.txt" as per the configuration mentioned in your Runner file. Now, you need to create one more Runner file (for example - "FailureRunner") and in this file provide the path of "@target/rerun.txt" ( this already have the details of the failure scenarios ) as -> features = { "@target/rerun.txt" }

Now, you need to UPDATE your TestNG.xml file and include the "FailureRunner" as below-

    <class name="Class path of Your First Runner Class name" />
    <class name="class path of FailureRunner Class" />

Once you do all the above steps and run your execution, the first execution will write the failure scenarios in the "target/rerun.txt" and After that, the "FailureRunner" Class will be executed which will pick up the "@target/rerun.txt" file and Hence, Failure scenarios will be executed.

I have executed in the same way and it works fine, let me know if it helps !!