2

我想结合 ant 执行 junit5 测试,特别是junitlauncher

这是我的项目结构:

在此处输入图像描述

这是我的构建文件:

<?xml version="1.0" encoding="UTF-8"?>
<project default="test" name="test">
    <property name="src.dir" value="${basedir}/src" />
    <property name="output.dir" value="${basedir}/build" />

    <!-- PATH DEFINITIONS -->
    <path id="junit5.classpath">
        <fileset dir="${basedir}/lib/" />
    </path>
    <path id="test.classpath">
        <path refid="junit5.classpath" />
        <fileset dir="${output.dir}">
            <include name="**/*.class" />
        </fileset>
    </path>

    <target name="clean">
        <delete dir="${output.dir}" />
    </target>

    <target name="compile" depends="clean">
        <mkdir dir="${output.dir}" />
        <mkdir dir="${output.dir}/report" />
        <javac srcdir="${src.dir}" destdir="${output.dir}" includeantruntime="false">
            <!-- test libs -->
            <classpath>
                <path refid="junit5.classpath" />
            </classpath>
        </javac>
    </target>

    <target name="test" depends="compile">
        <pathconvert property="classpathProp" refid="test.classpath"/>
        <echo>Classpath is ${classpathProp}</echo>
        <echo>Running junit tests...</echo>
        <junitlauncher>
            <classpath refid="test.classpath" />
            <test name="test.SomeClassTest" />
        </junitlauncher>
    </target>
</project>

当我尝试运行测试任务时,我可以看到类路径:

Classpath is 
/Users/some-username/Documents/workspace/test/lib/junit-jupiter-api-5.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-jupiter-engine-5.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-jupiter-migrationsupport-5.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-jupiter-params-5.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-platform-commons-1.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-platform-engine-1.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-platform-launcher-1.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-platform-runner-1.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-platform-suite-api-1.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-vintage-engine-5.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit.jar:
/Users/some-username/Documents/workspace/test/lib/org.apiguardian_1.0.0.v20180327-1502.jar:
/Users/some-username/Documents/workspace/test/lib/org.hamcrest.core_1.3.0.v20180420-1519.jar:
/Users/some-username/Documents/workspace/test/lib/org.opentest4j_1.0.0.v20180327-1502.jar:
/Users/some-username/Documents/workspace/test/build/test/SomeClass.class:
/Users/some-username/Documents/workspace/test/build/test/SomeClassTest.class:
/Users/some-username/Documents/workspace/test/build/test/TestSuite.class

其次是:

构建失败 /Users/some-username/Documents/workspace/test/build.xml:36: java.lang.NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory

您可以在此处找到 Eclipse 项目存档:https ://drive.google.com/file/d/161rYTopmqM3JMqv8kvM6VjJbG1sm-3k2/view?usp=sharing

我怎样才能让它工作?我错过了什么?

4

2 回答 2

0

我遇到了同样的问题,直接在 ant 调用中手动处理库,例如“ant -lib lib/”。

于 2018-09-07T14:57:19.070 回答
0

Junit5 测试需要不同的 ant 标签。检查https://ant.apache.org/manual/Tasks/junitlauncher.html

这是我的蚂蚁代码:

<target name="runTests" depends="compile,jar">      
    <junitlauncher printsummary="yes" haltonfailure="no">           
        <classpath refid="classpath.base" />  
        <classpath>
            <!-- the test classes themselves -->
            <pathelement location="${build}"/>
        </classpath>
        
        <!-- test by test - class -->           
        <!--<test name="TestPackage.ConcatTest" haltonfailure="no" /> -->   
        
        <!-- run all found tests in dir with classes -->           
        <testclasses outputdir="../report">
                        <fileset dir="${build}"/>
                        <listener type="legacy-brief" sendSysOut="true"/>
                        <listener type="legacy-xml" sendSysErr="true" 
sendSysOut="true"/>

         </testclasses>
    </junitlauncher>
</target>
于 2021-02-25T15:08:44.450 回答