开发者

How to run all JUnit tests of a given package?

I use JUnit 4 in eclipse. I have some test classes in my package and want to run them all. How?开发者_JAVA百科


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


In eclipse if you right click the folder and select Run As JUnit Test only the tests in that folder will be run (i.e. tests in nested subfolders will not be run). 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.

Using this I created something like the following

package com.mycompany.myproject.mymodule;

import org.junit.runner.RunWith;

import com.googlecode.junittoolbox.SuiteClasses;
import com.googlecode.junittoolbox.WildcardPatternSuite;

@RunWith(WildcardPatternSuite.class)
@SuiteClasses({ "**/*Test.class" })
public class RunAllMyModuleTests {
}

I added the required dependencies (jar files) using this in my mavin build (in addition to the junit dependency):

<dependency>
    <groupId>com.googlecode.junit-toolbox</groupId>
    <artifactId>junit-toolbox</artifactId>
    <version>1.5</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit-dep</artifactId>
    <version>4.8.2</version>
</dependency>

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.


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


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
  }


With JUnit5, you can easily create a "suite" class, that will run all tests in a package (or even subpackages, it works recursively):

@RunWith(JUnitPlatform.class)
@SelectPackages("my.test.package")
public class MySuite {
}

Once that's done, you can run this suite with "Run as Test".


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