我有@BeforeClass
设置我运行测试套件,如下所示:
@RunWith(Categories.class)
@IncludeCategory(IntegrationTest.class)
.
.
.
public class IntegrationTestSuite {
@BeforeClass
public static void initialise() throws Exception {
// Integration test-specific config.
}
}
当我通过套件运行所有测试时,这很有效。但是,当我运行单独的测试时,显然这些东西不会被执行。是否有更优雅的方式允许我在测试用例级别重用测试类别设置?
答案 0 :(得分:1)
考虑创建一个仅执行初始化一次的自定义规则(可能使用ExternalResourse)。使用一种测试为其他测试进行初始化的机制是一种反模式。它太脆弱了,因为它取决于运行测试的顺序,并且在仅运行单个测试时也会失败。我认为@Rule
机制是一个更好的解决方案。
答案 1 :(得分:0)
我建议将全局标志用作静态上下文,或者在属性文件中使用:
public static boolean runTestCaseStandAlone = false;
或
boolean runTestCaseStandAlone = properties.get("run.test.case.alone");
将测试套件方法更新为:
public class IntegrationTestSuite {
@BeforeClass
public static void initialise() throws Exception {
if(!GLOBALCONTEXT.runTestCaseStandAlone){
// Integration test-specific config.
}
}
}
为您的测试用例创建一个Base类,例如
public class BaseTest ....
@BeforeClass
public static void initialise() throws Exception {
if(GLOBALCONTEXT.runTestCaseStandAlone){
// Integration test-specific config.
}
}
确保所有单个测试用例都扩展了上述基类。