有没有办法使用CLI(命令行界面)使用junit测试用例测试我的项目的jar文件?

问题描述 投票:0回答:2

我正在尝试创建一个帮助我的eclipse项目的项目,该项目被导出为jar文件以进行junit测试并提供结果,因此有没有办法测试项目的jar文件以使用CLI测试junit

java eclipse junit javac
2个回答
0
投票

你必须下载JUnit jar:https://github.com/junit-team/junit4/wiki/Download-and-Install

然后执行以下命令:

java -cp /path/to/my.jar:/path/to/junit.jar junit.textui.TestRunner com.mypackage.MyClassWithUnitTests

如果你有窗户:

java -cp /path/to/my.jar;/path/to/junit.jar junit.textui.TestRunner com.mypackage.MyClassWithUnitTests

0
投票

是的,绝对有可能。通常,您需要在构建的系统中构建“测试”目标,以便能够在需要时首先编译项目。

如果您使用的是ANT,则需要使用Ant JUnit task并确保将所有必需的依赖项传递给<classpath>元素。

<junit printsummary="yes" haltonfailure="yes">
    <classpath>
        <pathelement location="${build.tests}"/>
        <pathelement path="${java.class.path}"/>
    </classpath>

    <formatter type="plain"/>

    <batchtest fork="yes" todir="${reports.tests}">
        <fileset dir="${src.tests}">
            <include name="**/*Test*.java"/>
            <exclude name="**/AllTests.java"/>
        </fileset>
    </batchtest>
</junit>

如果您使用Maven,Surefire plugin应该在构建项目时或运行maven test时自动运行测试。

© www.soinside.com 2019 - 2024. All rights reserved.