Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My setup:

  • A TestBase class containing a @BeforeClass method
  • Several Test classes extending from TestBase class and also containing a @BeforeClass method
  • testNG 6.8.8

Why this setup?:

  • I need the @BeforeClass in the TestBase class to provide setup that all testclasses will need and I don't want to repeat in every Test class. For example thread-id-dependent login credentials.
  • TestBase class also instantiates the Selenium WebDriver
  • I need the @BeforeClass in the Test classes to initialize everything that all @Test methods will need to use but that only needs to (or must) be built/invoked once for all tests. This includes calls to said WebDriver instance (that's why a "normal" constructor doesn't work here)

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);
    }
}
like image 557
drkthng Avatar asked Jul 17 '15 13:07

drkthng


People also ask

How do you skip the test failed test cases in TestNG?

Use the parameter enabled=false at @Test. By default, this parameter is set as True. Use throw new SkipException(String message) to skip a test. Conditional Skip − User can have a condition check.

Why is a TestNG test getting skipped?

We can Skip complete test without executing it or we can Skip a test when a specific condition is not satisfied. In TestNG, @Test(enabled=false) annotation is used to skip a test case if it is not ready to test.

What is the difference between ignore and skip in TestNG?

Ignore means do not run it at all, and skip with the combinations of Listeners can be use for listening dependent methods and/or test. So let assume you have dependency between two tests and /or methods, test 2 can be performed only if test 1 pass. Skip will hapen for test 2 if test 1 fails.


1 Answers

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

like image 105
niharika_neo Avatar answered Nov 15 '22 21:11

niharika_neo