使用 Ant 运行 JUnit 测试

发布于 2024-10-09 02:07:22 字数 2595 浏览 1 评论 0原文

我正在尝试运行 JUnit 测试用例,但不断收到错误:

Test com.capscan.accentsWorld FAILED

报告已创建,但测试未运行。这是我的蚂蚁代码:

<!--this file was created by Eclipse Runnable JAR Export Wizard-->
<!--ANT 1.7 is required                                        -->
<target name="create_run_jar">

    <fail if="testsfailed" message="tests failed" /> 
    <delete dir="outputs/reports" />
    <mkdir dir="outputs/reports" />

    <junit printsummary="on" errorproperty="testsfailed">
        <classpath>
            <pathelement location="${build.com.capscan.accentsWorld}"/>
            <pathelement path="${java.class.path}"/>
        </classpath>

        <formatter type="plain"/>

        <test name="com.capscan.accentsWorld" haltonfailure="no" outfile="result">
            <formatter type="xml"/>
        </test>

        <batchtest fork="yes" todir="src/com/capscan/accentsWorld/" >
            <fileset dir="src/com/capscan/accentsWorld/">
                <include name="**/Test*.java" />
            </fileset>
        </batchtest>
        <formatter type="xml" />
    </junit>

    <junitreport todir="outputs/reports">
        <fileset dir="outputs/reports">
            <include name="TEST-*.xml" />
        </fileset>
        <report todir="outputs/reports" />
    </junitreport>

</target>

这是我的控制台上的输出消息:

Buildfile: C:\Documents and Settings\Ergun Polat\Desktop\Erguns Content\workspace\AccentsWorld\build.xml
create_run_jar:
   [delete] Deleting directory C:\Documents and Settings\Ergun Polat\Desktop\Erguns Content\workspace\AccentsWorld\outputs\reports
    [mkdir] Created dir: C:\Documents and Settings\Ergun Polat\Desktop\Erguns Content\workspace\AccentsWorld\outputs\reports
    [junit] Running com.capscan.accentsWorld
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] Test com.capscan.accentsWorld FAILED
[junitreport] Processing C:\Documents and Settings\Ergun Polat\Desktop\Erguns Content\workspace\AccentsWorld\outputs\reports\TESTS-TestSuites.xml to C:\DOCUME~1\ERGUNP~1\LOCALS~1\Temp\null1982495120
[junitreport] Loading stylesheet jar:file:/C:/ant/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 1047ms
[junitreport] Deleting: C:\DOCUME~1\ERGUNP~1\LOCALS~1\Temp\null1982495120
BUILD SUCCESSFUL
Total time: 2 seconds

I am trying to run my JUnit test cases, but I keep getting the error :

Test com.capscan.accentsWorld FAILED

The report is created, but the test aren't run. Here is my Ant code:

<!--this file was created by Eclipse Runnable JAR Export Wizard-->
<!--ANT 1.7 is required                                        -->
<target name="create_run_jar">

    <fail if="testsfailed" message="tests failed" /> 
    <delete dir="outputs/reports" />
    <mkdir dir="outputs/reports" />

    <junit printsummary="on" errorproperty="testsfailed">
        <classpath>
            <pathelement location="${build.com.capscan.accentsWorld}"/>
            <pathelement path="${java.class.path}"/>
        </classpath>

        <formatter type="plain"/>

        <test name="com.capscan.accentsWorld" haltonfailure="no" outfile="result">
            <formatter type="xml"/>
        </test>

        <batchtest fork="yes" todir="src/com/capscan/accentsWorld/" >
            <fileset dir="src/com/capscan/accentsWorld/">
                <include name="**/Test*.java" />
            </fileset>
        </batchtest>
        <formatter type="xml" />
    </junit>

    <junitreport todir="outputs/reports">
        <fileset dir="outputs/reports">
            <include name="TEST-*.xml" />
        </fileset>
        <report todir="outputs/reports" />
    </junitreport>

</target>

and here is the output message on my console:

