Running a single JUnit test in Eclipse

EclipseJunit

Eclipse Problem Overview


If I have a test suite with multiple tests, when I try to run a single unit test, either from the context menu of the code editor, or from the JUnit view, it seems to insist on always running the entire suite, rather than the single test. Is there a way to disable to change this behavior so that I can ask to to run that, and only that, test.

Eclipse Solutions


Solution 1 - Eclipse

In the package explorer unfold the class. It should show you all methods. Right click on the one method you want to run, then select Run As -> JUnit from the context menu (just tested with Eclipse 3.4.1). Also selecting "Run" on a single entry in the JUnit-results view to re-run a test works in the same way.

Solution 2 - Eclipse

Fastest way I know of:

  1. Press Ctrl+Shift+ (moves cursor to current method declaration),
  2. press Alt+Shift+x (or d for debug) then press t (hotkey for "Run JUnit Test"),
  3. check test result,
  4. press Alt+ to get back to the line of code you were before.

If you want to run all tests, you can skip steps 1 & 4.

Solution 3 - Eclipse

In Eclipse 3.5, you can get around this by changing the test runner to JUnit 3. This will only work for JUnit 3 tests, not JUnit 4 tests. You can change the test runner by doing the following:

  1. Right click on one of the test methods in the Outline explorer
  2. Select Run As -> Run Configurations
  3. On the 'Test' tab, select 'Run a single test'
  4. In the Test Runner dropdown, select 'JUnit 3'

It may work in other versions as well.

Solution 4 - Eclipse

This is possible in Eclipse Indigo with JUnit 4.8.2. You right click the method you want to unit test individually in the Outline pane, and select Run As -> JUnit Test.

You can even selectively right click a Test method name in the source editor and select Run As -> Junit Test.

Solution 5 - Eclipse

Don't use Strg+F11 but the hotkey Alt+Shift+X -> T.
Then Eclipse will execute the current open file as a JUnit test. The green play button will only run the last chosen Run Configuration.

For me, it works well with Alt+Shift+X -> T.

Solution 6 - Eclipse

I'll add to the others by including a highly productive keyboard only way that allows you to debug a single unit test (method).

Move your cursor to the method name by using either

  • Ctrl+Shift+Up or
  • Ctrl+Shift+Down or
  • Ctrl+o then type the name of the method

Once your cursor is on the method name (or right before it):

  • Alt+Shift+D -> T (Debug)
  • Alt+Shift+X -> T (Run)

After you run the test you can go back to where your cursor was by doing:

Alt+Back

You almost get REPL like behavior by:

Ctrl+Shift+Up and Alt+Shift+X -> T and Alt+Back

You can also quickly set a breakpoint:

Ctrl+Shift+B

Solution 7 - Eclipse

Right click somewhere on the test method name in the file and select "Run" -> "Junit Test". I do it all the time with Kepler and JUnit 4.

Solution 8 - Eclipse

I had the same problem others have had using Eclipse 3.4.1 and JUnit 4 test runner -- couldn't run single test. But I found a suggestion somewhere else that resolved the problem. My test class was still extending junit.framework.TestCase. When I stopped extending TestCase, everything worked fine and I was able to run single JUnit tests with the JUnit 4 test runner. Of course, I needed to JUnit 4 annotations use static imports for the assert methods, but I had already done that -- I had just not removed the TestCase inheritance.

Solution 9 - Eclipse

To run only one test method.
In the (package explorer or project explorer) unfold the class. It should show you all methods. Right click on the one method you want to run, then select Run As -> JUnit from the context menu.
enter image description here

Solution 10 - Eclipse

If you have many tests in more than one file, and you want to run only the tests in a particular file, you could right click that file, and select run as -> junit test. Now, if you want to run only one test, contained in a file with many tests, my guess is (I dont have eclipse installed here) that the Outline view will list all test methods for a file, and you will probably be able to right click a single test method and execute it as a JUNit test.

Hope it helps.

Solution 11 - Eclipse

For me, it also does not work in Eclipse 3.4.2 , although it worked in the previous releases (I have tried running single method from package explorer and single junit test from junit view)

Solution 12 - Eclipse

I agree with the point about 3.4.2 no longer working the way it did in previous versions. Before you could right-click on a single test in a group of tests from the JUnit view and execute just that test. In the latest version it insists on always running all of them. Granted, the view does run the test you clicked on first and dumps the rest in a rollup called "Unrooted Tests". If anyone figures this out I'd like to know how to get around it.

Solution 13 - Eclipse

Reading some of the comments here, it seems you might be interested in running the tests for the code you change as you change it, without losing focus on the code you are working on. There's an eclipse plugin for doing just that. See infinitest.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser14128View Question on Stackoverflow
Solution 1 - EclipseVolker StolzView Answer on Stackoverflow
Solution 2 - EclipseschnattererView Answer on Stackoverflow
Solution 3 - EclipselucrussellView Answer on Stackoverflow
Solution 4 - EclipsewulfgarproView Answer on Stackoverflow
Solution 5 - EclipseguerdaView Answer on Stackoverflow
Solution 6 - EclipseAdam GentView Answer on Stackoverflow
Solution 7 - EclipseKyle DyerView Answer on Stackoverflow
Solution 8 - EclipseghirschhornView Answer on Stackoverflow
Solution 9 - EclipsedaniloView Answer on Stackoverflow
Solution 10 - EclipseTomView Answer on Stackoverflow
Solution 11 - EclipsetttView Answer on Stackoverflow
Solution 12 - Eclipsera9rView Answer on Stackoverflow
Solution 13 - EclipseunigeekView Answer on Stackoverflow