使用 JUnit 在 Netbeans 中测试方法而不是测试整个文件
我使用的是 Netbeans 6.8,从 IDE 运行 JUnit 测试的最细粒度方法似乎是右键单击 Test Packages
下的类,然后单击 Test File
在 Eclipse 中,可以缩小范围以测试给定测试工具中的单个方法。如何在 Netbeans 中仅测试一项测试?
I'm using Netbeans 6.8 and the finest-grained way to run my JUnit tests from the IDE appears to be to right-click a class under Test Packages
and click Test File
In Eclipse it's possible to narrow the scope to testing an individual method in a given test harness. How do I test only one individual test out of a harness in Netbeans?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“运行重点测试”选项在我的项目中呈灰色显示,因为 project.xml 中没有名为“run.single.method”的操作。
使其正常工作的解决方法:
1)在project.xml文件中创建新的“run.single.method”操作(您可以复制“run.single”
行动)。
project.xml 片段:
2)在 ide-file-targets.xml 中创建新的“run.single.method”目标(您可以复制
"run-selected-file-in-src") 并确保测试标签有一个参数
使用属性“${method}”的“方法”。
ide-file-targets.xml 片段:
现在您可以使用鼠标右键菜单“运行集中测试”。
当项目扫描时,“运行重点测试”选项也可能会暂时灰显
The option "Run Focused Test" was grayed-out on my project because there was no action named "run.single.method" in project.xml.
Workaround to get it working:
1) create new "run.single.method" action in project.xml file (you could copy "run.single"
action).
project.xml fragment:
2) create new "run.single.method" target in ide-file-targets.xml (you could copy
"run-selected-file-in-src") and make sure the test tag has a parameter
"methods" using the property "${method}".
ide-file-targets.xml fragment:
Now you can use the right mouse menu "Run focused test".
The option "Run focused test" might also be temporary be grayed-out when the project is scanning
在 NetBeans 7.1 中,打开单元测试文件,在特定单元测试中右键单击并选择“运行重点测试”。
In NetBeans 7.1, open the unit test file, right click within the specific unit test and select "Run Focused Test."
更新:NetBeans 支持此功能(例如,请参阅 NetBeans 6.8)。只需右键单击通过或失败的测试,然后单击“再次运行”或“调试”。或者右键单击编辑器,然后单击“运行/调试重点测试”。
旧:
据我所知,这是不可能的。也许 NetBeans 人员认为一个单元的所有测试始终都应该通过。我知道工具不应该限制开发人员,但是如果一种方法花费太长时间 - 也许您应该考虑单独的集成测试?
另请查看这篇文章。 (顺便说一句:在 Maven 项目中,可以运行单元测试...)
UPDATE: NetBeans supports this (e.g. see NetBeans 6.8). Just right click the passed or failed test and then click 'Run Again' or 'Debug'. Or right click in the editor and click Run/Debug focused test.
Old:
To my knowledge this is not possible. And maybe the NetBeans-guys had in mind that always all tests should pass for one unit. I know that tools should not limit the developers, but If one method takes too long - maybe you should consider a separate integration test?
Also take a look at this post. (BTW: in maven projects running tests of a unit is possible ...)
Netbeans 7.1“运行重点测试”不适用于我的构建,因此我使用测试组来标记我想要运行的测试。
您可以通过将
-Dgroups=DEBUG
添加到项目属性中的“调试测试文件”操作来配置 Netbeans 来运行这些测试。注意:这些是使用 maven/surefire 运行的 TestNG 方法。
Netbeans 7.1 "Run Focused Test" isn't working with my build, so I'm using use a test group to mark the tests I want to run.
You can configure Netbeans to run these tests by adding
-Dgroups=DEBUG
to your "Debug Test File" action in the project properties.Note: these are TestNG methods run with maven/surefire.
如果有人想知道通过 IntelliJ 也可以通过右键单击特定测试并运行进行焦点测试
Incase anyone is wondering focus testing is also possible via IntelliJ by right click on a specific test and run