Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run all tests in Junit 4

I want to be able to run all tests in a project programmatically. I know Eclipse has a "Run as JUnit test" configuration which somehow grabs all the tests in a project and run them. Is there any way for me to also grab the list of tests programmatically and run them? Or is there some good way to construct a test suite containing all the test cases without manually listing out every one (all 700+) of them?

I've tried the "New... -> Test Suite" option in Eclipse, but that seems to work only for JUnit 3, identifying tests by their extending from TestCase

The test classes are JUnit 4, so their only distinguishing characteristic is the annotation, no naming convention, no subclassing from TestCase.

Thanks in advance!

like image 955
alexloh Avatar asked Feb 12 '10 20:02

alexloh


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.

How do you run a test in JUnit 4?

Step 1: The method implemented under the @BeforeAll annotation is executed once. Step 2: The method implemented under the @BeforeEach annotation executes before the first test. Step 3: The method implemented under the @Test annotation is executed.


2 Answers

Though it does not really solve your immediate problem, I find it a very useful general practice to create suites and suites of suites, e.g. for a package something like PackageFooSuite etc. and assemble these suites in one or more suites again, like ModuleFooSuite and have one top-level suite, like AllTestsSuite. That way it's easy to run both all tests in one step as well as submodule tests for the package I'm currently working on (and have the tests run quicker than if I would always run all of them):

@RunWith(Suite.class) @Suite.SuiteClasses({ PackageFooSuite.class, PackageBarSuite.class} ) public final class AllTestsSuite {} // or ModuleFooSuite, and that in AllTests 
like image 80
Fabian Steeg Avatar answered Sep 19 '22 22:09

Fabian Steeg


None of the other answers did it for me. I had 40k tests I needed to run, so manually listing every class was not an option.

I did it with ClasspathSuite. A test suite that runs all Junit4 and Junit3 test cases in the class path is as follows:

import org.junit.extensions.cpsuite.ClasspathSuite; import org.junit.extensions.cpsuite.ClasspathSuite.*; import org.junit.runner.RunWith; import org.junit.runner.JUnitCore; import static org.junit.extensions.cpsuite.SuiteType.*;  @RunWith(ClasspathSuite.class) @SuiteTypes({ JUNIT38_TEST_CLASSES, TEST_CLASSES }) public class RunAllSuite {         /* main method not needed, but I use it to run the tests */         public static void main(String args[]) {                 JUnitCore.runClasses(RunAllSuite.class);         } } 

I needed to run it from command line, so this is what I did:

  1. Downloaded cp-1.2.6.jar
  2. Create the previously mentioned RunAllSuite
  3. Compile the class, javac RunAllSuite.java -cp cpsuite-1.2.6.jar;junit-4.8.1.jar
  4. run it with target tests in the class path, java -cp cpsuite-1.2.6.jar;junit-4.8.1.jar;path/to/runallsuite/folder;target/classes;target/test-classes RunAllSuite

And that's it. With the RunAllSuite above, anywhere in your code you can just do JUnitCore.runClasses(RunAllSuite.class), which runs all tests in class path. There are other config options as well which are explained in the ClasspathSuite home page.

Note also that the class given above does not print anything. If that is needed, you can do

import org.junit.extensions.cpsuite.ClasspathSuite; import org.junit.extensions.cpsuite.ClasspathSuite.*; import org.junit.runner.RunWith; import org.junit.runner.JUnitCore; import org.junit.internal.TextListener; import static org.junit.extensions.cpsuite.SuiteType.*;  @RunWith(ClasspathSuite.class) @SuiteTypes({ JUNIT38_TEST_CLASSES, TEST_CLASSES }) public class RunAllSuite {         public static void main(String args[]) {                 JUnitCore junit = new JUnitCore();                 junit.addListener(new TextListener(System.out));                 junit.run(RunAllSuite.class);         } } 
like image 42
eis Avatar answered Sep 18 '22 22:09

eis