即使在构建 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 作业配置。