Junit4中有没有一种方法可以将一些预初始化的数据(例如使用“@ClassRule”在用“@RunWith(Suite.class)”注释的类中初始化的http连接传递给所有子(即套件)测试类?另外如何保证套件中的类不能单独运行?
例如
class A {
private HttpClient client;
@Test
public void someTest() {
client.execute(...);
...
}
}
@RunWith(Suite.class)
@Suite.SuiteClasses({A.class})
public class Suite {
private static HttpClient client;
@ClassRule
public static ExternalResource resource= new ExternalResource() {
@Override
protected void before() throws Throwable {
client = new DefaultHttpClient();
}
}
谢谢
稻田
最佳答案
我认为使用 JUnit 无法实现这一目标。
我看到的唯一可能性是使用抽象类并在每个测试类中继承该抽象类。
public abstract class AbstractTest {
@ClassRule
public static ExternalResource resource= new ExternalResource() {
//...
}
}
public class MyTest extends AbstractTest {
}
Also how to ensure that the classes in the suite cannot be run individually?
我认为这也是不可能的。
但是你为什么想要那个? 修复单个失败的测试后,您真的想重新运行所有测试来检查一个测试是否正常工作吗? (当然,修复可能会带来副作用,但当您知道修复的测试正在运行时,您可以重新运行所有测试。) 此外,测试应该能够独立运行。
关于java - Junit 4如何在测试套件中初始化并将其传递给测试类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27981438/