Is it possible to run JUnit tests from multiple packages in Eclipse?

JavaEclipseTestingJunit

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

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

Solution 4 - Java

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

Solution 5 - Java

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

Solution 6 - Java

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

Solution 7 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwillView Question on Stackoverflow
Solution 1 - JavaspanView Answer on Stackoverflow
Solution 2 - JavajhegedusView Answer on Stackoverflow
Solution 3 - JavaicyerasorView Answer on Stackoverflow
Solution 4 - JavaJurgen HannaertView Answer on Stackoverflow
Solution 5 - JavachillysapienView Answer on Stackoverflow
Solution 6 - JavaPhilView Answer on Stackoverflow
Solution 7 - Javabruno condeView Answer on Stackoverflow