java JUnit4 使用测试套件运行特定包中的所有测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7331214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:33:17  来源:igfitidea点击:

JUnit4 run all tests in a specific package using a testsuite

javajunitjunit4test-suite

提问by Fortega

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
     }
  }

...

}

采纳答案by oers

The takari-cpsuite(originally developed by Johannes Link) offers a classpath-suite which should fit your needs. It allows filtering of classes in the Classpath by regular expressions like:

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

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

回答by rcomblen

You can use JUnitToolBox:

您可以使用 JUnitToolBox:

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

Maven config:

马文配置:

<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/