在 Eclipse 中运行单个 JUnit 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/646307/
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-09-10 18:39:30  来源:igfitidea点击:

Running a single JUnit test in Eclipse

eclipsejunit

提问by user14128

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.

如果我有一个包含多个测试的测试套件,当我尝试从代码编辑器的上下文菜单或 JUnit 视图运行单个单元测试时,它似乎坚持始终运行整个套件,而不是单项测试。有没有办法禁用更改此行为,以便我可以要求运行该测试。

回答by Volker Stolz

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.

在包浏览器中展开类。它应该向您展示所有方法。右键单击要运行的一种方法,然后从上下文菜单中选择 Run As -> JUnit(刚刚使用 Eclipse 3.4.1 进行了测试)。同样在 JUnit-results 视图中的单个条目上选择“运行”以重新运行测试以相同的方式工作。

回答by schnatterer

Fastest way I know of:

我所知道的最快方法:

  1. Press Ctrl+Shift+(moves cursor to current method declaration),
  2. press Alt+Shift+x(or dfor 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.
  1. Ctrl+ Shift+ (将光标移动到当前方法声明),
  2. Alt+ Shift+ x(或d调试),然后按t(热键为“运行JUnit测试”),
  3. 检查测试结果,
  4. Alt+返回到之前的代码行。

If you want to run all tests, you can skip steps 1 & 4.

如果要运行所有测试,可以跳过步骤 1 和 4。

回答by lucrussell

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:

在 Eclipse 3.5 中,您可以通过将测试运行程序更改为 JUnit 3 来解决这个问题。这仅适用于 JUnit 3 测试,而不适用于 JUnit 4 测试。您可以通过执行以下操作来更改测试运行程序:

  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'
  1. 右键单击大纲浏览器中的一种测试方法
  2. 选择运行方式 -> 运行配置
  3. 在“测试”选项卡上,选择“运行单个测试”
  4. 在 Test Runner 下拉菜单中,选择“JUnit 3”

It may work in other versions as well.

它也可能适用于其他版本。

回答by wulfgarpro

This is possible in Eclipse Indigo with JUnit 4.8.2. You right click the method you want to unit test individually in the Outlinepane, and select Run As -> JUnit Test.

这在带有 JUnit 4.8.2 的 Eclipse Indigo 中是可能的。在Outline窗格中右键单击要单独进行单元测试的方法,然后选择Run As -> JUnit Test

You can even selectively right click a Test method name in the source editor and select Run As -> Junit Test.

您甚至可以有选择地右键单击源代码编辑器中的测试方法名称并选择Run As -> Junit Test.

回答by Adam Gent

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+Upor
  • Ctrl+Shift+Downor
  • Ctrl+othen type the name of the method
  • Ctrl+ Shift+Up
  • Ctrl+ Shift+Down
  • Ctrl+o然后输入方法的名称

Once your cursor is on the method name (or right before it):

一旦您的光标位于方法名称上(或就在它之前):

  • Alt+Shift+D-> T(Debug)
  • Alt+Shift+X-> T(Run)
  • Alt+ Shift+ D-> T(调试)
  • Alt+ Shift+ X-> T(运行)

After you run the test you can go back to where your cursor was by doing:

运行测试后,您可以通过执行以下操作返回光标所在的位置:

Alt+Back

Alt+Back

You almost get REPL like behavior by:

您几乎可以通过以下方式获得类似 REPL 的行为:

Ctrl+Shift+Upand Alt+Shift+X-> Tand Alt+Back

Ctrl+ Shift+UpAlt+ Shift+ X->TAlt+Back

You can also quickly set a breakpoint:

您还可以快速设置断点:

Ctrl+Shift+B

Ctrl+ Shift+B

回答by guerda

Don't use Strg+F11but 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.

不要使用Strg+F11而是使用热键Alt+ Shift+ X-> T
然后 Eclipse 将执行当前打开的文件作为 JUnit 测试。绿色播放按钮将只运行最后选择的运行配置。

For me, it works well with Alt+Shift+X-> T.

对我来说,它适用于Alt+ Shift+ X-> T

回答by Kyle Dyer

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.

右键单击文件中测试方法名称的某处,然后选择“运行”->“Junit 测试”。我一直在用 Kepler 和 JUnit 4 做这件事。

回答by ghirschhorn

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.

我在其他人使用 Eclipse 3.4.1 和 JUnit 4 测试运行器时遇到了同样的问题——无法运行单个测试。但是我在其他地方找到了解决问题的建议。我的测试类仍在扩展 junit.framework.TestCase。当我停止扩展 TestCase 时,一切正常,我能够使用 JUnit 4 测试运行器运行单个 JUnit 测试。当然,我需要 JUnit 4 注释对 assert 方法使用静态导入,但我已经这样做了——我只是没有删除 TestCase 继承。

回答by Tom

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.

如果在多个文件中有多个测试,并且只想运行特定文件中的测试,则可以右键单击该文件,然后选择 run as -> junit test。现在,如果您只想运行一个包含在包含多个测试的文件中的测试,我的猜测是(我这里没有安装 eclipse)大纲视图将列出一个文件的所有测试方法,您可能能够右键单击单个测试方法并将其作为 JUNit 测试执行。

Hope it helps.

希望能帮助到你。

回答by danilo

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

只运行一种测试方法。
在(包资源管理器或项目资源管理器)中展开类。它应该向您展示所有方法。右键单击要运行的一种方法,然后从上下文菜单中选择 Run As -> JUnit。
在此处输入图片说明