Java Tutorial/JUnit/Test Suite
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
Define your own test case runner with reflection
import java.lang.reflect.Method;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class TestSample extends TestCase {
public TestSample(String name) {
super(name);
}
public void setUp() {
}
public void tearDown() {
}
public void testMe() {
assertTrue(true);
}
public static Test suite() {
return new TestSuite(TestSample.class);
}
public static void main(String[] args) {
TestFinder.run(TestSample.class, args);
}
}
class TestFinder {
public static void run(Class which, String[] args) {
TestSuite suite = null;
if (args.length != 0) {
try {
java.lang.reflect.Constructor ctor;
ctor = which.getConstructor(new Class[] { String.class });
suite = new TestSuite();
for (int i = 0; i < args.length; i++) {
suite.addTest((TestCase) ctor.newInstance(new Object[] { args[i] }));
}
} catch (Exception e) {
System.err.println("Unable to instantiate " + which.getName() + ": " + e.getMessage());
System.exit(1);
}
} else {
try {
Method suite_method = which.getMethod("suite", new Class[0]);
suite = (TestSuite) suite_method.invoke(null, null);
} catch (Exception e) {
suite = new TestSuite(which);
}
}
junit.textui.TestRunner.run(suite);
}
}
Per-Suite Setup and Tear-Down
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class TestClassTwo extends TestCase {
public TestClassTwo(String method) {
super(method);
}
// This one takes a few hours...
public void testLongRunner() {
assertEquals(2300, 50);
}
public void testShortTest() {
assertEquals(140, 5);
}
public void testAnotherShortTest() {
assertEquals(586, 10);
}
public static Test suite() {
TestSuite suite = new TestSuite();
// Only include short tests
suite.addTest(new TestClassTwo("testShortTest"));
suite.addTest(new TestClassTwo("testAnotherShortTest"));
TestSetup wrapper = new TestSetup(suite) {
protected void setUp() {
oneTimeSetUp();
}
protected void tearDown() {
oneTimeTearDown();
}
};
return wrapper;
}
public static void oneTimeSetUp() {
}
public static void oneTimeTearDown() {
}
}
Test suite
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class TestClassOne extends TestCase {
public TestClassOne(String method) {
super(method);
}
public void testAddition() {
assertEquals(4, 2 + 2);
}
public void testSubtraction() {
assertEquals(0, 2 - 2);
}
}
class TestClassTwo extends TestCase {
public TestClassTwo(String method) {
super(method);
}
public void testLongRunner() {
assertEquals(2300, 0);
}
public static Test suite() {
TestSuite suite = new TestSuite();
// Only include short tests
suite.addTest(new TestClassTwo("testShortTest"));
suite.addTest(new TestClassTwo("testAnotherShortTest"));
return suite;
}
}
class TestClassComposite extends TestCase {
public TestClassComposite(String method) {
super(method);
}
static public Test suite() {
TestSuite suite = new TestSuite();
// Grab everything:
suite.addTestSuite(TestClassOne.class);
// Use the suite method:
suite.addTest(TestClassTwo.suite());
return suite;
}
}
Use the test suite method
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class TestClassComposite extends TestCase {
public TestClassComposite(String method) {
super(method);
}
static public Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(TestClassOne.class);
suite.addTest(TestClassTwo.suite());
return suite;
}
}
class TestClassOne extends TestCase {
public TestClassOne(String method) {
super(method);
}
public void testLongRunner() {
assertEquals(2300, 50);
}
}
class TestClassTwo extends TestCase {
public TestClassTwo(String method) {
super(method);
}
public void testLongRunner() {
assertEquals(2300, 50);
}
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new TestClassTwo("testLongRunner"));
return suite;
}
}