Java 在Netbeans w/JUnit中测试方法而不是测试整个文件

Java 在Netbeans w/JUnit中测试方法而不是测试整个文件,java,testing,netbeans,junit,Java,Testing,Netbeans,Junit,我使用的是Netbeans 6.8,从IDE运行JUnit测试的最细粒度方法似乎是右键单击测试包下的类,然后单击测试文件 在Eclipse中,可以将范围缩小到在给定的测试工具中测试单个方法。如何在Netbeans中只测试线束中的一个单独测试?更新:Netbeans支持此功能(例如,请参阅Netbeans 6.8)。只需右键单击通过或失败的测试,然后单击“再次运行”或“调试”。或者在编辑器中单击鼠标右键,然后单击以运行/调试为中心的测试 旧的: 据我所知,这是不可能的。也许NetBeans的家伙们

我使用的是Netbeans 6.8,从IDE运行JUnit测试的最细粒度方法似乎是右键单击
测试包
下的类,然后单击
测试文件


在Eclipse中,可以将范围缩小到在给定的测试工具中测试单个方法。如何在Netbeans中只测试线束中的一个单独测试?

更新:Netbeans支持此功能(例如,请参阅Netbeans 6.8)。只需右键单击通过或失败的测试,然后单击“再次运行”或“调试”。或者在编辑器中单击鼠标右键,然后单击以运行/调试为中心的测试

旧的

据我所知,这是不可能的。也许NetBeans的家伙们一直认为所有的测试都应该通过一个单元。我知道工具不应该限制开发人员,但是如果一个方法花费太长时间,也许你应该考虑一个单独的集成测试?
也来看看。(顺便说一句:在maven项目中,运行单元测试是可能的…

在NetBeans 7.1中,打开单元测试文件,右键单击特定的单元测试并选择“以运行为中心的测试”。

NetBeans 7.1“以运行为中心的测试”不适用于我的构建,因此我使用“使用测试组”来标记我想要运行的测试

@Test(groups={"DEBUG"})
您可以通过在项目属性中的“调试测试文件”操作中添加
-Dgroups=DEBUG
来配置Netbeans以运行这些测试

注意:这些是使用maven/surefire运行的TestNG方法。

在我的项目中,“以运行为中心的测试”选项变灰,因为project.xml中没有名为“run.single.method”的操作

使其正常工作的变通方法:

1) 在project.xml文件中创建新的“run.single.method”操作(您可以复制“run.single” 行动)

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>
<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>

nbproject/ide-file-targets.xml
run.single.method
跑步课
测试/src
\.爪哇$
java名称
2) 在ide-file-targets.xml中创建新的“run.single.method”目标(您可以复制 “在src中运行所选文件”),并确保测试标记具有参数 “方法”使用属性“${method}”

ide-file-targets.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>
<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>

必须设置属性“run.class”
现在您可以使用鼠标右键菜单“运行聚焦测试”


当项目扫描时,“运行聚焦测试”选项也可能暂时变灰,如果有人想知道通过IntelliJ也可以进行聚焦测试,请右键单击特定测试并在IntelliJ中运行,可以右键单击测试方法名称并选择运行,也许有一种类似的方法可以在Netbeans中运行它?可以在Netbeans的Navigator视图中查看方法并右键单击它们,但据我所知,目前还没有上下文菜单项来测试该方法。不过还是要谢谢你,我还在挖掘:)NetBeans现在正好有这样一个功能;请看下面我的答案。我已经在Netbeans 7.4中使用了此功能,效果非常好!谢谢你回来更新:)没问题。作为一个netbeans用户,这也让我感到困扰。这对你有用吗?它似乎只适用于maven项目!?罗杰,你做过这个吗?突然之间,我的项目将不会运行集中测试——我不知道为什么——项目中有几个开发人员(包括编写JUnit测试)。但我所做的测试不会运行。我正在使用Netbeans 7.1.2是的,我正在使用@Test(groups={DEBUG})来运行单个测试。我将NetBeans设置为在运行“调试测试文件”时使用-Dgroups=DEBUG。它运行良好,我还添加了其他系统属性进行调试(例如browser=chrome)。注意,groups属性用于TestNG。JUnit有自己的分组注释。但是,是的,我的Netbeans并没有在开箱即用的情况下运行集中测试——我认为这是因为我在测试用例()中使用了内部类。在NB8中仍然如此。