drkthng
drkthng

Reputation: 6909

TestNG: All subsequent Test classes are skipped when @BeforeClass method fails?

My setup:

Why this setup?:

Here's what happens:

When I run the tests via a testNG xml file and there is an exception within the @BeforeClass method of one of the Test classes, then all subsequent Test classes are skipped by TestNG.

Why does this happen? How to prevent it?

When I change the annotation in the TestBase class to @BeforeSuite for example, then all tests are run, even if there is an exception in on of the @BeforeClass methods.

Example:

When you run the xml file, complete RunAllTestClasses02 class is skipped.

testNG xml file:

<?xml version="1.0" encoding="UTF-8"?>

<suite name = "MiscSuite">
    <test name = "MiscTest">
        <classes >
            <class name="drkthng.misc.RunAllTestClasses01" />
            <class name="drkthng.misc.RunAllTestClasses02" />
        </classes>
    </test>
</suite>

TestBase class with a @BeforeClass method:

public abstract class RunAllTestClassesBase {

    @BeforeClass
    public void beforeClass() {
        // do something that all Test classes will need
    }
}

Test class that throws Exception within @BeforeClass method:

public class RunAllTestClasses01 extends RunAllTestClassesBase {

    @BeforeClass
    public void beforeClass() {
        Assert.assertTrue(false);
    }

    @Test
    public void Test01() {
        Assert.assertTrue(true);
    }
}

Upvotes: 10

Views: 16895

Answers (2)

Dexter
Dexter

Reputation: 98

Try to add @AfterClass(alwaysrun = true) or/and @AfterMethod(alwaysrun=true) as by default they are skipped if either BeforeClass or BeforeMethod are not completed.

The documentation on testNG Configuration Failures, Policy, and alwaysRun explains whether/when configuration failures cause listener methods (alwaysRun and other listeners) to be skipped, failure policies and best practices.

Upvotes: 0

niharika_neo
niharika_neo

Reputation: 8531

This was a bug in Testng. solved in 6.9.5. Please upgrade.

Upvotes: 4

Related Questions