【问题标题】:Problem running TestNG project using CMD commands使用 CMD 命令运行 TestNG 项目时出现问题
【发布时间】:2022-01-07 01:15:48
【问题描述】:

我有一个问题我已经好几天没能解决了(我已经通过了所有的谷歌)

我尝试通过 CMD 运行我的整个项目 (TestNG) 测试。 当我使用mvn -X package 运行项目时,它会运行整个项目。 当我对我的 TestNG.xml 文件进行更改时,问题就开始了,因此它只运行一些测试。所以相同的命令 (mvn -X package) 不起作用,它会继续运行所有测试。 当我尝试使用来自 Internet 的命令将项目作为 TestNG 运行时 java org.testng.TestNG TestNG.xml(我发现的所有变化) 它总是向我显示相同的错误消息“错误:无法找到或加载主类 org.testng.TestNG”

我看到他们多次使用我没有的 LIB 和 BIN 文件夹,我只有 SRC(我通过 intellij 工作),我不知道需要放置什么或如何创建这些文件夹。

附言 我注意到很多人都遇到过这个问题,但是没有答案可以帮助我

pom.xml ` 4.0.0

<groupId>SimplyClub.Tests.SubTotalTest</groupId>
<artifactId>SimplyTest</artifactId>
<version>1.0</version>
<dependencies>
    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>4.4.3</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.testng/testng -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>simplyclub.test.infrastructur</groupId>
        <artifactId>SimplyClub</artifactId>
        <scope>system</scope>
        <version>1.0-SNAPSHOT</version>
        <systemPath>${user.home}/IdeaProjects/SimplyTestInfrastructure/out/artifacts/SimplyTestInfrastructure_jar/SimplyTestInfrastructure.jar</systemPath>
    </dependency>



    <dependency>
        <groupId>com.aventstack</groupId>
        <artifactId>extentreports</artifactId>
        <version>5.0.8</version>
    </dependency>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.6.2</version>
    </dependency>
    </dependencies>



<properties>

   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <maven.compiler.source>1.8</maven.compiler.source>
   <maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>TestNG.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <!--<source>1.8</source>-->
                <!--<target>1.8</target>-->
            </configuration>
        </plugin>
    </plugins>
</build>

`

TestNG.xml

<?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="SimplyTest">
<listeners>
    <listener class-name = "utilities.Listeners"></listener>
</listeners>

<test name="Sub Total">
    <classes>
        <class name="Tests.SubTotalTest"></class>
    </classes>
</test>
<test name="TranEnd">
    <classes>
        <class name="Tests.TranEndTest"></class>
    </classes>
</test>

更新: 这是我运行命令mvn package 后出现的内容 没有错误

更新2: 所以在安装完所有插件之后,就有了进展 但现在它给我带来了另一个我无法理解的错误 org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler- 在此输入代码plugin:3.1:testCompile

enter image description here

我还更新了 POM.xml

【问题讨论】:

  • “所以同样的命令(mvn -X package)不起作用”。 “不起作用”是什么意思?
  • 删除 &lt;version&gt;RELEASE&lt;/version&gt; 并用正确的版本号替换它...进一步删除 &lt;scope&gt;Tests.SubTotalTest&lt;/scope&gt; 并用 &lt;scope&gt;test&lt;/scope&gt; 替换它...也在另一个依赖项中删除 &lt;scope&gt;Tests.SubTotalTest&lt;/scope&gt;... . 完全错了...
  • 请通过mvn package在普通命令行上检查您的构建,应该已经警告可能有错误..
  • @tgdavies 命令,它不会运行它在那里注册的测试,它会运行项目中的所有测试
  • @khmarbaise 我做了你记录的更改,没有解决问题

标签: java maven testng


【解决方案1】:

这是我在您的 pom 文件中看到的问题列表,您需要修复它们

  • &lt;scope&gt;Tests.SubTotalTest&lt;/scope&gt;

正如其他人所指出的,这不是范围的有效值。请删除它(然后默认为compile,这意味着该依赖项将可用于src/main/javasrc/test/java 中的所有java 类)

  • &lt;version&gt;RELEASE&lt;/version&gt;

这无法解析为任何正确的版本。请将其替换为实际版本 7.4.0(截至今天的最新 TestNG 发布版本)或使用适当的 maven 变量(我认为您现在不需要它,因为它会是一个矫枉过正)

要按照套件 xml 文件中的定义执行测试,您需要相应地配置surefire插件。如果未完成,surefire 插件将始终运行它可以找到的所有测试类(默认情况下,它会查找文件名中包含Test(或)TestCase 的文件)。您可以在 surefire 插件文档中了解更多信息。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M5</version>
    <configuration>
        <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
        </suiteXmlFiles>
    </configuration>
</plugin>

为标签&lt;suiteXmlFile&gt; 添加testng.xml 文件的位置(项目根目录中的相对文件位置),然后surefire 插件将只运行套件文件中提到的测试。

http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html 的文档中阅读有关 maven surefire 插件的更多信息

【讨论】:

  • 首先感谢您的回复!我在回复中更新了 POM 文件我进行了更改,但它仍然不起作用我现在正在使用 &lt;suiteXmlFile&gt; 进行测试希望这对我有帮助
  • 我在你的 pom 文件中没有看到 surefire 插件条目。我这样说是因为您的屏幕截图仍然显示您使用的是 surefire 插件版本 2.12.4,但如果您在 &lt;build&gt;&lt;plugins&gt; 部分下的 pom 文件中添加了上述插件条目,我应该已经看到了 3.0.0-M5 版本。请交叉检查。
  • 我刚刚更新了问题,你可以看到我添加了它但是现在弹出另一个错误org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile
  • 听起来你有很多编译错误。请确保无论您需要什么 jar,您都不是通过 IDE 添加它们,而是它们都需要以 &lt;dependencies&gt; 的形式进入您的 pom 文件。您需要通过查看测试代码并查看正在使用的导入以及它们从哪里进入并添加适当的依赖项来修复编译错误。
  • 请摆脱对编译器插件和surefire插件的依赖(从&lt;dependencies&gt;标签中删除它们,因为你不需要它们)。它们只需要在&lt;build&gt;&lt;plugins&gt; 部分下可用。您没有构建自定义插件,因此您不需要它们。
猜你喜欢
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 2016-11-30
  • 2020-08-04
  • 1970-01-01
  • 2022-01-12
  • 2011-06-29
相关资源
最近更新 更多