为什么不运行测试与 - 常​​规从蚁命令行NetBeans项目运行测试? [英] Why doesn't running test-with-groovy from ant command line in a netbeans project run the tests?

查看:324
本文介绍了为什么不运行测试与 - 常​​规从蚁命令行NetBeans项目运行测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NetBeans项目,我已经得到了在这时髦,以及石破天惊测试。当我在右击项目,并说测试,它运行一个称为任务

I've got a netbeans project, and I've got groovy in it as well for spock testing. When I right-click on the project and say test, it runs a task called

test-with-groovy

但是当我运行ant测试与 - 常​​规测试进行编译,但无法运行。我觉得自己喜欢的事必须由NetBeans IDE中加入,但我不知道和搜索的半天也没有捕结果。

but when I run ant test-with-groovy the tests are compiled but not run. I feel like something must be added by the netbeans ide but I have no idea what and half a day of searching has netted no results.

谁能帮我?

下面是你如何能得到我得到的结果:

Here is how you can get the results I am getting:

我在NetBeans中创建一个简单的Java项目用一个简单的主8.0.2

I created a simple java project with a simple main in netbeans 8.0.2

package simpleantjava;

public class SimpleAntJava {

    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("Main Ran");
    }

}

然后我创建了两个测试文件,一个一个JUnit的Java文件:

then I created two test files, one a junit java file:

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.");
    }

}

和一个基于常规测试斯波克:

and one a groovy based spock test:

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测试,输出表示,它正在运行:

if I run the tests from netbeans, the output says it is running:

ant -f /home/vextorspace/NetBeansProjects/SimpleAntJava -Dtest.binarytestincludes=**/*Test.class -Dtest.binaryincludes= -Dtest.binaryexcludes=**/*$* test-with-groovy

但如果我运行,在蚂蚁命令行不运行任何测试(虽然它没有给出任何错误),它在构建/测试/编译类​​两个测试文件到类文件。

but if I run that in the ant command line it doesn't run any tests (though it gives no errors either) It does compile both test files into class files in build/test/classes.

如果我运行ant干净的测试它建立两个测试文件,但不运行常规测试,只有Java测试。

If I run ant clean test it builds both test files but does not run the groovy test, only the java test.

在build-impl.xml中对测试的定义(我将不包括整个文件,但它是通过NetBeans中创建的标准之一:这里有相关的部分,我相信:

the build-impl.xml has the definitions for test (I won't include the whole file, but it is the standard one created by netbeans: here are the relevant sections I believe:

<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>

和测试与 - 常​​规Ant目标是在包括Groovy的build.xml文件:

and the test-with-groovy ant target is in the included 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>

我必须承认,我不知道是什么的macrodefs真的这样做。但我试图改变** / * Test.java到** / * Test.groovy,** / *的Test.class和** /的测试。的无济于事。

有谁知道如何使这项工作?我不想扔掉整个构建过程,因为我实际上是在一个传统项目,是6岁的JOGL插件和Ant文件不少的东西,工作,所以我想弄清楚如何为使这项工作。

Does anyone know how to make this work? I don't want to throw out the whole build process because I'm actually working on a legacy project that is 6 years old with the jogl plugin and a lot of custom stuff in the ant file so I'd like to figure out how to make this work.

感谢您!

推荐答案

NewGroovyTest 是完全独立的测试用斯波克和其他类没有关系。

NewGroovyTest is completely independent test using Spock and no relationship to other classes.

为了包括/测试中定义的类** / * Test.groovy文件我把编译的类(因为所有的Groovy编译成Java),并是其中送入JUnit的。正如前面所说的默认行为是到来自* Test.java文件中只使用类。

In order to include classes that are defined in test/**/*Test.groovy files I took the compiled classes (as all Groovy compiles into Java) and am feeding them into JUnit. As said earlier the default behavior is to only use classes that come from *Test.java files.

这个链接将帮助您:的Groovy单元测试

具体而言,加入以下内容:

Specifically, adding the following:

<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文件允许测试用命令来运行

to the build.xml file allows the tests to be run with the command

ant -f ~/NetBeansProjects/jdesigner -Dtest.binarytestincludes=**/*Test.class -Dtest.binaryincludes= -Dtest.binaryexcludes=**/* test-with-groovy

这篇关于为什么不运行测试与 - 常​​规从蚁命令行NetBeans项目运行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