为什么在NetBeans项目中从Ant命令行运行test-with-groovy无法运行测试?

6

我有一个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 - Rao
我做了你描述的同样的事情。唯一的问题是,我在groovy junit class中只有“passing test”。这在使用Netbeans IDE时可以正常运行。但是当在commoand-prompt上运行相同的命令时,会出现如屏幕截图所示的错误。你也遇到了同样的问题吗? - Rao
看起来 NewGroovyTest 是完全独立的测试,使用了 Spock,并且与其他类没有关系。 - Rao
1
发现了这个链接(http://www.javaworld.com/article/2072350/running-individual-junit-unit-tests-from-command-line-using-netbeans-build-xml-file.html),它提到了如何在命令行中运行测试,但是针对的是 java 且比较老旧。另一个链接(http://pstehlik.com/2009/03/how-to-run-groovy-unit-tests-in-netbeans/)则介绍了如何在 NetBeans 中运行 Groovy 单元测试。或许对你有用。 - Rao
谢谢Rao,我现在正在运行我的测试,看起来你提供的Groovy测试链接起了作用!我也学到了更多关于Ant测试的知识。 - vextorspace
真的,那会值得一个答案。饶先生,你帮我找到了答案。 - vextorspace
1个回答

1

NewGroovyTest是完全独立的测试,使用Spock,与其他类没有关系。

为了包含在test/**/*Test.groovy文件中定义的类,我取出编译后的类(因为所有Groovy都编译成Java),并将它们馈入JUnit。正如前面所说,默认行为是仅使用来自*Test.java文件的类。

这个链接会对你有所帮助:Groovy单元测试

具体来说,添加以下内容:

<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

有趣的是,我刚刚发现我可以通过将,**/*Test.groovy添加到-do-test-run目标的testincludes中来修改我的nbproject/build-impl.xml。我以前尝试过这个方法,但可能搞砸了什么。这样做的额外好处是不会重复运行Java测试,但缺点是build-impl.xml是自动生成的。有人知道这个模板来自哪里吗? - vextorspace

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接