junit4使用特定的测试方法创建测试套件 [英] junit4 creating test suite with specific test methods
问题描述
在junit4中,我想执行来自不同类的特定测试方法,即要创建一个具有来自不同类的特定测试方法的测试套件.
In junit4 I want to execute specific test methods from different classes i.e want create a test suite with specific test methods from different classes.
让我说我有2节课:
public class Test_Login {
@Test
public void test_Login_001(){
System.out.println("test_Login_001");
}
@Test
public void test_Login_002(){
System.out.println("test_Login_002");
}
@Test
public void test_Login_003(){
System.out.println("test_Login_003");
}
}
public class Logout {
@Test
public void test_Logout_001(){
System.out.println("test_Logout_001");
}
@Test
public void test_Logout_002(){
System.out.println("test_Logout_002");
}
@Test
public void test_Logout_003(){
System.out.println("test_Logout_003");
}
}
从上述类中,我只想执行测试方法test_Login_001,test_Login_003和test_Logout_002.
From the above classes I want to execute test methods test_Login_001 , test_Login_003 , test_Logout_002 only.
如何在junit4中实现?
How this can be achieved in junit4 ?
推荐答案
自从引入JUnit 4.8类别以来,存在一个干净的解决方案,请创建一个TestSuite:
Since JUnit 4.8 introduced Categories there exists a clean solution, create a TestSuite:
@RunWith(Categories.class)
@IncludeCategory(MustHaveTests.class)
@SuiteClasses( { Test_Login.class, Test_Logout.class })
public class MustHaveTestsTestSuite {
public interface MustHaveTests { /* category marker */ }
}
并在要使用TestSuite运行的每个测试上方添加@Category(MustHaveTests.class),例如:
And add the @Category(MustHaveTests.class) above every test you would like to run with the TestSuite, e.g.:
@Category(MustHaveTests.class)
@Test
public void test_Login_001(){
System.out.println("test_Login_001");
}
运行TestSuite时,将仅执行MustHaveTests-带标签"的测试.有关@Category的更多详细信息: https://github.com/junit-team/junit4/wiki /categories
When running the TestSuite only the MustHaveTests-"tagged" tests will be executed. More Details on @Category: https://github.com/junit-team/junit4/wiki/categories
这篇关于junit4使用特定的测试方法创建测试套件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!