简体   繁体   English

jUnit 4中的TestSuite设置

[英]TestSuite Setup in jUnit 4

I've managed to find out how to make a TestSuite in jUnit 4, but I really miss the v3 possibility of wrapping a suite in a TestSetup. 我已经设法找到如何在jUnit 4中创建一个TestSuite,但我真的很想念在TestSetup中包装套件的v3的可能性。

Any ideas as to how to get some @BeforeClass/@AfterClass setup executed for a suite of test cases in jUnit 4? 关于如何在jUnit 4中为一组测试用例执行一些@ BeforeClass / @ AfterClass设置的任何想法?

Ie

@RunWith(Suite.class)
@Suite.SuiteClasses({Test1.class, Test2.class})
public class MyTestSuite {
    @BeforeClass public static void setUpClass() {
        // Common initialization done once for Test1 + Test2
    }
    @AfterClass public static void tearDownClass() {
        // Common cleanup for all tests
    }
}

Unfortunately the above code fragment doesn't work. 不幸的是,上面的代码片段不起作用。 @BeforeClass only works on a per-test-class basis. @BeforeClass仅适用于每个测试类。

Here is what I have and it runs just fine. 这是我拥有的,它运行得很好。

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");
    }

}

Here is my test suite 1 (do the same for test suite 2). 这是我的测试套件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 {}

And here is my test class. 这是我的测试课。 Create both testcase1 and testcase2. 创建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);
    }
}    

you should have 5 classes completesuite suite1 suite2 test1 test2 你应该有5个类completeuite suite1 suite2 test1 test2

and make sure you have Junit in your build path. 并确保你的构建路径中有Junit。 This should run! 这应该运行!

Here is the output 这是输出

Master setup
TestCase1 setup
Master tearDown

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM