53

Is it possible to run JUnit tests for multiple packages at the same time without manually creating test suites.

For example if I have the hierarchy:

code.branchone
code.branchone.aaa
code.branchone.bbb
code.branchtwo
code.branchtwo.aaa
code.branchtwo.bbb

Is it possible to:

  1. Run all tests in code.branchone and in descendent packages
  2. Run all tests in say code.branchone.aaa and code.branchtwo.bbb

The problem I see with manually creating test suites is that when new tests come along you may forget to add them.

will
  • 1,175
  • 4
  • 17
  • 20
  • 5
    None of the answers below work. I have the same problem. I can run all the tests in "code.branchone.aaa" but if I go to the root "code.branchone" it says "No test found with test runner 'Junit 4'". Keep in mind the "code.branchone" doesn't have any source in it. – Bdoserror Jan 05 '12 at 01:06
  • Extra confusion note: my project (built by someone else) already has a launch configuration which works from a higher directory in the hierarchy ("code"). I copied that config and modified it for the subdirectory I wanted, it still doesn't work and that directory spec is the only difference between them. – Bdoserror Jan 05 '12 at 01:13
  • Hmm, maybe the issue is because the one that works runs from a directory which is "sub-project"(?) under the main project. e.g. in this setup the working config runs for "src/it/java": Project (root of tree) with 1 child, src/it/java And the one I want is a subdir of src/it/java, and in the source view the common parent of the subdirectories I want to run doesn't have it's own entry. e.g.: Project (root) has src/it/java as child, and srt/it/java has 2 children, sub1.subA & sub1.subB, and I want to run all tests from src/it/java/sub1 – Bdoserror Jan 05 '12 at 01:25

8 Answers8

29

Yes, it is possible. The easiest way for me at least is to add a test suite class. It can look like this:

package tests;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

import tests.message.ATest;
import tests.validator.BTest;
import tests.validator.CTest;
import tests.validator.DTest;

@RunWith(Suite.class)
@SuiteClasses({ ATest.class, 
        BTest.class, 
        CTest.class, 
        DTest.class })
public class AllTests {

}

This will allow you to test any class that you import no matter what package it is in. To run this in eclipse you just right click the AllTests class and run it as JUnit test. It will then run all the tests you define in @SuiteClasses.

This will work with linked sources as well, I use it all the time.

span
  • 5,405
  • 9
  • 57
  • 115
  • 3
    I don't suppose there's a way to use package names or wildcards with `@SuiteClasses`, is there? I've seen http://johanneslink.net/projects/cpsuite.jsp, http://burtbeckwith.com/blog/?p=52, and http://stackoverflow.com/questions/1293880/run-all-tests-in-a-source-tree-not-a-package but none is quite as nice of a solution as I'd hoped for. – jacobq Oct 02 '13 at 18:36
  • This helped me solve an issue where 2 test cases were interfering each other (separately they were green but failed when they were executed in a sequence). Nice trick :-) – rlegendi Oct 30 '14 at 07:56
  • java.lang.Exception: No runnable methods – Abdullah Nurum Jun 16 '20 at 14:31
24

An other way:

Click on the black triangle denoted by red rectangle in the picture below (in your Eclipse, not here :).)

enter image description here

Then open run configurations, create a new configuration and then set "Run all tests..." as exemplified in the image below.

enter image description here

jhegedus
  • 20,244
  • 16
  • 99
  • 167
  • 7
    Only on the src (root of the classpath) it will run all unit tests of the project. If chosen on a particular package it only runs the tests within the chosen package itself, but not the tests from the sub packages. – Heri Dec 22 '15 at 12:57
14

Maybe not exactly what the original question was, but you can easily run all tests of a whole Project, by simply right-clicking the project -> Run As JUnitTest. Don't worry where the annotated classes reside, this will be scanned.

This does not work if applied to the test-src-folder or a package with subpackes. Quite a shame actually -.-

icyerasor
  • 4,973
  • 1
  • 43
  • 52
3

I'm sure u can tweak this a bit. Make a Collection of the CLASSES_DIR property and loop over it in the findClasses method. (junit4)

http://burtbeckwith.com/blog/?p=52

Jurgen Hannaert
  • 973
  • 10
  • 22
  • I also used this in a different way than the one you suggested, using annotations. This link is really useful! – oshai Jan 27 '11 at 19:37
1

I beleieve that you can add all your test packages to a single directory. If you right click on this directory, then you should find the "run as -> JUnit test" option available. This will run all tests contained in the directory and will catch anything you've added. Any new tests get put in there with the rest of them and whatever package name you have it doesn't matter. Hope that helps

chillysapien
  • 2,256
  • 1
  • 26
  • 42
1

Sure, right-click on the packages you want, and select Run As... JUnit Test

Phil
  • 815
  • 5
  • 13
1

In Eclipse, on your debug/run configurations you have the following options:

  1. Run a single test
  2. Run all tests in the selected project, package or source folder

I think the second option is your friend in this case.

bruno conde
  • 47,767
  • 15
  • 98
  • 117
0

If you are using JUnit 5 you can cherry pick which tests you want to run:

import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

import tests.Test1;
import tests.Test2;
import tests.Test3;

@Suite
@SelectClasses({
    Test1.class,
    Test2.class,
    Test3.class
})
public class AllMyTests {

}

If you want to select which packages to run you can:

import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectPackages({
    "com.package1",
    "com.package2",
    "com.package3"
})
public class AllMyTests {

}

If you need to be able to exclude certain sub-packages you can do:

import org.junit.platform.suite.api.ExcludePackages;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectPackages({
    "com.package1",
    "com.package2",
    "com.package3"
})
@ExcludePackages({
    "com.package1.subpackage",
    "com.package2.othersubpackage"
})
public class AllMyTests {

}

Other useful annotations:

  • To match a particular pattern: @IncludeClassNamePatterns({"^.*ATests?$"})
  • To exclude a particular pattern: @ExcludeClassNamePatterns({"^.*ATests?$"})

Examples were based on: https://howtodoinjava.com/junit5/junit5-test-suites-examples/

jspek
  • 438
  • 3
  • 10