Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a single JUnit test method in Eclipse? [duplicate]

In a JUnit test case with multiple @Test annotations, how does one selectively run tests ?

For e.g., from the following code, how does one run just one test method ?

   @Test
   public void testHelloEmpty() 
   {
      assertEquals(h.getName(),"");
      assertEquals(h.getMessage(),"Hello!");
   }

   @Test
   public void testHelloWorld() 
   {
      // h.setName("World");
      assertEquals(h.getName(),"World");
      assertEquals(h.getMessage(),"Hello World!");
   }

I have tried to just highlight one @Test method and tried to run it, but it doesn't work that way.

like image 222
AussieMoss Avatar asked Nov 12 '13 04:11

AussieMoss


People also ask

How can individual unit test method be executed or debugged?

Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T). If individual tests have no dependencies that prevent them from being run in any order, turn on parallel test execution in the settings menu of the toolbar.

Is parallel execution possible in JUnit?

Yes, You can. Show activity on this post. JUnit Toolbox provides JUnit runners for parallel execution of tests.


2 Answers

For running a single test case in Eclipse (as per your last comment):

  • Go to Run (the green forward arrow button) -> Run Configurations.
  • Right click on JUnit, and select new.
  • Fill in your test case and test method (the Search button is really helpful here).
  • Click Run.
like image 163
Mureinik Avatar answered Sep 21 '22 08:09

Mureinik


It seems that now days (Eclispse 4.4.0) this can be done easily.

If you place the caret at the head of the definition of a test case method, issue the Run or Debug command (menu or F11 or Ctrl+F11), then the JUnit plugin runs only that test case.

(If you on the other hand place the caret in the body of a method then all the test cases in that class are run.)

Some more tips for running JUnit tests:

  • The Go to Previous/Next Member commands can be used to quickly move the caret to the head of the definition of a method with the keyboard. The default key bindings are Ctrl+Shift+Up/Down.
  • If the Run or Debug commands are issued when the Debug, JUnit or Console views are active, then Eclipse will run the last run configuration. This can be used to re-run your single test case without having to navigate back to the editor.
  • Running a particular run configuration can be done by navigating the Run menu: Alt + R, H, number key.
like image 21
Lii Avatar answered Sep 19 '22 08:09

Lii