在Jenkins中使用Allure显示测试结果

allure 官网对自己的定义:

Allure Framework is a flexible lightweight multi-language test report tool that not only shows a very concise representation of what have been tested in a neat web report form …

1)首先在开发机器中安装 allure,

参考官方文档 https://docs.qameta.io/allure/#_installing_a_commandline 安装 allure。这里要注意的是如果是通过下载压缩包解压进行的安装,那么需要把 allure 连接到/usr/bin/allsure 这样的系统目录,否则 allure-maven 插件运行时找不到 allure command。如果是 mac 系统,因为权限管理的问题,可以链接到/usr/local/bin/allure。

2) 配置 pom 文件保证 allure 在本地可工作。

配置 maven 项目使用的是哪种测试框架来配置 pom 文件,可参考:https://docs.qameta.io/allure/#_java
运行以下命令确认本地环境可以正常生成 allure report。

mvn clean test allure:report
mvn allure:serve

3)在 jenkins 中安装 allure 插件并配置。

如图,安装 allure plugin
安装allure插件

如图,配置 allure tool,这里选择用到 allure 时自动下载。
配置allure工具

4)修改 Jenkinsfile 添加 allure 相关 step。

allure 相关配置大致如下:

post {
    always {
        junit testResults: "**/target/surefire-reports/*.xml"

        script {
          allure includeProperties: false, jdk: '', results: [[path: 'target/allure-results/']]
        }
    }

    success {
        archiveArtifacts 'target/*.jar'
    }
}

当把对 pom 和 jenkinsfile 的修改 push 到 GitLab 之后,因为之前已经配置了 pipeline 可被 gitlab 事件触发执行。顺利的话,就可在 Jenkins 中看到 allure 生成的报表了。

报表入口:
注意,生成的 allure-report.zip 并不能解压后直接通过浏览器查看,需要通过运行 allure open ./allure-report.zip 来查看。

可视化图表:

Reference:
https://docs.qameta.io/allure/
https://github.com/jenkinsci/allure-plugin