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

时间:2019-02-11 20:14:40

标签: java eclipse junit javac

我正在尝试创建一个项目,该项目有助于将我的eclipse项目导出为jar文件,以执行junit测试并向我提供结果,因此可以使用以下方法来测试要使用junit进行测试的项目的jar文件CLI

2 个答案:

答案 0 :(得分:0)

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

然后执行以下命令:

\d+

如果您有Windows:

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

答案 1 :(得分: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时自动运行测试。