JUnit 测试套件 - 仅包括特定测试,而不是 Class 中的每个测试?
JUnit Test Suite- Only include Specific tests, not every test in the Class?
我正在设置 Junit Test Suite
。我知道如何使用 运行 在 class 中进行所有测试的标准方法来设置测试套件,例如 here。
是否可以从多个不同的 class 中创建 test suite
且仅 运行 某些测试?
如果可以,我该怎么做?
Is it possible to create a test suite and only run certain tests from
several different classes?
选项 (1)(更喜欢这个): 您实际上可以使用 @Category
执行此操作,您可以查看 here
选项 (2):您可以按照以下说明通过几个步骤完成此操作:
您需要使用 JUnit 自定义测试 @Rule
并在您的测试用例中使用简单的自定义注释(如下所示)。基本上,规则将在 运行 测试之前评估所需的条件。如果满足前置条件,则执行Test方法,否则,将忽略Test方法。
现在,您像往常一样需要class所有 @Suite
测试。
代码如下:
MyTestCondition 自定义注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyTestCondition {
public enum Condition {
COND1, COND2
}
Condition condition() default Condition.COND1;
}
MyTestRule class:
public class MyTestRule implements TestRule {
//Configure CONDITION value from application properties
private static String condition = "COND1"; //or set it to COND2
@Override
public Statement apply(Statement stmt, Description desc) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
MyTestCondition ann = desc.getAnnotation(MyTestCondition.class);
//Check the CONDITION is met before running the test method
if(ann != null && ann.condition().name().equals(condition)) {
stmt.evaluate();
}
}
};
}
}
我的测试class:
public class MyTests {
@Rule
public MyTestRule myProjectTestRule = new MyTestRule();
@Test
@MyTestCondition(condition=Condition.COND1)
public void testMethod1() {
//testMethod1 code here
}
@Test
@MyTestCondition(condition=Condition.COND2)
public void testMethod2() {
//this test will NOT get executed as COND1 defined in Rule
//testMethod2 code here
}
}
我的测试套件class:
@RunWith(Suite.class)
@Suite.SuiteClasses({MyTests.class
})
public class MyTestSuite {
}
我正在设置 Junit Test Suite
。我知道如何使用 运行 在 class 中进行所有测试的标准方法来设置测试套件,例如 here。
是否可以从多个不同的 class 中创建 test suite
且仅 运行 某些测试?
如果可以,我该怎么做?
Is it possible to create a test suite and only run certain tests from several different classes?
选项 (1)(更喜欢这个): 您实际上可以使用 @Category
执行此操作,您可以查看 here
选项 (2):您可以按照以下说明通过几个步骤完成此操作:
您需要使用 JUnit 自定义测试 @Rule
并在您的测试用例中使用简单的自定义注释(如下所示)。基本上,规则将在 运行 测试之前评估所需的条件。如果满足前置条件,则执行Test方法,否则,将忽略Test方法。
现在,您像往常一样需要class所有 @Suite
测试。
代码如下:
MyTestCondition 自定义注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyTestCondition {
public enum Condition {
COND1, COND2
}
Condition condition() default Condition.COND1;
}
MyTestRule class:
public class MyTestRule implements TestRule {
//Configure CONDITION value from application properties
private static String condition = "COND1"; //or set it to COND2
@Override
public Statement apply(Statement stmt, Description desc) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
MyTestCondition ann = desc.getAnnotation(MyTestCondition.class);
//Check the CONDITION is met before running the test method
if(ann != null && ann.condition().name().equals(condition)) {
stmt.evaluate();
}
}
};
}
}
我的测试class:
public class MyTests {
@Rule
public MyTestRule myProjectTestRule = new MyTestRule();
@Test
@MyTestCondition(condition=Condition.COND1)
public void testMethod1() {
//testMethod1 code here
}
@Test
@MyTestCondition(condition=Condition.COND2)
public void testMethod2() {
//this test will NOT get executed as COND1 defined in Rule
//testMethod2 code here
}
}
我的测试套件class:
@RunWith(Suite.class)
@Suite.SuiteClasses({MyTests.class
})
public class MyTestSuite {
}