用不同的参数运行两个相同的测试

Rav*_*avi 6 java junit unit-testing

我有一个15-20个不同测试用例的测试,我想用两个不同的参数运行相同的测试,这两个参数应该被传递给测试的BeforeClass方法,例如:

public class TestOne {
    private static ClassToTest classToTest;

    @BeforeClass
    public static void setUp() throws Exception {
        classToTest = new ClassToTest("Argument1", "Argument2");
    }

    @Test
    public void testOne() {
    ........roughly 15 - 20 tests here
}

public class TestTwo {
    private static ClassToTest classToTest;

    @BeforeClass
    public static void setUp() throws Exception {
        classToTest = new ClassToTest("Argument3", "Argument4");
    }

    @Test
    public void testOne() {
    ........roughly 15 - 20 tests here, same as in TestOne
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,这两个测试之间的唯一区别在于setup方法,它将不同的值传递给ClassToTest的构造函数.我不想在两个类中复制测试方法,但更喜欢继承或其他一些智能方法在一个类中实现这一点.

Lou*_*man 6

这似乎是JUnit4的完美用例@Parameters; 请参阅https://blogs.oracle.com/jacobc/entry/parameterized_unit_tests_with_junithttp://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/.也就是说,您必须将初始化从setUp方法移动到测试类的构造函数.