我有一个netbeans项目,而且我已经熟练地进行了spock测试。当我右键单击项目并说测试时,它会运行一个名为
的任务test-with-groovy
但是当我运行ant test-with-groovy时,测试被编译但没有运行。我觉得必须通过netbeans ide添加一些内容,但我不知道是什么,半天的搜索没有结果。
任何人都可以帮助我吗?
以下是如何获得我得到的结果:
我在netbeans 8.0.2
中使用简单的main创建了一个简单的java项目package simpleantjava;
public class SimpleAntJava {
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Main Ran");
}
}
然后我创建了两个测试文件,一个是junit java文件:
package simpleantjava;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author vextorspace
*/
public class SimpleAntJavaTest {
public SimpleAntJavaTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of main method, of class SimpleAntJava.
*/
@Test
public void testMain() {
System.out.println("main");
String[] args = null;
SimpleAntJava.main(args);
// TODO review the generated test code and remove the default call to fail.
fail("Main JUnit Test is a prototype.");
}
}
和一个基于groovy的spock测试:
package simpleantjava
import org.junit.Test
import spock.lang.Specification
/**
*
* @author vextorspace
*/
class NewGroovyTest extends Specification{
@Test
def "Our groovy test should run"() {
expect:
true
}
@Test
def "Failing tests should fail"() {
expect:
false
}
}
如果我从netbeans运行测试,输出显示它正在运行:
ant -f /home/vextorspace/NetBeansProjects/SimpleAntJava -Dtest.binarytestincludes=**/*Test.class -Dtest.binaryincludes= -Dtest.binaryexcludes=**/*$* test-with-groovy
但如果我在ant命令行中运行它,它就不会运行任何测试(尽管它也没有给出错误)它会将两个测试文件编译成build / test / classes中的类文件。
如果我运行ant clean test,它会构建两个测试文件但不运行groovy测试,只运行java测试。
build-impl.xml包含测试的定义(我不会包含整个文件,但它是netbeans创建的标准文件:以下是我相信的相关部分:
<target if="${nb.junit.single}" name="-init-macrodef-junit-single" unless="${nb.junit.batch}">
<macrodef name="junit" uri="http://www.netbeans.org/ns/j2se-project/3">
<attribute default="${includes}" name="includes"/>
<attribute default="${excludes}" name="excludes"/>
<attribute default="**" name="testincludes"/>
<attribute default="" name="testmethods"/>
<element name="customize" optional="true"/>
<sequential>
<property name="junit.forkmode" value="perTest"/>
<junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" forkmode="${junit.forkmode}" showoutput="true" tempdir="${build.dir}">
<test methods="@{testmethods}" name="@{testincludes}" todir="${build.test.results.dir}"/>
<syspropertyset>
<propertyref prefix="test-sys-prop."/>
<mapper from="test-sys-prop.*" to="*" type="glob"/>
</syspropertyset>
<formatter type="brief" usefile="false"/>
<formatter type="xml"/>
<jvmarg value="-ea"/>
<customize/>
</junit>
</sequential>
</macrodef>
</target>
<target depends="-init-test-properties" if="${nb.junit.batch}" name="-init-macrodef-junit-batch" unless="${nb.junit.single}">
<macrodef name="junit" uri="http://www.netbeans.org/ns/j2se-project/3">
<attribute default="${includes}" name="includes"/>
<attribute default="${excludes}" name="excludes"/>
<attribute default="**" name="testincludes"/>
<attribute default="" name="testmethods"/>
<element name="customize" optional="true"/>
<sequential>
<property name="junit.forkmode" value="perTest"/>
<junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" forkmode="${junit.forkmode}" showoutput="true" tempdir="${build.dir}">
<batchtest todir="${build.test.results.dir}">
<fileset dir="${test.src.dir}" excludes="@{excludes},${excludes}" includes="@{includes}">
<filename name="@{testincludes}"/>
</fileset>
<fileset dir="${build.test.classes.dir}" excludes="@{excludes},${excludes},${test.binaryexcludes}" includes="${test.binaryincludes}">
<filename name="${test.binarytestincludes}"/>
</fileset>
</batchtest>
<syspropertyset>
<propertyref prefix="test-sys-prop."/>
<mapper from="test-sys-prop.*" to="*" type="glob"/>
</syspropertyset>
<formatter type="brief" usefile="false"/>
<formatter type="xml"/>
<jvmarg value="-ea"/>
<customize/>
</junit>
</sequential>
</macrodef>
</target>
<target depends="-init-macrodef-junit-init,-init-macrodef-junit-single, -init-macrodef-junit-batch" if="${junit.available}" name="-init-macrodef-junit"/>
...
<!--
=======================
TEST EXECUTION SECTION
=======================
-->
<target depends="init" if="have.tests" name="-pre-test-run">
<mkdir dir="${build.test.results.dir}"/>
</target>
<target depends="init,compile-test,-pre-test-run" if="have.tests" name="-do-test-run">
<j2seproject3:test includes="${includes}" testincludes="**/*Test.java"/>
</target>
<target depends="init,compile-test,-pre-test-run,-do-test-run" if="have.tests" name="-post-test-run">
<fail if="tests.failed" unless="ignore.failing.tests">Some tests failed; see details above.</fail>
</target>
<target depends="init" if="have.tests" name="test-report"/>
<target depends="init" if="netbeans.home+have.tests" name="-test-browse"/>
<target depends="init,compile-test,-pre-test-run,-do-test-run,test-report,-post-test-run,-test-browse" description="Run unit tests." name="test"/>
<target depends="init" if="have.tests" name="-pre-test-run-single">
<mkdir dir="${build.test.results.dir}"/>
</target>
并且在groovy-build.xml中包含了带有groovy-ant的测试目标:
<target depends="init,compile-test,-pre-test-run" if="have.tests" name="-do-test-run-with-groovy">
<j2seproject3:test testincludes=""/>
</target>
<target depends="init,compile-test,-pre-test-run,-do-test-run-with-groovy" if="have.tests" name="-post-test-run-with-groovy">
<fail if="tests.failed" unless="ignore.failing.tests">Some tests failed; see details above.</fail>
</target>
<target depends="init,compile-test,-pre-test-run,-do-test-run-with-groovy,test-report,-post-test-run-with-groovy,-test-browse" description="Run unit tests." name="test-with-groovy"/>
<target depends="init,compile-test-single,-pre-test-run-single" if="have.tests" name="-do-test-run-single-groovy">
<fail unless="test.binarytestincludes">Must select some files in the IDE or set test.includes</fail>
<j2seproject3:test testincludes=""/>
</target>
我必须承认我不知道macrodef究竟在做什么。但我尝试将** / * Test.java改为** / * Test.groovy,** / * Test.class和** / 测试。无效。
有谁知道如何使这项工作?我不想抛弃整个构建过程,因为我实际上正在使用jogl插件和ant文件中的大量自定义内容工作6年前的遗留项目,所以我&#39;我想弄清楚如何使这项工作。
谢谢!
答案 0 :(得分:1)
NewGroovyTest
使用Spock
完全独立测试,与其他类没有任何关系。
为了包含在test / ** / * Test.groovy文件中定义的类,我将编译后的类(因为所有Groovy编译成Java)并将它们提供给JUnit。如前所述,默认行为是仅使用来自* Test.java文件的类。
此链接可以帮助您:Groovy Unit Tests
具体来说,添加以下内容:
<target name="-post-test-run-with-groovy">
<junit dir="${work.dir}" errorproperty="tests.failed"
failureproperty="tests.failed" fork="true"
showoutput="true">
<batchtest todir="${build.test.results.dir}">
<fileset dir="${build.test.classes.dir}">
<include name="**/*Test.class"/>
<exclude name="**/*$*"/>
</fileset>
</batchtest>
<classpath>
<path path="${run.test.classpath}"/>
</classpath>
<syspropertyset>
<propertyref prefix="test-sys-prop."/>
<mapper from="test-sys-prop.*" to="*" type="glob"/>
</syspropertyset>
<formatter type="brief" usefile="false"/>
<formatter type="xml"/>
<jvmarg line="${run.jvmargs}"/>
</junit>
<mkdir dir="${build.test.results.dir}/../report"/>
<mkdir dir="${build.test.results.dir}/../report/html"/>
<junitreport todir="${build.test.results.dir}/../report">
<fileset dir="${build.test.results.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${build.test.results.dir}/../report/html"/>
</junitreport>
到build.xml文件允许使用命令
运行测试ant -f ~/NetBeansProjects/jdesigner -Dtest.binarytestincludes=**/*Test.class -Dtest.binaryincludes= -Dtest.binaryexcludes=**/* test-with-groovy