4

I am in need of running some of the selected test cases from my test suite. Test cases are available in different test classes. Is it possible to create some custom annotation and configure junit to run only test cases with that annotation?

Please let me know if you have suggestions. Thanks

  • Have a look at http://stackoverflow.com/questions/3627392/how-to-add-test-cases-to-a-suite-using-junit – walters Sep 18 '12 at 13:11

1 Answers1

1

A TestSuite is a Composite of Tests. It runs a collection of test cases. Here is an example using the dynamic test definition.

 TestSuite suite= new TestSuite();
 suite.addTest(new MathTest("testAdd"));
 suite.addTest(new MathTest("testDivideByZero"));

Alternatively, a TestSuite can extract the tests to be run automatically. To do so you pass the class of your TestCase class to the TestSuite constructor.

 TestSuite suite= new TestSuite(MathTest.class);

This constructor creates a suite with all the methods starting with "test" that take no arguments.

A final option is to do the same for a large array of test classes.

 Class[] testClasses = { MathTest.class, AnotherTest.class }
 TestSuite suite= new TestSuite(testClasses);

Source: http://www.junit.org/apidocs/junit/framework/TestSuite.html

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
walters
  • 1,427
  • 3
  • 17
  • 28