Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit4 run all tests in a specific package using a testsuite

Tags:

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      }   }  ...  } 
like image 715
Fortega Avatar asked Sep 07 '11 08:09

Fortega


People also ask

How do I run all JUnit tests at once?

In order to run all of the tests in a directory including tests in nested directories you will need to use something like googlecode. junittool box. Right clicking on this class and selecting Run As JUnit test runs all of the tests in the specified directory including all tests in nested subfolders.

What is Test Suite in JUnit?

Test suite is used to bundle a few unit test cases and run them together. In JUnit, both @RunWith and @Suite annotations are used to run the suite tests. This chapter takes an example having two test classes, TestJunit1 & TestJunit2, that run together using Test Suite.


2 Answers

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... 
like image 181
oers Avatar answered Oct 14 '22 19:10

oers


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.

like image 41
rcomblen Avatar answered Oct 14 '22 18:10

rcomblen