在我的自定义 NetBeans 项目中,我可以结合使用 project.xml 文件中的操作和 build.xml 中的 Ant 目标,在 NetBeans UI 中运行单个 JUnit 测试。在将我的测试移植到 TestNG 时,我希望也能够使用 NetBeans UI 运行单个 TestNG 测试。不幸的是,这比预期的要困难。这是我的 Ant 目标:
<target name="testng-single" depends="compile-test" description="Run individual testng test" >
<testng failureproperty="test.failed" haltonfailure="yes" outputDir="src/testng/test-output" workingDir="." >
<classpath refid="classpath.test" />
<classpath refid="classpath.groovy" />
<classpath location="${build}"/>
<classpath location="src/testng"/>
<classfileset dir="src/testng" includesfile="${test.class}" />
</testng>
<fail message="Tests failed!" if="test.failed"/>
</target>
这是我的行动:
<action name="test.single">
<script>build.xml</script>
<target>testng-single</target>
<context>
<property>test.class</property>
<folder>src/testng</folder>
<pattern>\.java$</pattern>
<format>java-name</format>
<arity>
<one-file-only/>
</arity>
</context>
</action>
我可以右键单击测试文件并选择测试文件,但是当它运行时,它找不到该类。我看到了以下错误:
Includesfile D:\javamarket\ftharness\com.javamarket.testng.donothing.DoNothingTest not found.
Ftharness 是我的项目的顶级目录,src/testng 是该目录下面的目录,其中包含 TestNG 测试。我尝试过各种改变但没有成功。有人可以帮忙吗?
最佳答案
如果这可以帮助任何想要解决相同问题的人:我按如下方式解决了这个问题。这不是很好,但似乎有效。
本质上,我使用 TestNG 的功能将测试定义作为 XML 文件使用。我沿着这些思路创建了一个 ant 目标:
<target name="testng-single" depends="compile-test" description="Run individual testng test" >
<copy file="${tst.dir}/TestSingle.xml" overwrite="true" tofile="${tst.dir}/TempTestSingle.xml" >
<filterset>
<filter token="CLASS" value="${test.class}"/>
</filterset>
</copy>
<testng failureproperty="test.failed" haltonfailure="yes" outputDir="src/testng/test-output" suitename="TestNG Suite" testname="TestNG Name" workingDir="." >
<classpath refid="classpath.test" />
<classpath refid="classpath.groovy" />
<classpath location="${build}"/>
<classpath location="src/testng"/>
<xmlfileset dir="${tst.dir}" includes="TempTestSingle.xml" />
</testng>
<fail message="Tests failed!" if="test.failed"/>
</target>
将以下内容添加到project.xml 文件中:
<action name="test.single">
<script>build.xml</script>
<target>testng-single</target>
<context>
<property>test.class</property>
<folder>src/testng</folder>
<pattern>\.java$</pattern>
<format>java-name</format>
<arity>
<one-file-only/>
</arity>
</context>
</action>
并添加了一个 TestSingle.xml 文件,如下所示:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Single Method Suite">
<test name="Single Method Test">
<classes>
<class name="@CLASS@">
<methods>
<include name=".*" />
</methods>
</class>
</classes>
</test>
</suite>
通过这些更改,我现在可以右键单击我的 TestNG java 类之一并运行它。
希望这对某人有帮助!马丁
关于java - 在 NetBeans 8.1 中运行单个 TestNG 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38439307/