【问题标题】:How to run testng.xml using command prompt如何使用命令提示符运行 testng.xml
【发布时间】:2015-09-24 12:44:16
【问题描述】:

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

错误:无法找到或加载主类 org.testng.TestNG

或两者之一

错误:无法找到或加载主类 java。

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

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

【问题讨论】:

  • 请描述你到底做了什么,你的整个命令行是什么,你把包含测试的 jar 文件放在哪里,testng jar 文件在哪里等等你做了官方文档的哪一部分但遇到了问题?

标签: java selenium selenium-webdriver testng testng-dataprovider


【解决方案1】:

谢谢大家,但我已经使用 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

【讨论】:

    【解决方案2】:

    在类 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
    

    【讨论】:

    • 它给了我以下错误:线程“main”中的异常 org.testng.TestNGException:在 org.testng 的 org.testng.TestNG.checkConditions(TestNG.java:1170) 处未指定源目录。 TestNG.privateMain(TestNG.java:1010) at org.testng.TestNG.main(TestNG.java:997)
    • 我试过这个命令:java -cp ".;C:\TestNG\testng-6.8.jar" org.testng.TestNG testng.xml 但它给了我:[TestNG] [错误]在类路径中找不到类:test_test.Run
    • 在 src 和 bin 文件夹所在的项目中创建一个 bash 文件。当你使用 ;所以我假设你有窗户。您不需要 testng.jar 来传递 bash 文件。确保您的 testng.xml 文件具有相同的名称,不区分大小写,最后检查您的 java 文件的 testng.xml 文件类位置。应该是完美的
    【解决方案3】:

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

    结果:试运行成功

    一直在windows上:我用git bash命令,命令执行不成功

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-22
        • 2017-08-08
        • 2013-03-28
        • 2010-11-30
        • 2021-08-28
        相关资源
        最近更新 更多