Jenkins不从文件输出Junit报告信息

Joh*_*don 9 jenkins karma-runner

问题

Jenkins没有收到junit格式的报告,导致报告没有列在项目的状态屏幕中.

细节

junit格式的报告数据由名为Karma-runner(以前称为Testacular)的测试框架生成.被忽略的文件创建于/target/surefire-reports- 创建surefire生成的报告的位置.报告数据看起来几乎与maven surefire插件生成的报告数据相同,只是它的父元素<testsuites>代替<testsuite>- <testsuite>是surefire生成的报告作为报告文件的父元素.这是来自业力生成的junit格式报告的片段,命名为TEST-karma.resultsTest.xml:

Junit格式的Karma生成的报告文件, TEST-karma.resultsTest.xml

<?xml version="1.0"?>
<testsuites>
  <testsuite name="PhantomJS 1.9 (Mac)" package="karma.tests" timestamp="2013-04-10T13:32:26" id="0" hostname="jgmbp.local" tests="16" errors="0" failures="0" time="0.069">
    <properties>
      <property name="browser.fullName" value="Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.0 Safari/534.34"/>
    </properties>
    <testcase name="successfully cancels a new member" time="0.006" classname="karma.tests PhantomJS 1.9 (Mac).Household Controller"/>
    <testcase name="should parse an existing re-eval create date, setting the data in scope" time="0.003" classname="karma.tests PhantomJS 1.9 (Mac).Re-Eval Controller"/>
    <system-out><![CDATA[
]]></system-out>
    <system-err/>
  </testsuite>
</testsuites>
Run Code Online (Sandbox Code Playgroud)

Karma测试在我的maven构建的测试阶段运行.我已经尝试创建只生成这个junit-report文件以及运行构建的项目,以便所有junit测试和karma测试生成报告文件.詹金斯总是会接受万无一失的测试,但绝不会进行业力测试.

感谢您的任何意见!

小智 9

即使在构建Maven项目时,你也可以在Jenkins中发布你的Karma测试结果,如果你用一点hack来欺骗Jenkins:在你运行Karma测试的Javascript模块中运行Surefire插件.

surefire插件在您的Javascript模块中找不到任何JUnit测试,但它会通知Jenkins Surefire测试结果可用.

下面是一个示例pom.xml,以及来自Gruntfile.js的片段,它运行Karma测试和JSHint代码分析,并使Jenkins发布测试结果和代码分析结果.

<?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>com.xxx.yyyy</groupId>
<artifactId>zzzzz</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Zzzzz</name>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <configuration>
                        <target>
                            <exec
                                dir="${basedir}"
                                executable="npm"
                                failonerror="true">
                                <arg value="install"/>
                            </exec>
                            <exec
                                dir="${basedir}"
                                executable="bower"
                                failonerror="true">
                                <arg value="install"/>
                            </exec>
                            <exec
                                    dir="${basedir}"
                                    executable="grunt"
                                    failonerror="true">
                                <arg value="--no-color"/>
                                <arg value="build"/>
                            </exec>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>

                <execution>
                    <id>jshint</id>
                    <phase>test</phase>
                    <configuration>
                        <target>
                            <exec
                                dir="${basedir}"
                                executable="grunt"
                                failonerror="false">
                                <arg value="--no-color"/>
                                <arg value="karma:run"/>
                            </exec>
                            <exec
                                dir="${basedir}"
                                executable="grunt"
                                failonerror="false">
                                <arg value="--no-color"/>
                                <arg value="jshint:jenkins"/>
                            </exec>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>

            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.4</version>
            <executions>
                <!-- This is a hack to get Jenkins to publish Karma test results when running a Maven project: we run 0 surefire tests, so Jenkins publishes the report of the Karma tests. -->
                <execution>
                    <id>dummySureFire</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.12</version>
            <executions>
                <!-- This is a hack to get Jenkins to publish JSHint results when running a Maven project: we run checkstyle, so Jenkins publishes the report of the JSHint run. -->
                <execution>
                    <id>dummyCheckStyle</id>
                    <phase>test</phase>
                    <goals>
                        <goal>checkstyle</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<reporting>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>2.12.4</version>
        </plugin>
    </plugins>
</reporting>
</project>
Run Code Online (Sandbox Code Playgroud)

在Gruntfile.js中,我们有以下内容.输出文件名必须以TEST-开头才能发布:

karma: {
        run: { // produces reports for Jenkins
            configFile: 'karma.conf.js',
            singleRun: true,
            reporters : ['junit', 'coverage'],
            junitReporter : {
                outputFile: 'target/surefire-reports/TEST-results.xml'
            },
        ...
Run Code Online (Sandbox Code Playgroud)

对于Gruntfile.js中的JSHint:

jshint: {
        all: [
            'Gruntfile.js',
            '<%= yeoman.app %>/scripts/{,*/}*.js'
        ],
        options: {
            jshintrc: '.jshintrc',
            reporter: require('jshint-stylish')
        },
        jenkins: {
            options: {
                reporter: 'checkstyle',
                reporterOutput: "target/checkstyle-result.xml"
            },
            src: [
                'Gruntfile.js',
                '<%= yeoman.app %>/scripts/{,*/}*.js'
            ]
        }
    }
Run Code Online (Sandbox Code Playgroud)

在Jenkins作业配置中,您只需检查"构建设置"部分中的"发布Checkstyle分析结果"框,以便让Jenkins发布JSHint结果.发布测试结果不需要额外的Jenkins作业配置.


Joh*_*don 3

感谢 amey 和 Wouter 的意见,终于有时间弄清楚了。

关键是我正在构建一个 Maven 项目,这不适用于显示业力测试。显示 karma 结果的唯一方法是为 Junit 测试添加构建后报告。由于某些原因,基于 Maven 的作业不提供添加 junit 报告的选项。

解决方案是创建一个自由形式的作业而不是 Maven 作业。您可以配置自由形式作业来构建 Maven 项目。只要您按照上面 amey 的帖子添加发布 junit 任务,一切就都很好。