开发者

JUnit4 run all tests in a specific package using a testsuite

开发者 https://www.devze.com 2023-04-02 21:12 出处:网络
Is this possible in JUnit4? In JUnit3, I would do the following: public class MyTestSuite { public static Test suite() throws Exception {

Is this possible in JUnit4?

In JUnit3, I would do the following:

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. It allows filtering of classes in the Classpath by regular expressions like:

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


You can use 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号