在 Eclipse 中运行单个 JUnit 测试

如果我有一个包含多个测试的测试套件,当我试图运行单个单元测试时,无论是从代码编辑器的上下文菜单还是从 JUnit 视图,它似乎都坚持要运行整个套件,而不是单个测试。是否有一种方法可以禁用来更改此行为,以便我可以要求运行该测试,并且只运行该测试。

93788 次浏览

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.

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.

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.

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)

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.

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.

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.

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.

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

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.

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.

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.

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

If by test you might even mean single test method I like to use "Run Tests of Selected Member" using a key binding.

When I am inside of a test method only that test method will be run – if I have the cursor in the class scope it will run the whole class. I changed the default binding to something easy like Ctrl+r since I use it a lot.

Please see:

Eclipse Preferences → General → Keys

(If you don't see that keys entry it might come from the MoreUnit plugin – which is great anyway and you might want to have it)