![](/img/trans.png)
[英]How to run a single junit test from an ant project created by Netbeans from the command line?
[英]Why doesn't running test-with-groovy from ant command line in a netbeans project run the tests?
我有一個netbeans項目,而且我已經熟悉了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.");
}
}
還有一個基於時髦的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>
並且groovy-build.xml中包含test-with-groovy ant目標:
<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>
我必須承認我不知道macrodef究竟在做什么。 但我嘗試將** / * Test.java更改為** / * Test.groovy,** / * Test.class和** / Test。 無濟於事。
有誰知道如何使這項工作? 我不想拋棄整個構建過程,因為我實際上正在使用jogl插件和ant文件中的大量自定義內容工作6年的遺留項目,所以我想弄清楚如何使這項工作。
謝謝!
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.