Monday, October 25, 2010

Adding test suites to a test suite in Junit 3.x

Normally in a junit 3.x test suite we will add test classes (that has our test methods) as entries. 
As an example,

public class AllTests {

    public static Test suite() {

        TestSuite suite = new TestSuite(
                "All tests wrapped in a suite");
        // $JUnit-BEGIN$
        suite.addTestSuite(SampleTest.class);
        suite.addTestSuite(AnotherSampleTest.class);
        // $JUnit-END$
        return suite;
    }

}

But what if we want to put several test suites into a master test suite.
It can be done in this way:

public static Test suite() throws Exception {
         TestSuite suite = new TestSuite(
                "Master test suite that has all test suites");
        // $JUnit-BEGIN$
        suite.addTest(AllTests.suite());
        suite.addTest(AnotherAllTests.suite());       
        // $JUnit-END$
        return suite;
    }


And last but not least, you can mix and match both.

No comments:

Post a Comment