【发布时间】:2016-01-09 19:12:39
【问题描述】:
我有一个 netbeans 项目,而且我也有 groovy 用于 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 有 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. 无济于事。
有谁知道如何进行这项工作?我不想放弃整个构建过程,因为我实际上正在使用 jogl 插件和 ant 文件中的许多自定义内容处理一个已有 6 年历史的遗留项目,所以我想弄清楚如何使这项工作。
谢谢!
【问题讨论】:
-
您是否尝试过在调试模式下运行
ant以查看发生了什么?命令是 -ant -debug -
做了和你描述的一样的事情。唯一的问题是,
groovy junit class中只有passing test。这在使用Netbeans IDE时运行正常。当在commoand-prompt上运行相同的命令时,会出现screen shot 中所示的错误。那么你也面临同样的问题吗? -
看来
NewGroovyTest是完全独立 使用Spock进行的测试,与其他类没有关系。 -
感谢 Rao,我现在正在运行我的测试,看来您的 groovy 测试链接成功了!我学到了更多关于 ant 测试的知识。