Java JUnit测试套件-只包括特定的测试,而不是类中的所有测试?
我正在设置JunitJava JUnit测试套件-只包括特定的测试,而不是类中的所有测试?,java,unit-testing,junit,test-suite,Java,Unit Testing,Junit,Test Suite,我正在设置Junit测试套件。例如,我知道如何使用运行类中所有测试的标准方法来设置测试套件 是否可以创建一个测试套件,并且只从几个不同的类运行某些测试 如果是,我该如何做 是否可以创建一个测试套件并仅从中运行某些测试 几个不同的班级 选项(1)(首选此选项):您实际上可以使用@Category来执行此操作,您可以查看 选项(2):您只需执行以下几个步骤即可: 您需要在测试用例中使用JUnit自定义测试@Rule,并带有一个简单的自定义注释(如下所示)。基本上,规则将在运行测试之前评估所需的条件。
测试套件
。例如,我知道如何使用运行类中所有测试的标准方法来设置测试套件
是否可以创建一个测试套件
,并且只从几个不同的类运行某些测试
如果是,我该如何做
是否可以创建一个测试套件并仅从中运行某些测试
几个不同的班级
选项(1)(首选此选项):您实际上可以使用@Category
来执行此操作,您可以查看
选项(2):您只需执行以下几个步骤即可:
您需要在测试用例中使用JUnit自定义测试@Rule
,并带有一个简单的自定义注释(如下所示)。基本上,规则将在运行测试之前评估所需的条件。如果满足前提条件,将执行测试方法,否则,将忽略测试方法
现在,您需要像往常一样将所有测试类添加到@Suite
代码如下:
MyTestCondition自定义注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyTestCondition {
public enum Condition {
COND1, COND2
}
Condition condition() default Condition.COND1;
}
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();
}
}
};
}
}
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
}
}
@RunWith(Suite.class)
@Suite.SuiteClasses({MyTests.class
})
public class MyTestSuite {
}
MyTestRule类:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyTestCondition {
public enum Condition {
COND1, COND2
}
Condition condition() default Condition.COND1;
}
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();
}
}
};
}
}
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
}
}
@RunWith(Suite.class)
@Suite.SuiteClasses({MyTests.class
})
public class MyTestSuite {
}
MyTests类:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyTestCondition {
public enum Condition {
COND1, COND2
}
Condition condition() default Condition.COND1;
}
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();
}
}
};
}
}
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
}
}
@RunWith(Suite.class)
@Suite.SuiteClasses({MyTests.class
})
public class MyTestSuite {
}
MyTestSuite类:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyTestCondition {
public enum Condition {
COND1, COND2
}
Condition condition() default Condition.COND1;
}
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();
}
}
};
}
}
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
}
}
@RunWith(Suite.class)
@Suite.SuiteClasses({MyTests.class
})
public class MyTestSuite {
}