简体   繁体   English

JUnit4使用测试套件在特定软件包中运行所有测试

[英]JUnit4 run all tests in a specific package using a testsuite

Is this possible in JUnit4? 在JUnit4中可以吗?

In JUnit3, I would do the following: 在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
     }
  }

...

}

The takari-cpsuite (originally developed by Johannes Link ) offers a classpath-suite which should fit your needs. takari-cpsuite (最初由Johannes Link开发)提供了一个适合您需求的classpath-suite。 It allows filtering of classes in the Classpath by regular expressions like: 它允许通过正则表达式对Classpath中的类进行过滤,例如:

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

You can use JUnitToolBox: 您可以使用JUnitToolBox:

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

Maven config: Maven配置:

<dependency>
<groupId>com.googlecode.junit-toolbox</groupId>
<artifactId>junit-toolbox</artifactId>
<version>1.5</version>
</dependency>

see https://code.google.com/p/junit-toolbox/ for more details. 有关更多详细信息,请参见https://code.google.com/p/junit-toolbox/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM