我有一个Netbeans项目,其中包含Groovy以进行Spock测试。当我右键单击项目并选择测试时,它会运行一个名为的任务。
test-with-groovy
但是当我运行ant test-with-groovy时,测试被编译但没有运行。我感觉NetBeans IDE必须添加了一些东西,但我不知道具体是什么,我已经搜索了半天也没有结果。
有人能帮帮我吗?
以下是您可以复现我的结果的方法:
我在NetBeans 8.0.2中创建了一个简单的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中定义了test(我不会包含整个文件,但它是由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>
test-with-groovy的ant目标在包含的groovy-build.xml文件中:
<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>
我必须承认我不知道宏定义实际上在做什么。但是我尝试将 **/*Test.java 更改为 **/*Test.groovy、**/*Test.class 和 **/Test.,但都没有成功。
有人知道如何使这个工作吗?我不想放弃整个构建过程,因为我实际上正在处理一个6年历史的遗留项目,其中包括 jogl 插件和很多自定义内容在 ant 文件中,所以我想找出如何使其工作。
谢谢!
ant
命令来查看发生了什么吗?命令为 -ant -debug
。 - Raogroovy junit class
中只有“passing test”。这在使用Netbeans IDE
时可以正常运行。但是当在commoand-prompt
上运行相同的命令时,会出现如屏幕截图所示的错误。你也遇到了同样的问题吗? - RaoNewGroovyTest
是完全独立的测试,使用了Spock
,并且与其他类没有关系。 - Raojava
且比较老旧。另一个链接(http://pstehlik.com/2009/03/how-to-run-groovy-unit-tests-in-netbeans/)则介绍了如何在 NetBeans 中运行 Groovy 单元测试。或许对你有用。 - Rao