【发布时间】:2010-12-27 15:35:34
【问题描述】:
当使用下面的这种方法时,通过设置带有套件的 jUnit。当每个 Testclass 中的所有 @BeforeClass 都将在任何测试开始执行之前执行时,我们遇到了问题。 (对于每个 n 个 TestClass 文件,@BeforeClass 运行,然后在它们执行后,它开始执行第一个 MyTest.class 文件@Test)
这将导致我们分配大量资源和内存。 我的想法是它一定是错误的,每个@BeforeClass 不应该只在实际测试类执行之前运行,而不是在套件启动时运行?
@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
...
【问题讨论】:
-
它们是在每个类的测试之前执行,还是只在第一个测试之前执行(但是第二个在没有再次运行所有 @BeforeClass 的情况下运行)?后者看起来不错,因为 @BeforeClass 在该测试中的 @Test 方法之前运行。我想内存量不会改变,除非你在每个班级的测试之后清理(而且这些也只在整个套件完成后才会发生)。
-
我现在得到的是每个 @BeforeClass 都首先运行。 @BeforeClass (Mytests) @BeforeClass (Mytests2) @Test (MyTests) @Test (MyTests2) 在我看来,这是不正确的。如果我错了,请纠正我,但一定是设置错误导致此问题。