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

eclipsejavajunittesting

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.

Best Answer

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.