安装插件

  1. 搜索安装 Allure Jenkins Plugin 插件
  2. Manage Jenkins -> Global Tool Configuration 的设置如下:
    Allure Commandline配置

当Jenkins检测没有安装allure时,自动安装

POM配置

  1. build -> plugins 编辑如下插件

    <!-- JUnit单元测试 -->
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <configuration>
         <!-- 默认报告目录为 target/surefire-reports/ -->
    <!--reportsDirectory>target/test-reports/</reportsDirectory>-->
         <argLine>
             -javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
         </argLine>
         <properties>
             <property>
                 <name>listener</name>
                 <value>io.qameta.allure.junit4.AllureJunit4</value>
             </property>
         </properties>
         <systemProperties>
             <property>
                 <name>allure.result.directory</name>
                 <value>${project.build.directory}/allure-results/</value>
             </property>
             <property>
                 <name>allure.link.issue.pattern</name>
                 <value>http://example.org/issue/{}</value>
             </property>
         </systemProperties>
     </configuration>
     <dependencies>
         <dependency>
             <groupId>org.aspectj</groupId>
             <artifactId>aspectjweaver</artifactId>
             <version>${aspectj.version}</version>
         </dependency>
     </dependencies>
    </plugin>
  2. dependencies 添加如下依赖

    <!-- Allure美化测试报告 -->
    <dependency>
     <groupId>io.qameta.allure</groupId>
     <artifactId>allure-junit4</artifactId>
     <version>2.9.0</version>
     <scope>test</scope>
    </dependency>

Notes: maven-surefire-plugin和allure-junit4需要添加对应的版本

Jenkinsfile

  1. Dashboard -> {项目名} -> {分支名} -> Pipeline Syntax,获取allure命令
    获取Allure命令
  2. 修改 Jenkinsfile 如下

    pipeline {
      agent any
      tools {
     maven "maven-3.6.3"
      }
    
      stages {
     stage ("UnitTest") {
       steps {
         configFileProvider([configFile(fileId: "mvn-setting-hhw", variable: "MVN_HHW")]) {
           sh "mvn -s ${MVN_HHW} test"
         }
       }
     }
      }
    
      post {
     always {
       script {
         allure includeProperties: false, jdk: '', results: [[path: 'target/allure-results/']]
       }
     }
    
      }
    }
    • sh "mvn -s ${MVN_HHW} test"以指定的maven配置文件运行 test 任务
    • post -> always -> script -> allure 在stages完成后总是运行allure操作

结果

  1. 构建结果如下
    带有Allure的构建结果
  2. Allure报告如下
    点击 Allure report 即可进入Allure报告
    Allure报告

Junit5

  1. pom.xml -> build -> plugins

    <!-- JUnit单元测试 -->
    <plugin>
     <artifactId>maven-surefire-plugin</artifactId>
     <configuration>
         <testFailureIgnore>false</testFailureIgnore>
         <argLine>
             -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
         </argLine>
         <systemProperties>
             <property>
                 <name>junit.jupiter.extensions.autodetection.enabled</name>
                 <value>true</value>
             </property>
         </systemProperties>
         <reportsDirectory>target/allure-reports/</reportsDirectory>
     </configuration>
     <dependencies>
         <dependency>
             <groupId>org.junit.platform</groupId>
             <artifactId>junit-platform-surefire-provider</artifactId>
             <version>1.2.0</version>
         </dependency>
         <dependency>
             <groupId>org.aspectj</groupId>
             <artifactId>aspectjweaver</artifactId>
             <version>${aspectj.version}</version>
         </dependency>
     </dependencies>
    </plugin>
    <plugin>
     <groupId>io.qameta.allure</groupId>
     <artifactId>allure-maven</artifactId>
     <version>2.10.0</version>
     <configuration>
         <reportVersion>2.4.1</reportVersion>
     </configuration>
    </plugin>
  2. pom.xml -> dependencys

    <!-- Allure美化测试报告 -->
    <dependency>
     <groupId>io.qameta.allure</groupId>
     <artifactId>allure-junit5</artifactId>
     <version>2.9.0</version>
     <scope>test</scope>
    </dependency>

chadLi
55 声望2 粉丝