Skip to content

How to re run suite without using listener in testng

I had a requirement to re-run a complete suite from the TestNG test method itself if any of the test-case from the suite failed. Is there any way to call complete suite using an XML file or Test class within the Test method? The complete suite should re-run the after-class method or teardown test-case or last test-case

@AfterClass or @AfterMethod I can’t use due to some other functionality I’m handling into it. I require to check in the last test method if any of the above test methods got failed then I should rerun the complete suite or class.

Or please suggest if there is any listener that tracks which test failed, suppose I had some 10 tests methods in which 3 and 7 are the main tests if they failed I had to rerun the complete suite that should check at the end of the test method (In Test method 10 we can say as per example.)


You could use rerun the suite in an AfterSuite method.

In your test class which would hold the AfterSuite method, declare two static int variables to keep the number of reruns and maximum allowed rerun count. Then in the AfterSuite method check this count and rerun the suite if maximum is not reached.

public class AnyTestClass {
    private static int counter;
    private static final int MAX_RERUN = 4;
    // replace "" with location of your suite xml -> something like src/test/......
    private static final String SUITE_LOC = "";

    // test methods.....

    public void afterSuite(ITestContext ctx) {
        if(counter == MAX_RERUN) {
        if(ctx.getFailedTests().size() > 0) {            

            // you could also have this list as static final in the class.
            List<String> suiteFiles = new ArrayList<>();
            TestNG runner = new TestNG();