Test runners

Scott Babcock edited this page Jun 11, 2018 · 36 revisions

IDE support - graphical runners

NetBeans, Eclipse and IntelliJ IDEA have native graphical test runners built in.

Console based Test runner

JUnit provides tools to define the suite to be run and to display its results. To run tests and see the results on the console, run this from a Java program:

    org.junit.runner.JUnitCore.runClasses(TestClass1.class, ...);

or this from the command line, with both your test class and junit on the classpath:

    java org.junit.runner.JUnitCore TestClass1 [...other test classes...]

This usage is documented further here: http://junit.org/javadoc/latest/org/junit/runner/JUnitCore.html

Using older runners with Adapter

JUnit4TestAdapter enables running JUnit-4-style tests using a JUnit-3-style test runner. To use it, add the following to a test class:

      public static Test suite() {
            return new JUnit4TestAdapter('YourJUnit4TestClass'.class);

Caveat: See #1189 for issues with including a JUnit-4-style suite that contains a JUnit-3-style suite.

@RunWith annotation

When a class is annotated with @RunWith or extends a class annotated with @RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit.

  • JavaDoc for @RunWith http://junit.org/javadoc/latest/org/junit/runner/RunWith.html
  • The default runner is BlockJUnit4ClassRunner which supersedes the older JUnit4ClassRunner
  • Annotating a class with @RunWith(JUnit4.class) will always invoke the default JUnit 4 runner in the current version of JUnit, this class aliases the current default JUnit 4 class runner.

Specialized Runners




You can specify groups of tests to be excluded or included by using the Categories runner. Once you have annotated certain methods with @Category(MyCategory.class), you can use the --filter option to restrict which tests will be run:

java org.junit.runner.JUnitCore --filter=org.junit.experimental.categories.IncludeCategories=MyCat1,MyCat2 --filter=org.junit.experimental.categories.ExcludeCategories=MyCat3,MyCat4

You may filter tests according to any instance of FilterFactory. The --filter option takes the general form:

java [Runner] --filter=[FilterFactory]=[Categories,]
  • Categories is a standard runner enabling subsets of tests tagged with certain categories to execute/be excluded from a given test run.
  • More information at Categories page.

Experimental Runners


Third Party Runners

Some popular third party implementations of runners for use with @RunWith include:

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.