I have huge set of JUnits in my projects. Out of which, I would like to execute certain set by looking up the classes from a property file.
Prior to JUnit 4:
public class TestSuite
{
public static Test suite()
{
TestSuite suite = new TestSuite();
Class [] array = readFromPropertyFile();
for (Class tempClz : array)
{
suite.addTestSuite(tempClz);
}
}
}
However in Junit4, I'm forced to annotate the classes at compile time as follows:
@RunWith(Suite.class)
@Suite.SuiteClasses({
Test1.class,
Test2.class
})
public class TestSuite
{
}
I cannot, switch back to Junit 3.x because, all the test classes are no more extending TestCase
meaning Test1.class is not of type Class<? extends TestCase>
Is there a way to dynamically configure the testsuite in this case ?
The cpsuite project uses a custom annotation to determine the tests to run at runtime based on patterns. This isn't what you want, but it is a good start. You can take the source code for it and replace the "find tests" part with reading from your property file.
Note that the annotation/code is different for different versions of JUnit 4.X. If you are still on JUnit 4.4 or below, you'll need the older one.