3

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 ?

3
  • Is JUnit4 is not compatible to the earlier approach, it should be. Commented Jul 3, 2013 at 6:28
  • as I said, suite.addTestSuite(tempClz); expects tempClz to be Class<? extends TestCase> where I get an error (compile time). Commented Jul 3, 2013 at 6:30
  • I had the same issue, see my answer here stackoverflow.com/a/42423367/2267723 Commented Feb 23, 2017 at 18:22

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.