Test runners

Tobias Preuss edited this page Aug 16, 2016 · 25 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

Suite

Parameterized

Categories

  • Categories is a standard runner enabling subsets of tests tagged with certain categories to execute/be excluded from a given test run.

Experimental Runners

Enclosed

Third Party Runners

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