JUnit 4 使用 Suites 时@BeforeClass & @AfterClass

2022-09-01 11:49:55

在下面使用此方法时,通过设置 jUnit with Suites。我们遇到了一个问题,即在任何测试开始执行之前,每个Testclass中的所有@BeforeClass都将被执行。(对于@BeforeClass运行的每个n个TestClass文件,然后在它们执行后,它开始执行第一个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
        ...

答案 1

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

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

答案 2

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

@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的内部结构不够了解,不知道这些是否可以成为一个因素。


推荐