Running a single JUnit test in Eclipse

1 minute read

A lot of the test-driven developers may find it annoying to run a single JUnit test in Eclipse: if there is a test suite with multiple tests, when running a single unit test, either from the context menu of the code editor, or from the JUnit view, Eclipse seems to always insist on running the entire suite, rather than the single test.

Like the example below, running testOne will trigger running the rest of the tests in the suite which are grouped under the “Unrooted Tests” node. This is quite irritating sometimes since running unexpected tests may slow down the workflow if they take time to finish. It’s also quite confusing since programmers get unwanted test results. Some of my coworkers solve this problem by commenting out unwanted tests each time before running a test. However, it’s not very effective and it’s error-prone.

A single test triggers multiple tests

This happens mainly because we are running JUnit 3 tests with the JUnit 4 runner. Before version 4.6, the JUnit 4 runner seems to have the problem of falling back to run a single JUnit 3 test.

Running JUnit 3 tests with JUnit 4 runner

So…the solution is quite simple:

  1. change the JUnit runner to JUnit 3 in Eclipse, or
  2. upgrade your JUnit 4 library to 4.6 or higher.

Now the problem is gone and the test-driven developers are happy :-).

Categories:

Updated:

Leave a Comment