30

当使用下面的这种方法时,通过设置 jUnit with Suites。当每个 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
        ...
4

3 回答 3

51

在类中编写一个@BeforeClass方法,该方法AllTests将在套件启动时执行。

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
于 2009-12-17T20:32:30.797 回答
1

我对@RunWithJUnit 不太熟悉,所以我可能做错了什么,但我似乎无法复制你描述的行为。与班级:

@RunWith(Suite.class)
@Suite.SuiteClasses( { FirstTest.class, SecondTest.class, ThirdTest.class })
public class AllTests {
    // empty
}

FirstTest.java 看起来像这样:

public class FirstTest {
    @BeforeClass
    public static void doBeforeClass() {
         System.out.println("Running @BeforeClass for FirstTest");
    }

    @Test
    public void doTest() {
        System.out.println("Running @Test in " + getClass().getName());
    }
}

...与 SecondTest.java 和 ThirdTest.java 几乎相同。我得到测试输出:

Running @BeforeClass for FirstTest
Running @Test in FirstTest
Running @BeforeClass for SecondTest
Running @Test in SecondTest
Running @BeforeClass for ThirdTest
Running @Test in ThirdTest

这适用于 Sun 的 JDK 1.6.0_12 上的 JUnit 4.5.0(Eclipse 3.5.1 中的默认 JUnit)。你能发现我的例子和你的有什么不同吗?也许是不同的 JDK/JVM?我不太了解 JUnit 的内部结构,不知道这些是否是一个因素。

于 2009-12-17T13:50:43.593 回答
-4

我认为,@BeforeClass在实例化时执行。

于 2009-12-17T13:27:00.530 回答