如何 运行 testng.xml 使用命令提示符

How to run testng.xml using command prompt

我试图从命令提示符 运行 testng.xml 但无法找出以下错误:

错误:无法找到或加载主程序 class org.testng.TestNG

或者

错误:无法找到或加载主程序 class java。

同时使用两种不同的方法。

请提供一个突出的方法来做同样的事情。

路径在类 Unix 系统下使用 : 分隔,而不是 Windows:

中的 ;
java -cp ./src/lib/*:./bin org.testng.TestNG testng.xml

如果您在 Windows 下使用 bash,您应该使用 ;,任何其他地方使用 :

; 字符表示 Unix shell 语句的结束,因此您可能试图执行的是:

java -cp ./src/lib/*
./bin org.testng.TestNG testng.xml

谢谢大家,但我已经使用 Apache ant 解决了这个问题。

通过使用以下 - build.xml

<project name="name" basedir=".">
    <!-- ========== Initialize Properties =================================== -->
    <!-- set global properties for build -->
    <property name="basedir" value="." />
    <property name="lib" value="${basedir}/lib" />
    <property name="src" value="${basedir}/src" />
    <property name="bin" value="${basedir}/bin" />
    <property name="report-dir" value="${basedir}/Test-Report" />
    <property name="testng-report-dir" value="${report-dir}/TestNGreport" />

    <!-- ====== Set the classpath ====  -->
    <path id="classpath">
        <pathelement location="${bin}" />
        <fileset dir="${lib}">
            <include name="*.jar" />
        </fileset>
    </path>

    <!-- Delete directories  -->
<target name="delete-dir">
        <delete dir="${bin}" />
        <delete dir="${report-dir}" />
    </target>

    <!-- Creating directories -->
 <target name="create" depends="delete-dir">
        <mkdir dir="${bin}" />
        <mkdir dir="${report-dir}" />
    </target>

    <!-- Compile the java code from ${src} into ${bin} -->
    <target name="compile" depends="create">
        <javac srcdir="${src}" classpathref="classpath" includeAntRuntime="No" destdir="${bin}" />
        <echo> /* Compiled Directory Classes */ </echo>
    </target>

    <!-- Runs the file and generates Reportng report for TestNG-->
    <taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="classpath" />

    <target name="testng-execution" depends="compile">
        <mkdir dir="${testng-report-dir}" />
        <testng outputdir="${testng-report-dir}" classpathref="classpath" useDefaultListeners="true">
            <xmlfileset dir="${basedir}" includes="testng.xml" />
        </testng>
    </target>
</project>

然后在命令提示符下输入:ant testng-execution

我在windows上使用testNg,在windows命令提示符下执行,我使用命令:java -cp bin;libs/* org.testng.TestNG testng.xml

结果:测试运行成功

always on windows: 我使用git bash命令,命令没有执行成功

尝试通过 cmd 提示符 运行,如下所示:

1. Go to project folder
2. Then: `java -cp [projectpath]\lib\*;[projectpath]\target org.testng.TestNG testng.xml`