Running a single JUnit test in Eclipse
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.
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.
Fastest way I know of:
- Press Ctrl+Shift+↑ (moves cursor to current method declaration),
- press Alt+Shift+x (or d for debug) then press t (hotkey for "Run JUnit Test"),
- check test result,
- 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.
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:
- Right click on one of the test methods in the Outline explorer
- Select Run As -> Run Configurations
- On the 'Test' tab, select 'Run a single test'
- In the Test Runner dropdown, select 'JUnit 3'
It may work in other versions as well.