JUnit4使用testsuite在特定包中运行所有测试

这在JUnit4中是否可行?

在JUnit3中,我将执行以下操作:

public class MyTestSuite { public static Test suite() throws Exception { doBeforeActions(); try { TestSuite testSuite = new TestSuite(); for(Class clazz : getAllClassesInPackage("com.mypackage")){ testSuite.addTestSuite(clazz); } return testSuite; } finally { doAfterActions } } ... } 

takari-cpsuite (最初由Johannes Link开发)提供了一个适合您需求的类路径套件。 它允许通过正则表达式过滤Classpath中的类,如:

 import org.junit.extensions.cpsuite.ClasspathSuite.*; ... @ClassnameFilters({"mytests.*", ".*Test"}) public class MySuite... 

您可以使用JUnitToolBox:

 @RunWith(WildcardPatternSuite.class) @SuiteClasses("**/*Test.class") public class MySuite { } 

Maven配置:

  com.googlecode.junit-toolbox junit-toolbox 1.5  

有关详细信息,请参阅https://code.google.com/p/junit-toolbox/ 。