基于junit4, 参考:http://www.yiibai.com/junit/what-is-junit.html
public class App { @BeforeClass // 初始化类时调用 public static void testBeforeClass(){ System.out.println("before class"); } @AfterClass // 所有测试用例执行完毕时调用 public static void testAfterClass(){ System.out.println("after class"); } @Before // 每个测试用例运行之前调用 public void testBeforeMethod(){ System.out.println("before method"); } @After // 每个测试用例运行之后调用 public void testAfterMethod(){ System.out.println("after method"); } @Test // 测试用例 public void testCase1(){ System.out.println("test case1"); } @Test(timeout = 3000) // 测试用例,设置3秒超时 public void testCase2(){ System.out.println("test case2"); } @Ignore // 暂时忽略该用例 public void testIgnore(){ System.out.println("ignore"); } } 结果: before class before method test case1 after method before method test case2 after method after class
@RunWith(Suite.class) @Suite.SuiteClasses({ App.class, App1.class }) // 把App,App1测试类一起执行(安先后顺序) public class SuitTest { }