Jenkins-如果测试失败,即使构建通过,也要查看任何失败图标

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jenkins-如果测试失败,即使构建通过,也要查看任何失败图标相关的知识,希望对你有一定的参考价值。

目标:我需要完成/成功的Maven构建周期,以便生成报告。但是如何在Jenkins中显示测试失败而不取消构建?

-步骤:

  1. MVN干净验证
  2. 试运行
  3. 请参见测试方法的通过或失败
  4. 构建成功,将生成报告。

-Wants:

  1. 在Jenkins中-尽管构建成功,但如果测试(TestNg)失败,则显示红色图标。

-为什么? :

  1. 我不想每天查看报告只是为了查看是否失败。
  2. 快速浏览Jenkins仪表板,看是否有故障。

这是我的上下文控制台输出。

10:51:16 
10:51:16 ===============================================
10:51:16     TEST FRAMEWORK DEMO 1 PASS 1 FAIL
10:51:16     Tests run: 2, Failures: 1, Skips: 0
10:51:16 ===============================================
10:51:16 
10:51:16 [ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 5.405 s <<< FAILURE! - in TestSuite
10:51:16 [ERROR] com.demo.runnertypes.DemoTest.runScenario  Time elapsed: 1.473 s  <<< FAILURE!
10:51:16 org.openqa.selenium.InvalidSelectorException: 
10:51:16 invalid selector: Unable to locate an element with the xpath expression //input[@id='password']invalidXpath because of the following error:
10:51:16 SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//input[@id='password']invalidXpath' is not a valid XPath expression.
10:51:16   (Session info: headless chrome=78.0.3904.97)
10:51:16 For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
10:51:16 Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T15:28:36.4Z'
10:51:16 System info: host: 'fmdat01ap01.unix.ctcwest.ctc', ip: '192.168.241.251', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-1062.4.1.el7.x86_64', java.version: '1.8.0_212'
10:51:16 Driver info: org.openqa.selenium.chrome.ChromeDriver
10:51:16 Capabilities [{networkConnectionEnabled=false, chrome={chromedriverVersion=77.0.3865.40 (f484704e052e0b556f8030b65b953dce96503217-refs/branch-heads/3865@{#442}), userDataDir=/tmp/.com.google.Chrome.kiacKj}, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, unhandledPromptBehavior=dismiss and notify, strictFileInteractability=false, platform=LINUX, proxy=Proxy(), goog:chromeOptions={debuggerAddress=localhost:33385}, acceptInsecureCerts=false, browserVersion=78.0.3904.97, browserName=chrome, javascriptEnabled=true, platformName=LINUX, setWindowRect=true}]
10:51:16 Session ID: d6c2fbc4cbbd341cf6ef05609cb1e7a0
10:51:16 *** Element info: {Using=xpath, value=//input[@id='password']invalidXpath}
10:51:16 
10:51:16 [INFO] 
10:51:16 [INFO] Results:
10:51:16 [INFO] 
10:51:16 [ERROR] Failures: 
10:51:16 [ERROR] com.demo.runnertypes.DemoTest.runScenario
10:51:16 [INFO]   Run 1: PASS
10:51:16 [ERROR]   Run 2: DemoTest.runScenario » InvalidSelector invalid selector: Unable to locate an e...
10:51:16 [INFO] 
10:51:16 [INFO] 
10:51:16 [ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
10:51:16 [INFO] 
10:51:16 [ERROR] There are test failures.
10:51:16 
10:51:16 Please refer to /home/fmdigi/data/jenkins/workspace/CucumberTest/target/surefire-reports for the individual test results.
10:51:16 Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
10:51:16 [INFO] 
10:51:16 [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ testframework ---
10:51:17 [INFO] Building jar: /home/fmdigi/data/jenkins/workspace/CucumberTest/target/testframework-1.0-SNAPSHOT.jar
10:51:17 [INFO] 
10:51:17 [INFO] --- cluecumber-report-plugin:2.3.1:reporting (report) @ testframework ---
10:51:17 [INFO] ------------------------------------------------------------------------
10:51:17 [INFO]  Cluecumber Report Maven Plugin, version 2.3.1
10:51:17 [INFO] ------------------------------------------------------------------------
10:51:17 [INFO] - source JSON report directory     : /home/fmdigi/data/jenkins/workspace/CucumberTest/target/cucumber-report
10:51:17 [INFO] - generated HTML report directory  : test-report/generated-cluecumber-report
10:51:17 [INFO] ------------------------------------------------------------------------
10:51:17 [INFO] - fail pending/undefined scenarios : false
10:51:17 [INFO] - expand before/after hooks        : true
10:51:17 [INFO] - expand step hooks                : false
10:51:17 [INFO] - expand doc strings               : false
10:51:17 [INFO] - page title                       : Cluecumber Report
10:51:17 [INFO] - colors (passed, failed, skipped) : #28a745, #dc3545, #ffc107
10:51:17 [INFO] ------------------------------------------------------------------------
10:51:17 [INFO] => Cluecumber Report: test-report/generated-cluecumber-report/index.html
10:51:17 [INFO] ------------------------------------------------------------------------
10:51:17 [INFO] BUILD SUCCESS
10:51:17 [INFO] ------------------------------------------------------------------------
10:51:17 [INFO] Total time: 10.711 s
10:51:17 [INFO] Finished at: 2019-11-19T10:51:47-07:00
10:51:18 [INFO] Final Memory: 28M/571M
10:51:18 [INFO] ------------------------------------------------------------------------
10:51:18 [htmlpublisher] Archiving HTML reports...
10:51:18 [htmlpublisher] Archiving at BUILD level /home/fmdigi/data/jenkins/workspace/CucumberTest/test-output/HtmlReport to /var/lib/jenkins/jobs/CucumberTest/builds/15/htmlreports/ExtentHtml_Report
10:51:18 [htmlpublisher] Archiving at BUILD level /home/fmdigi/data/jenkins/workspace/CucumberTest/test-report/generated-cluecumber-report to /var/lib/jenkins/jobs/CucumberTest/builds/15/htmlreports/Test_Report
10:51:18 Collecting metadata...
10:51:18 Metadata collection done.
10:51:18 Finished: SUCCESS

POM.xml构建阶段

   <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M4</version>
                    <configuration>
                        <suiteXmlFiles>
                            <suiteXmlFile>testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
                        <testFailureIgnore>true</testFailureIgnore>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <systemPropertyVariables>
                        <extent.reporter.html.start>true</extent.reporter.html.start>
                        <extent.reporter.html.out>test-output/HtmlReport/ExtentHtml.html</extent.reporter.html.out>
                    </systemPropertyVariables>
                </configuration>
            </plugin>

            <plugin>
                <groupId>com.trivago.rta</groupId>
                <artifactId>cluecumber-report-plugin</artifactId>
                <version>${cluecumber-report.version}</version>
                <executions>
                    <execution>
                        <id>report</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>reporting</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <sourceJsonReportDirectory>${project.build.directory}/cucumber-report</sourceJsonReportDirectory>
                    <generatedHtmlReportDirectory>test-report/generated-cluecumber-report</generatedHtmlReportDirectory>
                    <expandBeforeAfterHooks>true</expandBeforeAfterHooks>
                </configuration>
            </plugin>

            <plugin>
                <groupId>com.googlecode.maven-download-plugin</groupId>
                <artifactId>download-maven-plugin</artifactId>
                <version>${maven-download-plugin.version}</version>
                <executions>
                    <execution>
                        <id>install-chrome-driver-linux64</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>wget</goal>
                        </goals>
                        <configuration>
                            <url>
                                https://chromedriver.storage.googleapis.com/${chromedriver-dep.version}/chromedriver_linux64.zip
                            </url>
                            <unpack>true</unpack>
                            <outputDirectory>${basedir}/drivers/linux/64bit/</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>install-chrome-driver-windows</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>wget</goal>
                        </goals>
                        <configuration>
                            <url>
                                https://chromedriver.storage.googleapis.com/${chromedriver-dep.version}/chromedriver_win32.zip
                            </url>
                            <unpack>true</unpack>
                            <outputDirectory>${basedir}/drivers/windows/</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>install-chrome-driver-osx</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>wget</goal>
                        </goals>
                        <configuration>
                            <url>
                                https://chromedriver.storage.googleapis.com/${chromedriver-dep.version}/chromedriver_mac64.zip
                            </url>
                            <unpack>true</unpack>
                            <outputDirectory>${basedir}/drivers/osx/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
答案

我将所有Selenium测试放在一个单独的项目中,与其他代码分开。如果您的情况并非如此,则以下内容可能会破坏您的构建!

关闭Surefire插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${surefire.plugin.version}</version>
    <configuration>
        <skipTests>true</skipTests>
    </configuration>
</plugin>

并替换为Failsafe plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>${surefire.plugin.version}</version>
    <configuration>
        <testFailureIgnore>false</testFailureIgnore>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>integration-test</goal>
          <goal>verify</goal>
        </goals>
      </execution>
    </executions>
</plugin>

注意,版本是故意相同

来自Failsafe site

故障安全插件旨在运行集成测试,而Surefire插件旨在运行单元测试。选择名称(failsafe)既是因为它是surefire的同义词,又因为它表示失败时会以安全的方式进行。

这意味着在集成测试失败时,构建将继续,并将运行您在<phase>post-integration-test</phase>中正确定义的报告。

为了使Failsafe进行测试,您可以将*Test的所有内容重命名为*IT,也可以将Failsafe插件配置为选择测试,如here所述。请注意,故障安全的几乎所有配置都与Surefire相同。您可能还需要阅读TestNG configuration section

您仍将像以前一样运行所有内容,并带有:mvn clean verify

詹金斯所做的

一个秘密事情是,它使用testFailureIgnore flag覆盖了如何处理集成测试失败。 Jenkins会覆盖默认行为,并且不会将构建标记为失败(红色图标)。因此,对于詹金斯,您必须将其重新设置。可以轻松地在pom中使用配置<testFailureIgnore>false</testFailureIgnore>或在Jenkins中将MAVEN_OPTS设置为-Dmaven.test.failure.ignore=false

另一答案

使用JUnit插件-通过Publish JUnit test result report-根据我想要的配置,将构建图标变为黄色/红色。

以上是关于Jenkins-如果测试失败,即使构建通过,也要查看任何失败图标的主要内容,如果未能解决你的问题,请参考以下文章

如何设置 SBT 构建以在 Jenkins 测试失败时返回零退出代码?

即使某些测试失败,也要继续测试 Maven 多模块

即使加载失败,也要在 Spring 测试中缓存上下文

即使 ROBOCOPY 命令在 BAT 脚本中成功执行,JENKINS 作业也会失败

使用 Jenkins 从 Karma.js 获得百分比覆盖率

设置Jenkins使用Karma和Eslint进行单元测试和Lint检查