【问题标题】:How can I set Maven TestNG project return code according to failed tests?如何根据失败的测试设置 Maven TestNG 项目返回码?
【发布时间】:2021-08-20 10:32:57
【问题描述】:

我的 selenium 测试套件由 Maven 和 TestNG 管理。
一切都很完美,在特定测试失败的情况下,我使用 Extent Reports 创建了完美的报告。
问题是 Maven 返回码始终为 0,无论是所有测试都通过还是多个测试失败,而我们需要在 Maven 返回码中指示测试失败。
到目前为止,我找不到我该怎么做。
我的pom.xml 文件是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>MyProjectName</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>5.0.6</version>
        </dependency>
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>4.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>3.141.59</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>4.3.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.opencsv/opencsv -->
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>5.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-firefox-driver -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>3.141.59</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-remote-driver -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-remote-driver</artifactId>
            <version>3.141.59</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-edge-driver -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-edge-driver</artifactId>
            <version>3.141.59</version>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</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>-->
                        <!--suppress UnresolvedMavenProperty -->
                        <suiteXmlFile>${suite}.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

测试套件是这样的

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" thread-count="1" parallel="methods">
    <listeners>
        <listener class-name="utilities.ExtentListener" />
    </listeners>
    <test verbose="2" preserve-order="true" name="Chrome Sanity Tests">
        <parameter name="browser" value="Chrome"/>
        <groups>
            <run>
                <include name = "Sanity">
                </include>
            </run>
        </groups>
        <classes>
            <class name="MainRunner">
            </class>
        </classes>
    </test>
</suite>

【问题讨论】:

  • 看看这是否有帮助:stackoverflow.com/questions/14829845/…
  • @itronic1990 非常感谢,但在问这个问题之前我已经看过那篇帖子了。可能我不明白某些东西,但我看不到我在寻找什么
  • @AbhishekDhoundiyal 这会给我什么?

标签: java selenium maven testng maven-surefire-plugin


【解决方案1】:

&lt;configuration&gt;里面尝试添加

&lt;testFailureIgnore&gt;true&lt;/testFailureIgnore&gt;

在 maven-surefire-plugin 中。

将此设置为“true”以在测试期间忽略失败。不推荐使用它,但有时很方便。 默认值为:假。 用户属性是:maven.test.failure.ignore。

要执行的Maven命令:mvn clean compile test

完整代码:

<plugins>
    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
                <testFailureIgnore>true</testFailureIgnore>
                <suiteXmlFiles>
                    <suiteXmlFile>testNG.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
</plugins>

【讨论】:

  • 我会试试这个并告诉你。谢谢!
  • 好的,请告诉我。
  • 我没有忘记,别担心。只是有一些更紧迫的问题要解决。我也需要 devOps 帮助来验证这一点,而他这些天也很忙。我希望这周能测试一下。无论如何,你的支持已经在这里了 :)
  • 我还是没试过。为此,我必须与我们的 devOps 团队合作,但他们现在非常忙碌。这些天,我们有一些大而关键的项目要做。但我没有忘记这个问题,相信我。我会尽快更新你。
  • 好的,谢谢兄弟?
猜你喜欢
  • 2017-10-11
  • 2012-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-30
  • 2021-08-26
  • 1970-01-01
相关资源
最近更新 更多