【问题标题】:Why doesn't running test-with-groovy from ant command line in a netbeans project run the tests?为什么不在 netbeans 项目中从 ant 命令行运行 test-with-groovy 运行测试?
【发布时间】: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 进行的测试,与其他类没有关系。
  • 找到了this关于运行测试命令行的链接,但它是为java和年龄不大的。还有另一个指向Groovy 的链接。可能对你有用。
  • 感谢 Rao,我现在正在运行我的测试,看来您的 groovy 测试链接成功了!我学到了更多关于 ant 测试的知识。

标签: java netbeans groovy ant


【解决方案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

【讨论】:

  • 有趣的是,我刚刚发现我可以通过将 ,**/*Test.groovy 添加到 -do-test-run 目标的 testincludes 来修改我的 nbproject/build-impl.xml。我以前试过这个,但一定搞砸了。这样做的好处是不会两次运行 Java 测试,但缺点是 build-impl.xml 是自动生成的。有谁知道这个模板来自哪里?
猜你喜欢
  • 1970-01-01
  • 2011-04-10
  • 2013-05-08
  • 2021-01-16
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多