Running a single JUnit test in Eclipse

后端 未结 13 512
面向向阳花
面向向阳花 2020-11-27 15:05

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

相关标签:
13条回答
  • 2020-11-27 15:43

    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.

    0 讨论(0)
  • 2020-11-27 15:44

    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.

    0 讨论(0)
  • 2020-11-27 15:45

    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.

    0 讨论(0)
  • 2020-11-27 15:46

    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.

    0 讨论(0)
  • 2020-11-27 15:50

    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

    0 讨论(0)
  • 2020-11-27 15:53

    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.

    0 讨论(0)
提交回复
热议问题