Buildfile: C:\Documents and Settings\Ergun Polat\Desktop\Erguns Content\workspace\AccentsWorld\build.xml
create_run_jar:
   [delete] Deleting directory C:\Documents and Settings\Ergun Polat\Desktop\Erguns Content\workspace\AccentsWorld\outputs\reports
    [mkdir] Created dir: C:\Documents and Settings\Ergun Polat\Desktop\Erguns Content\workspace\AccentsWorld\outputs\reports
    [junit] Running com.capscan.accentsWorld
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] Test com.capscan.accentsWorld FAILED
[junitreport] Processing C:\Documents and Settings\Ergun Polat\Desktop\Erguns Content\workspace\AccentsWorld\outputs\reports\TESTS-TestSuites.xml to C:\DOCUME~1\ERGUNP~1\LOCALS~1\Temp\null1982495120
[junitreport] Loading stylesheet jar:file:/C:/ant/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 1047ms
[junitreport] Deleting: C:\DOCUME~1\ERGUNP~1\LOCALS~1\Temp\null1982495120
BUILD SUCCESSFUL
Total time: 2 seconds

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的巷 2024-10-16 02:07:22

我没有看到任何用于编译测试的 任务。你不需要先这样做吗?

我不确定您认为 java.class.path 来自哪里,但我会在 Ant build.xml 中显式设置它

这是我使用的通用 build.xml,包括测试任务。看看它是否可以帮助您:

<?xml version="1.0" encoding="UTF-8"?>
<project name="xslt-converter" basedir="." default="package">

    <property name="version" value="1.6"/>
    <property name="haltonfailure" value="no"/>

    <property name="out" value="out"/>

    <property name="production.src" value="src"/>
    <property name="production.lib" value="lib"/>
    <property name="production.resources" value="config"/>
    <property name="production.classes" value="${out}/production/${ant.project.name}"/>

    <property name="test.src" value="test"/>
    <property name="test.lib" value="lib"/>
    <property name="test.resources" value="config"/>
    <property name="test.classes" value="${out}/test/${ant.project.name}"/>

    <property name="exploded" value="out/exploded/${ant.project.name}"/>
    <property name="exploded.classes" value="${exploded}/WEB-INF/classes"/>
    <property name="exploded.lib" value="${exploded}/WEB-INF/lib"/>

    <property name="reports.out" value="${out}/reports"/>
    <property name="junit.out" value="${reports.out}/junit"/>
    <property name="testng.out" value="${reports.out}/testng"/>

    <path id="production.class.path">
        <pathelement location="${production.classes}"/>
        <pathelement location="${production.resources}"/>
        <fileset dir="${production.lib}">
            <include name="**/*.jar"/>
            <exclude name="**/junit*.jar"/>
            <exclude name="**/*test*.jar"/>
        </fileset>
    </path>

    <path id="test.class.path">                            
        <path refid="production.class.path"/>
        <pathelement location="${test.classes}"/>
        <pathelement location="${test.resources}"/>
        <fileset dir="${test.lib}">
            <include name="**/junit*.jar"/>
            <include name="**/*test*.jar"/>
        </fileset>
    </path>

    <path id="testng.class.path">
        <fileset dir="${test.lib}">
            <include name="**/testng*.jar"/>
        </fileset>
    </path>

    <available file="${out}" property="outputExists"/>

    <target name="clean" description="remove all generated artifacts" if="outputExists">
        <delete dir="${out}" includeEmptyDirs="true"/>
        <delete dir="${reports.out}" includeEmptyDirs="true"/>
    </target>

    <target name="create" description="create the output directories" unless="outputExists">
        <mkdir dir="${production.classes}"/>
        <mkdir dir="${test.classes}"/>
        <mkdir dir="${reports.out}"/>
        <mkdir dir="${junit.out}"/>
        <mkdir dir="${testng.out}"/>
        <mkdir dir="${exploded.classes}"/>
        <mkdir dir="${exploded.lib}"/>
    </target>

    <target name="compile" description="compile all .java source files" depends="create">
        <!-- Debug output
                <property name="production.class.path" refid="production.class.path"/>
                <echo message="${production.class.path}"/>
        -->
        <javac srcdir="src" destdir="${out}/production/${ant.project.name}" debug="on" source="${version}">
            <classpath refid="production.class.path"/>
            <include name="**/*.java"/>
            <exclude name="**/*Test.java"/>
        </javac>
        <javac srcdir="${test.src}" destdir="${out}/test/${ant.project.name}" debug="on" source="${version}">
            <classpath refid="test.class.path"/>
            <include name="**/*Test.java"/>
        </javac>
    </target>

    <target name="junit-test" description="run all junit tests" depends="compile">
        <!-- Debug output
                <property name="test.class.path" refid="test.class.path"/>
                <echo message="${test.class.path}"/>
        -->
        <junit printsummary="yes" haltonfailure="${haltonfailure}">
            <classpath refid="test.class.path"/>
            <formatter type="xml"/>
            <batchtest fork="yes" todir="${junit.out}">
                <fileset dir="${test.src}">
                    <include name="**/*Test.java"/>
                </fileset>
            </batchtest>
        </junit>
        <junitreport todir="${junit.out}">
            <fileset dir="${junit.out}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report todir="${junit.out}" format="frames"/>
        </junitreport>
    </target>

    <taskdef resource="testngtasks" classpathref="testng.class.path"/>
    <target name="testng-test" description="run all testng tests" depends="compile">
        <!-- Debug output
                <property name="test.class.path" refid="test.class.path"/>
                <echo message="${test.class.path}"/>
        -->
        <testng classpathref="test.class.path" outputDir="${testng.out}" haltOnFailure="${haltonfailure}" verbose="2" parallel="methods" threadcount="50">
            <classfileset dir="${out}/test/${ant.project.name}" includes="**/*.class"/>
        </testng>
    </target>

    <target name="exploded" description="create exploded deployment" depends="testng-test">
        <copy todir="${exploded.classes}">
            <fileset dir="${production.classes}"/>
        </copy>
        <copy todir="${exploded.lib}">
            <fileset dir="${production.lib}"/>
        </copy>
    </target>

    <target name="package" description="create package file" depends="exploded">
        <jar destfile="${out}/${ant.project.name}.jar" basedir="${production.classes}" includes="**/*.class"/>
    </target>

