In JUnit 3 I simply called
suite.addTestSuite( MyTest.class )
However if MyTest is a JUnit 4 test which does not extend TestCase this doesn't work. What should I do instead to create a suite of tests?
JUnit 4 – @Ignore Annotation The JUnit 4 @Ignore annotation could be applied for a test method, to skip its execution. In this case, you need to use @Ignore with the @Test annotation for a test method you wish to skip. The annotation could also be applied to the test class, to skip all the test cases under a class.
Create Test Runner Class It imports the JUnitCore class and uses the runClasses() method that takes the test class name as its parameter. Compile the Test case and Test Runner classes using javac. Now run the Test Runner, which will run the test case defined in the provided Test Case class. Verify the output.
Do not put either in ANT_HOME/lib , and instead include their locations in your CLASSPATH environment variable. Add both JARs to your classpath using -lib . Specify the locations of both JARs using a <classpath> element in a <taskdef> in the build file. Leave ant-junit.
As outlined in Recipe 4.6, JUnit calls setUp( ) before each test, and tearDown( ) after each test. In some cases you might want to call a special setup method once before a series of tests, and then call a teardown method once after all tests are complete. The junit. extensions.
For those with a large set of 3.8 style suites/tests that need to coexist with the new v4 style you can do the following:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
// Add a JUnit 3 suite
CalculatorSuite.class,
// JUnit 4 style tests
TestCalculatorAddition.class,
TestCalculatorDivision.class
})
public class CalculatorSuite {
// A traditional JUnit 3 suite
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(TestCalculatorSubtraction.class);
return suite;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With