Java 在NetBeans 8.1中运行单个TestNG测试

Java 在NetBeans 8.1中运行单个TestNG测试,java,netbeans,ant,testng,Java,Netbeans,Ant,Testng,在我的自定义NetBeans项目中,我可以在NetBeans UI中运行单个JUnit测试,使用project.xml文件中的操作和build.xml中的Ant目标的组合。在将测试移植到TestNG时,我希望能够使用NetBeans UI运行单个TestNG测试。不幸的是,事实证明这比预期的要困难。这是我的蚂蚁目标: <target name="testng-single" depends="compile-test" description="Run individual testng

在我的自定义NetBeans项目中,我可以在NetBeans UI中运行单个JUnit测试,使用project.xml文件中的操作和build.xml中的Ant目标的组合。在将测试移植到TestNG时,我希望能够使用NetBeans UI运行单个TestNG测试。不幸的是,事实证明这比预期的要困难。这是我的蚂蚁目标:

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

Ftharness是我的项目的顶级目录,src/testng是下面包含testng测试的目录。我尝试过各种改变,但都没有成功。有人能帮忙吗?

如果这能帮助任何想解决同样问题的人:我解决了这个问题,如下所示。这不太好,但似乎很管用

本质上,我使用TestNG的功能将测试定义作为XML文件使用。我创建了一个蚂蚁目标:

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

build.xml
testng单曲
测试类
src/testng
\.java$
java名称
并添加了一个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类并运行它

希望这对别人有帮助!马丁

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