在 Eclipse 中运行单个 JUnit 测试
如果我有一个包含多个测试的测试套件,当我尝试从代码编辑器的上下文菜单或 JUnit 视图运行单个单元测试时,它似乎坚持始终运行整个套件,而不是单次测试。 有没有办法禁用更改此行为,以便我可以要求运行该测试,并且仅运行该测试。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
在包资源管理器中展开该类。 它应该向您展示所有方法。 右键单击要运行的一个方法,然后选择运行方式 -> 上下文菜单中的 JUnit(刚刚使用 Eclipse 3.4.1 进行了测试)。 还可以在 JUnit 结果视图中的单个条目上选择“运行”,以相同的方式重新运行测试。
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.
我知道的最快方法:
如果您想运行所有测试,可以跳过步骤 1 和 2。 4.
Fastest way I know of:
If you want to run all tests, you can skip steps 1 & 4.
在 Eclipse 3.5 中,您可以通过将测试运行器更改为 JUnit 3 来解决此问题。这仅适用于 JUnit 3 测试,不适用于 JUnit 4 测试。 您可以通过执行以下操作来更改测试运行程序:
它也可能适用于其他版本。
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:
It may work in other versions as well.
这在带有 JUnit 4.8.2 的 Eclipse Indigo 中是可能的。 在“Outline”窗格中右键单击要单独进行单元测试的方法,然后选择“Run As”->“Run As”。 JUnit 测试。
您甚至可以有选择地右键单击源代码编辑器中的测试方法名称,然后选择“运行方式”->“运行方式”。 Junit 测试。
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 selectRun As -> JUnit Test
.You can even selectively right click a Test method name in the source editor and select
Run As -> Junit Test
.我将通过包含一个高效的仅键盘方式来添加其他内容,该方式允许您调试单个单元测试(方法)。
将光标移动到方法名称
一旦光标位于方法名称上(或之前) it):
运行测试后,您可以通过执行以下操作返回到光标所在的位置:
Alt+Back
您几乎会得到REPL,就像行为方式:
Ctrl+Shift+Up 和 Alt+Shift+X -> T 和 Alt+Back
还可以快速设置断点:
Ctrl+Shift+B
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
Once your cursor is on the method name (or right before it):
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
不要使用 Strg+F11 而是使用热键 Alt+Shift+X -> T。
然后 Eclipse 将执行当前打开的文件作为 JUnit 测试。 绿色播放按钮将仅运行最后选择的运行配置。
对我来说,它与 Alt+Shift+X -> 配合得很好。 T。
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.
右键单击文件中测试方法名称的某处,然后选择“运行”-> “Junit 测试”。 我一直使用 Kepler 和 JUnit 4 来做这件事。
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。
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.
我在使用 Eclipse 3.4.1 和 JUnit 4 测试运行程序时遇到了其他人遇到的同样问题——无法运行单个测试。 但我在其他地方找到了解决问题的建议。 我的测试类仍在扩展 junit.framework.TestCase。 当我停止扩展 TestCase 时,一切正常,我能够使用 JUnit 4 测试运行程序运行单个 JUnit 测试。 当然,我需要 JUnit 4 注释对断言方法使用静态导入,但我已经这样做了——我只是没有删除 TestCase 继承。
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),大纲视图将列出一个文件的所有测试方法,您可能能够右键单击单个测试方法并将其作为 JUNit 测试执行。
希望能帮助到你。
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.
对我来说,它在 Eclipse 3.4.2 中也不起作用,尽管它在以前的版本中起作用(我尝试从包资源管理器运行单个方法并从 junit 视图运行单个 junit 测试)
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)
我同意关于 3.4.2 不再像以前版本那样工作的观点。 在此之前,您可以从 JUnit 视图中右键单击一组测试中的单个测试并仅执行该测试。 在最新版本中,它坚持始终运行所有这些。 当然,该视图确实运行您首先单击的测试,并将其余测试转储到名为“Unrooted Tests”的汇总中。 如果有人解决了这个问题,我想知道如何解决它。
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.
阅读此处的一些评论,您似乎可能有兴趣在更改代码时运行测试,而不会失去对正在处理的代码的关注。 有一个 eclipse 插件可以做到这一点。 请参阅无限。
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.
如果通过测试您甚至可能意味着单一测试方法,我喜欢使用键绑定来使用“运行所选成员的测试”。
当我位于测试方法内部时,只会运行该测试方法 - 如果我将光标放在类范围内,它将运行整个类。 我将默认绑定更改为像 Ctrl+r 这样简单的东西,因为我经常使用它。
请参阅:
(如果您没有看到该键条目,它可能来自 MoreUnit 插件 – 无论如何,这都很棒,你可能想要拥有它)
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:
(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)