我正在使用 JUnit 测试套件运行一些测试,其中一个测试使用 @Parameterized 运行多次。我发现当我运行测试时,@Parameterized 函数在@BeforeClass 之前运行。这是预期的行为还是发生了其他事情?我原以为 @BeforeClass 会在任何测试开始之前运行。
这是我的测试套件:
@RunWith(Suite.class)
@SuiteClasses({ Test1.class, Test2.class })
public class TestSuite {
@BeforeClass
public static void setup() throws Exception {
// setup, I want this to be run before anything else
}
}
Test1 使用@Parameterized:
public class Test1 {
private String value;
// @Parameterized function which appears to run before @BeforeClass setup()
@Parameterized.Parameters
public static Collection<Object[]> configurations() throws InterruptedException {
// Code which relies on setup() to be run first
}
public Test1(String value) {
this.value = value;
}
@Test
public void testA() {
// Test
}
}
如何解决此问题以在运行其他任何东西之前运行 @BeforeClass setup() 函数?
最佳答案
不幸的是,这是按预期工作的。 JUnit在开始测试之前需要枚举所有的测试用例,对于参数化测试,使用@Parameterized.Parameters
注解的方法来判断有多少个测试。
关于java - JUnit @Parameterized 函数在测试套件中的@BeforeClass 之前运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22391202/