Java 如何在 Eclipse 中运行单个 JUnit 测试方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19920830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:31:40  来源:igfitidea点击:

How to run a single JUnit test method in Eclipse?

javaeclipsejunit

提问by AussieMoss

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

在带有多个 @Test 注释的 JUnit 测试用例中,如何有选择地运行测试?

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.

我试图只突出显示一个 @Test 方法并尝试运行它,但它不能那样工作。

采纳答案by Mureinik

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

在 Eclipse 中运行单个测试用例(根据您的最后一条评论):

  • 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.
  • 转到运行(绿色向前箭头按钮)-> 运行配置。
  • 右键单击 JUnit,然后选择 new。
  • 填写您的测试用例和测试方法(搜索按钮在这里非常有用)。
  • 单击运行。

回答by amphibient

I think what you want to do is label your tests as belonging to different JUnit Categoriesand then run just those from one or more categories and not all tests, using the @RunWithand @Categoriesannotations. It's how I've done it in the past. In your case, you may have a category with just one test.

我认为您想要做的是将您的测试标记为属于不同的JUnit 类别,然后使用@RunWith@Categories注释仅运行来自一个或多个类别的测试而不是所有测试。这就是我过去所做的。在您的情况下,您可能有一个只有一个测试的类别。

See examples:

请参阅示例:

Running benchmark methods in a JUnit test class

在 JUnit 测试类中运行基准测试方法

How to run all tests belonging to a certain Category in JUnit 4

如何在 JUnit 4 中运行属于某个类别的所有测试

回答by Lii

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

似乎现在(Eclispse 4.4.0)这可以轻松完成。

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

如果将插入符号放在测试用例方法定义的开头,发出RunDebug命令(菜单F11Ctrl+ F11),则 JUnit 插件仅运行该测试用例。

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

(另一方面,如果您将插入符号放在方法的主体中,则该类中的所有测试用例都会运行。)

Some more tips for running JUnit tests:

运行 JUnit 测试的更多技巧:

  • The Go to Previous/Next Membercommands 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 Runor Debugcommands are issued when the Debug, JUnitor Consoleviews 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 Runmenu: Alt + R, H, number key.
  • 转到上一页/下一页Member命令可以用来插入符号快速移动到与键盘的方法定义的头部。默认键绑定是Ctrl+ Shift+ Up/ Down
  • 如果在DebugJUnitConsole视图处于活动状态时发出RunDebug命令,则 Eclipse 将运行上次运行配置。这可用于重新运行您的单个测试用例,而无需导航回编辑器。
  • 可以通过导航运行菜单来运行特定的运行配置:Alt + RH、 数字键。

回答by Thomas Weller

Put the cursor on the method name, then press Alt+Shift+X, then T. This will run the test method as JUnit test.

将光标放在方法名称上,然后按Alt+ Shift+ X,然后按T。这将作为 JUnit 测试运行测试方法。

Note: do not put the cursor insode the method. This will run all methods in the class. It must be on the method declaration.

注意:不要将光标放在方法中。这将运行类中的所有方法。它必须在方法声明中。

Also note: @BeforeClasstest setups might not be executed in case you organize your tests in test suites.

另请注意:@BeforeClass如果您在测试套件中组织测试,则可能不会执行测试设置。