views:

88

answers:

5

I use JUnit 4 in eclipse. I have some test classes in my package and want to run them all. How?

+2  A: 

In the package explorer you can use the context menu of the package and choose run as junit test.

tangens
+1  A: 

Right-click on the package in the package explorer and select 'Run as' and 'Unit-Test'.

DerMike
+1  A: 

Right click on the package and choose "Run as Test" from the "Run as" submenu.

Adrian
+2  A: 

I used to declare a AllTests class so that I would also be able to run all tests from the command line:

public final class AllTests
{
  /**
   * Returns a <code>TestSuite</code> instance that contains all the declared
   * <code>TestCase</code> to run.
   * 
   * @return a <code>TestSuite</code> instance.
   */
  public static Test suite()
  {
    final TestSuite suite = new TestSuite("All Tests");

    suite.addTest(Test1.suite());
    suite.addTest(Test2.suite());
    suite.addTest(Test3.suite());

    return suite;
  }

  /**
   * Launches all the tests with a text mode test runner.
   * 
   * @param args ignored
   */
  public static final void main(String[] args)
  {
    junit.textui.TestRunner.run(AllTests.suite());
  }

} // AllTests

Where each test class defines

  /**
   * Returns a <code>TestSuite</code> instance that contains all
   * the declared <code>TestCase</code> to run.
   * 
   * @return a <code>TestSuite</code> instance.
   */
  public static final Test suite()
  {
    return new TestSuite(Test1.class); // change the class accordingly
  }
Gregory Pakosz
I also use this variant because it can run automatically after nightly builds
stacker
A: 

with JUnit 4 I like to use an annotated AllTests class:

@RunWith(Suite.class)
@Suite.SuiteClasses({ 

    // package1
    Class1Test.class,
    Class2test.class,
    ...

    // package2
    Class3Test.class,
    Class4test.class,
    ...

    })

public class AllTests {
    // Junit tests
}

and, to be sure that we don't forget to add a TestCase to it, I have a coverage Test (also checks if every public method is being tested).

Carlos Heuberger