以下是我已經和它運行得很好。
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ TestSuite1.class, TestSuite2.class })
public class CompleteTestSuite {
@BeforeClass
public static void setUpClass() {
System.out.println("Master setup");
}
@AfterClass public static void tearDownClass() {
System.out.println("Master tearDown");
}
}
這是我的測試套件1(對於測試套件2也是這樣做的)。
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(value = Suite.class)
@SuiteClasses(value = { TestCase1.class })
public class TestSuite1 {}
這裏是我的測試類。創建testcase1和testcase2。
import static org.junit.Assert.assertEquals;
import org.junit.BeforeClass;
import org.junit.Test;
public class TestCase1 {
@BeforeClass
public static void setUpClass() {
System.out.println("TestCase1 setup");
}
@Test
public void test1() {
assertEquals(2 , 2);
}
}
你應該有5類 completesuite suite1 suite2 test1的 test2的
,並確保有junit在您的構建路徑。這應該運行!
這裏是輸出
Master setup
TestCase1 setup
Master tearDown
根據這個答案它應該工作:http://stackoverflow.com/questions/82949/before-and-after-suite-execution-hook-in-junit- 4-X#177069 – Sebi