下面是一个测试程序的例子: public class MyTestCase extends TestCase { /** * call super constrUCtor with a test name * string argument. * @param testName the name of the method that * should be run. */ public MyTestCase(String testName){ super(testName); } /** * called before every test * 在每项测试开始前被调用 */ PRotected void setUp() throws Exception { initSomething(); } /** * called after every test * 在测试完成后被调用 */ protected void tearDown() throws Exception { finalizeSomething(); } /** * this method tests that ... * 这个方法测试…… */ public void testSomeTest1(){ ... } /** * this method tests that ... * 这个方法测试…… */ public void testSomeTest2 (){ ... } }
下面是一个简单的例子: public class MyTestSuite extends TestSuite { public static Test suite() { TestSuite suite = new TestSuite("Test suite for ..."); // add the first test // 添加第一个测试 MyTestCase mtc = new MyTestCase("testSomeTest1"); suite.addTest(mtc); // add the second test // 添加第一个测试 MyTestCase mtc2 =new MyTestCase("testSomeTest2"); suite.addTest(mtc2); return suite; } }