java 使用 JUnit 在 Netbeans 中测试方法而不是测试整个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2193112/
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
Testing a method instead of testing a whole file in Netbeans w/ JUnit
提问by Maciek
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 Packagesand click Test File
我正在使用 Netbeans 6.8 并且从 IDE 运行我的 JUnit 测试的最细粒度的方法似乎是右键单击下面的一个类Test Packages并单击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?
在 Eclipse 中,可以缩小范围以测试给定测试工具中的单个方法。如何在 Netbeans 中使用一个工具只测试一个单独的测试?
采纳答案by Karussell
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.
更新:NetBeans 支持这一点(例如,参见 NetBeans 6.8)。只需右键单击通过或失败的测试,然后单击“再次运行”或“调试”。或者在编辑器中右键单击,然后单击运行/调试重点测试。
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?
据我所知,这是不可能的。并且也许 NetBeans 人已经想到,对于一个单元,所有测试总是应该通过。我知道工具不应该限制开发人员,但是如果一种方法花费的时间太长 - 也许您应该考虑单独的集成测试?
Also take a look at this post. (BTW: in maven projects running tests of a unit ispossible ...)
也看看这个帖子。(顺便说一句:在 maven 项目中运行一个单元的测试是可能的......)
回答by james.garriss
In NetBeans 7.1, open the unit test file, right click within the specific unit test and select "Run Focused Test."
在 NetBeans 7.1 中,打开单元测试文件,在特定单元测试中右键单击并选择“运行重点测试”。
回答by Roger Keays
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.
Netbeans 7.1“Run Focused Test”不适用于我的构建,因此我使用测试组来标记我想要运行的测试。
@Test(groups={"DEBUG"})
You can configure Netbeans to run these tests by adding -Dgroups=DEBUGto your "Debug Test File" action in the project properties.
您可以通过添加-Dgroups=DEBUG到项目属性中的“调试测试文件”操作来配置 Netbeans 以运行这些测试。
Note: these are TestNG methods run with maven/surefire.
注意:这些是使用 maven/surefire 运行的 TestNG 方法。
回答by R. Oosterholt
The option "Run Focused Test" was grayed-out on my project because there was no action named "run.single.method" in project.xml.
我的项目中的“Run Focused Test”选项是灰色的,因为在 project.xml 中没有名为“run.single.method”的操作。
Workaround to get it working:
使其工作的解决方法:
1) create new "run.single.method" action in project.xml file (you could copy "run.single" action).
1) 在 project.xml 文件中创建新的“run.single.method”动作(你可以复制“run.single”动作)。
project.xml fragment:
project.xml 片段:
<action name="run.single.method">
<script>nbproject/ide-file-targets.xml</script>
<target>run.single.method</target>
<context>
<property>run.class</property>
<folder>test/src</folder>
<pattern>\.java$</pattern>
<format>java-name</format>
<arity>
<one-file-only/>
</arity>
</context>
</action>
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}".
2)在ide-file-targets.xml中创建新的“run.single.method”目标(你可以复制“run-selected-file-in-src”)并确保测试标签有一个参数“methods”使用属性 "${method}"。
ide-file-targets.xml fragment:
ide-file-targets.xml 片段:
<target name="run.single.method" depends="compile">
<fail unless="run.class">Must set property 'run.class'</fail>
<junit maxmemory="256m" fork="true" haltonerror="true" haltonfailure="true" printsummary="on" jvm="${jdk.dir}/bin/java" showoutput="true">
<sysproperty key="build.basedir" value="${basedir}"/>
<!-- method attribute makes the selected method to be unit tested only -->
<test name="${run.class}" methods="${method}"/>
<formatter type="plain" usefile="false"/>
<classpath refid="CLASSPATH"/>
</junit>
</target>
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
项目扫描时,“运行重点测试”选项也可能暂时变灰
回答by wired00
Incase anyone is wondering focus testing is also possible via IntelliJ by right click on a specific test and run
如果有人想知道通过 IntelliJ 也可以通过右键单击特定测试并运行来进行焦点测试

