94

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.

Lii
  • 11,553
  • 8
  • 64
  • 88
user14128
  • 2,547
  • 4
  • 20
  • 16
  • 12
    It would be nice if you chose the best answer by checking the green mark next to the answer. – guerda Feb 24 '10 at 06:57

14 Answers14

103

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.

Volker Stolz
  • 7,274
  • 1
  • 32
  • 50
  • 3
    Unfortunately, this only works for test methods that are directly implemented in that class. It doesn't work for inherited @Test methods. – Christopher Barber Oct 22 '12 at 22:07
  • 2
    Note that it appears that you have to click on the method name. Clicking inside the method will run the entire class. (Or at least that's how it's behaving for me.) – emragins Jan 05 '15 at 19:30
  • It seams only to work from this one particular "package explorer" which is more than unhandy when it comes to quickly find a method you wand to test. And even than, the debugger seams to skip the other methods - which consumes extra time. There must be a better way ... – Macilias Aug 04 '15 at 15:25
  • and it`s fragile either... now eclipse decided not to skip other tests when running this way :( – Macilias Aug 04 '15 at 15:40
33

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.

Sk8erPeter
  • 6,899
  • 9
  • 48
  • 67
schnatterer
  • 7,525
  • 7
  • 61
  • 80
13

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.

lucrussell
  • 5,032
  • 2
  • 33
  • 39
  • Good call! Changing "Test Runner" to "JUnit 3" in the run configurations dialog worked for me in Eclipse 3.6.0 (Helios). Now I can run a single method in a JUnit test. FWIW, I've never had a problem running individual JUnit 3 or 4 test *classes*. – Adam Monsen Aug 31 '10 at 19:52
  • Perfect this worked for me as well. The answer above this didn't even though it seems to have more upvotes. – dreza Dec 18 '11 at 01:08
13

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.

wulfgarpro
  • 6,666
  • 12
  • 69
  • 110
  • 1
    it seems would test that specific test case. However, after that, it still continues with other test cases. – Jackie Jun 25 '13 at 02:48
  • @lwpro2 You have to be using JUnit 4 (annotations) to test a single test case. JUnit 3 (extend TestCase) will still run all related tests. – AnthonyW Jul 17 '13 at 14:12
  • 1
    wulfgar.pro I was letting @lwpro2 know that if you are running your JUnit test with a JUnit 4 runner (because, for example you are using JUnit 4.8.2) YET your test still extends TestCase, you will be unable to run a single test by itself. You must either use a JUnit 3 runner or convert your test to using annotations. – AnthonyW Jul 18 '13 at 17:06
  • thanks for the comments. However, i was using Junit4, with SpringJunit4ClassRunner. Anyway, not a big deal. commenting off the annotation is quick. – Jackie Jul 25 '13 at 02:46
  • this answer is simplest – Jackson Tale Mar 20 '15 at 09:23
10

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

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
9

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.

guerda
  • 23,388
  • 27
  • 97
  • 146
  • Thanks. But say you are working on a class that is being tested by a unit test. As you modify the class, how do you run its corresponding UnitTest without switching the window to the UnitTest file? – Bibek Shrestha Mar 22 '12 at 14:11
  • @bibstha You can use Ctrl+3 and then type "rerun". This allows you to rerun the last JUnit test case. Under Window->Preferences->General->Keys, you can assign an own shortcut for this action. Ctrl+Shift+F11 would be suitable, in my opinion. – guerda Mar 26 '12 at 12:00
3

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.

Kyle Dyer
  • 308
  • 1
  • 4
3

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

danilo
  • 7,680
  • 7
  • 43
  • 46
2

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.

ghirschhorn
  • 311
  • 1
  • 4
  • 7
1

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.

Tom
  • 43,810
  • 29
  • 138
  • 169
0

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.

unigeek
  • 2,656
  • 27
  • 27
0

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)

0

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.

ra9r
  • 4,528
  • 4
  • 42
  • 52
0

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)

Benjamin Peter
  • 4,009
  • 2
  • 23
  • 23