</project>

I don't see any <javac> task to compile the tests. Don't you have to do that first?

And I'm not sure where you think java.class.path is coming from, but I'd set it explicitly inside your Ant build.xml

Here's a generic build.xml that I use, including the testing task. Give it a look and see if it can help you:

<?xml version="1.0" encoding="UTF-8"?>
<project name="xslt-converter" basedir="." default="package">

    <property name="version" value="1.6"/>
    <property name="haltonfailure" value="no"/>

    <property name="out" value="out"/>

    <property name="production.src" value="src"/>
    <property name="production.lib" value="lib"/>
    <property name="production.resources" value="config"/>
    <property name="production.classes" value="${out}/production/${ant.project.name}"/>

    <property name="test.src" value="test"/>
    <property name="test.lib" value="lib"/>
    <property name="test.resources" value="config"/>
    <property name="test.classes" value="${out}/test/${ant.project.name}"/>

    <property name="exploded" value="out/exploded/${ant.project.name}"/>
    <property name="exploded.classes" value="${exploded}/WEB-INF/classes"/>
    <property name="exploded.lib" value="${exploded}/WEB-INF/lib"/>

    <property name="reports.out" value="${out}/reports"/>
    <property name="junit.out" value="${reports.out}/junit"/>
    <property name="testng.out" value="${reports.out}/testng"/>

    <path id="production.class.path">
        <pathelement location="${production.classes}"/>
        <pathelement location="${production.resources}"/>
        <fileset dir="${production.lib}">
            <include name="**/*.jar"/>
            <exclude name="**/junit*.jar"/>
            <exclude name="**/*test*.jar"/>
        </fileset>
    </path>

    <path id="test.class.path">                            
        <path refid="production.class.path"/>
        <pathelement location="${test.classes}"/>
        <pathelement location="${test.resources}"/>
        <fileset dir="${test.lib}">
            <include name="**/junit*.jar"/>
            <include name="**/*test*.jar"/>
        </fileset>
    </path>

    <path id="testng.class.path">
        <fileset dir="${test.lib}">
            <include name="**/testng*.jar"/>
        </fileset>
    </path>

    <available file="${out}" property="outputExists"/>

    <target name="clean" description="remove all generated artifacts" if="outputExists">
        <delete dir="${out}" includeEmptyDirs="true"/>
        <delete dir="${reports.out}" includeEmptyDirs="true"/>
    </target>

    <target name="create" description="create the output directories" unless="outputExists">
        <mkdir dir="${production.classes}"/>
        <mkdir dir="${test.classes}"/>
        <mkdir dir="${reports.out}"/>
        <mkdir dir="${junit.out}"/>
        <mkdir dir="${testng.out}"/>
        <mkdir dir="${exploded.classes}"/>
        <mkdir dir="${exploded.lib}"/>
    </target>

    <target name="compile" description="compile all .java source files" depends="create">
        <!-- Debug output
                <property name="production.class.path" refid="production.class.path"/>
                <echo message="${production.class.path}"/>
        -->
        <javac srcdir="src" destdir="${out}/production/${ant.project.name}" debug="on" source="${version}">
            <classpath refid="production.class.path"/>
            <include name="**/*.java"/>
            <exclude name="**/*Test.java"/>
        </javac>
        <javac srcdir="${test.src}" destdir="${out}/test/${ant.project.name}" debug="on" source="${version}">
            <classpath refid="test.class.path"/>
            <include name="**/*Test.java"/>
        </javac>
    </target>

    <target name="junit-test" description="run all junit tests" depends="compile">
        <!-- Debug output
                <property name="test.class.path" refid="test.class.path"/>
                <echo message="${test.class.path}"/>
        -->
        <junit printsummary="yes" haltonfailure="${haltonfailure}">
            <classpath refid="test.class.path"/>
            <formatter type="xml"/>
            <batchtest fork="yes" todir="${junit.out}">
                <fileset dir="${test.src}">
                    <include name="**/*Test.java"/>
                </fileset>
            </batchtest>
        </junit>
        <junitreport todir="${junit.out}">
            <fileset dir="${junit.out}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report todir="${junit.out}" format="frames"/>
        </junitreport>
    </target>

    <taskdef resource="testngtasks" classpathref="testng.class.path"/>
    <target name="testng-test" description="run all testng tests" depends="compile">
        <!-- Debug output
                <property name="test.class.path" refid="test.class.path"/>
                <echo message="${test.class.path}"/>
        -->
        <testng classpathref="test.class.path" outputDir="${testng.out}" haltOnFailure="${haltonfailure}" verbose="2" parallel="methods" threadcount="50">
            <classfileset dir="${out}/test/${ant.project.name}" includes="**/*.class"/>
        </testng>
    </target>

    <target name="exploded" description="create exploded deployment" depends="testng-test">
        <copy todir="${exploded.classes}">
            <fileset dir="${production.classes}"/>
        </copy>
        <copy todir="${exploded.lib}">
            <fileset dir="${production.lib}"/>
        </copy>
    </target>

    <target name="package" description="create package file" depends="exploded">
        <jar destfile="${out}/${ant.project.name}.jar" basedir="${production.classes}" includes="**/*.class"/>
    </target>

</project>
似最初 2024-10-16 02:07:22

尝试以详细模式运行 ant (ant -v)。您应该看到更多信息。

也许您缺少一些非常基本的东西,例如测试类中的 @Test 注释(碰巧我只有用 @Ignore 注释的方法),或者您类路径中有错误。向我们展示您的测试课程的片段。

Try to run the ant in a verbose mode (ant -v). You should see more information.

Maybe you are missing something that is really basic like the @Test annotation in your test class (it happend to me that I only had methods annotated with @Ignore), or you have an error in a classpath. Show us fragment of your test class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文