JUnit 4 @BeforeClass &@AfterClass 使用套件时 [英] JUnit 4 @BeforeClass & @AfterClass when using Suites
问题描述
当使用下面的这种方法时,通过设置带有套件的 jUnit.当每个测试类中的所有 @BeforeClass 将在任何测试开始执行之前执行时,我们遇到了问题.(@BeforeClass 运行每 n 个 TestClass 文件,然后在它们执行后,它开始执行第一个 MyTest.class 文件 @Test)
When using this approach below, by setting up the jUnit with Suites. We got the problem when all @BeforeClass in every Testclass will be executed before any tests starts to execute. (For each n TestClass file the @BeforeClass runs, then after they have execute, it started to execute the first MyTest.class files @Test)
这会导致我们分配很多资源和内存.我的想法是它一定是错误的,每个@BeforeClass 不应该只在实际测试类执行之前运行,而不是在套件启动时运行吗?
This will cause that we allocate up much resources and memory. My thoughts was that it must be wrong, shouldn't each @BeforeClass run only before the actual testclass is executed, not when the Suite is started?
@RunWith(Suite.class)
@Suite.SuiteClasses({ MyTests.class, Mytests2.class, n1, n2, n })
public class AllTests {
// empty
}
public class MyTests { // no extends here
@BeforeClass
public static void setUpOnce() throws InterruptedException {
...
@Test
...
public class MyTests2 { // no extends here
@BeforeClass
public static void setUpOnce() throws InterruptedException {
...
@Test
...
推荐答案
在 AllTests
类中编写一个 @BeforeClass
方法,该方法将在套件启动时执行.
Write a @BeforeClass
method in the AllTests
class which will be executed when the suite is started.
public class MyTests1 {
@BeforeClass
public static void beforeClass() {
System.out.println("MyTests1.beforeClass");
}
@Before
public void before() {
System.out.println("MyTests1.before");
}
@AfterClass
public static void afterClass() {
System.out.println("MyTests1.AfterClass");
}
@After
public void after() {
System.out.println("MyTests1.after");
}
@Test
public void test1() {
System.out.println("MyTests1.test1");
}
@Test
public void test2() {
System.out.println("MyTests1.test2");
}
}
public class MyTests2 {
@BeforeClass
public static void beforeClass() {
System.out.println("MyTests2.beforeClass");
}
@Before
public void before() {
System.out.println("MyTests2.before");
}
@AfterClass
public static void afterClass() {
System.out.println("MyTests2.AfterClass");
}
@After
public void after() {
System.out.println("MyTests2.after");
}
@Test
public void test1() {
System.out.println("MyTests2.test1");
}
@Test
public void test2() {
System.out.println("MyTests2.test2");
}
}
@RunWith(Suite.class)
@Suite.SuiteClasses( { MyTests1.class, MyTests2.class })
public class AllTests {
@BeforeClass
public static void beforeClass() {
System.out.println("AllTests.beforeClass");
}
@Before
public void before() {
System.out.println("AllTests.before");
}
@AfterClass
public static void afterClass() {
System.out.println("AllTests.AfterClass");
}
@After
public void after() {
System.out.println("AllTests.after");
}
@Test
public void test1() {
System.out.println("AllTests.test1");
}
@Test
public void test2() {
System.out.println("AllTests.test2");
}
}
输出:
AllTests.beforeClass
MyTests1.beforeClass
MyTests1.before
MyTests1.test1
MyTests1.after
MyTests1.before
MyTests1.test2
MyTests1.after
MyTests1.AfterClass
MyTests2.beforeClass
MyTests2.before
MyTests2.test1
MyTests2.after
MyTests2.before
MyTests2.test2
MyTests2.after
MyTests2.AfterClass
AllTests.AfterClass
这篇关于JUnit 4 @BeforeClass &@AfterClass 使用套件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!