9

问题

Jenkins 未获取 junit 格式的报告,导致报告未列在项目的状态屏幕中。

细节

junit 格式的报告数据由名为 Karma-runner(以前称为 Testacular)的测试框架生成。被忽略的文件是在与创建/target/surefire-reportssurefire 生成的报告的位置相同的位置创建的。报告数据看起来与 maven surefire 插件生成的数据几乎相同,只是它的父元素<testsuites>不是<testsuite>--<testsuite>是surefire 生成的报告作为报告文件的父元素所具有的内容。这是 karma 生成的 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>

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

感谢您的任何意见!

4

6 回答 6

9

即使在构建 Maven 项目时,你也可以在 Jenkins 中发布你的 Karma 测试结果,如果你用一点小技巧来欺骗 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>

在 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'
            },
        ...

对于 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'
            ]
        }
    }

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

于 2014-03-18T17:24:39.240 回答
3

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

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

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

于 2013-07-15T16:34:52.623 回答
2

虽然这个问题很老:

有一个解决方案也适用于 Jenkins 中的 Maven 项目。问题是记录测试的 Jenkins 记录器直接在 surefire:test 目标之后运行。因此,为了让您的测试能够被接受,您用于运行业力的任何插件都必须在test阶段之前运行,例如在process-test-classes阶段中(因为您不能在现有插件之前的同一阶段中放置一些东西)。

于 2015-03-24T18:03:59.157 回答
1

1)您是否输入了Jenkins应该找到Junit报告的报告路径?这可以在构建后操作 -发布 JUnit 测试结果报告中完成

2) 或者您可以使用性能插件来传递您的 Junit 结果。这又是一个后期构建操作。在该 Jenkins 作业的配置中选择该插件。

添加一个新报告 - Junit 并输入报告的路径target/surefire-reports/*.xml(注意 - 我删除了第一个斜杠,因为路径是相对于当前工作区的)。

于 2013-04-10T15:41:33.633 回答
1

我有同样的问题。测试结果包含在单个<testsuites/>标签中的事实不是问题。Jenkins 的 Maven 项目插件是问题所在。典型的 maven 项目使用这个,因为它使配置你的构建更容易,我们这样做了。我将 Karma 嵌入到我的 pom.xml 中,以便能够一次构建所有内容,使用maven-exec-plugin如下:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <id>jsunit</id>
                    <phase>test</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <successCodes>
                    <successCode>0</successCode>
                    <successCode>1</successCode><!-- on failing test, don't get a build error -->
                </successCodes>
                <executable>${basedir}/src/test/javascript/test.bat</executable>
                <workingDirectory>${basedir}/src/test/javascript</workingDirectory>
            </configuration>
        </plugin>

maven jenkins 插件确实拒绝接收生成的 XML。但是,如果您使用插件配置“基本”詹金斯作业而不是特定于 maven2 的作业,它将起作用。添加一个后期步骤,“发布 JUNIT 报告”并使用target/surefire-reports/*.xml. 作为正常的构建步骤,您仍然可以添加“执行 maven 任务”步骤。

它仍然不完美,因为使用本地构建mvn test似乎忽略了 XML(maven-surefire-plugin如果 JUnit 测试失败,只需生成 XML 并停止构建)。如果 javascript 测试失败,您可以使用中的成功代码停止构建,exec-maven-plugin但错误消息仍然不清楚...

于 2013-05-15T09:41:26.350 回答
0

我在这场比赛中迟到了,但我遇到了同样的问题,我偶然发现了这个有一年历史的帖子。

无论如何,在我看来,通过利用maven-karma-plugin. 这使我可以继续使用Maven Project作业,而不是将它们切换到Jenkins 中的Freestyle Project作业。

详细的解决方案发布在http://myshittycode.com/2014/10/23/jenkins-getting-karma-generated-test-results-to-appear-in-maven-project-job/以防有人被卡住同样的问题。

谢谢。

于 2014-10-27T19:34:21.283 回答