利用Jenkins pipeline配置测试工具(下)

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介: 利用Jenkins pipeline配置测试工具(下)

3. 配置参数

打开./conf/sonar.properties,进行如下配置。

sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar
sonar.jdbc.username=sa
sonar.jdbc.password=123456
sonar.login=admin
sonar.password=admin


其中:

  • sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar # sonar为数据库名;
  • sonar.jdbc.username=sa      #SQLServer的登录名;
  • sonar.jdbc.password=123456  #SQLServer的登录密码;
  • sonar.login=admin          #sonar web的登录名;
  • sonar.password=admin       #sonar web的登录密码。
  • 修改pom.xml文件


<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <maven.compiler.source>11</maven.compiler.source>
   <maven.compiler.target>11</maven.compiler.target>
  </properties>
<build>
   </plugins>
       <plugin>
           <groupId>org.sonarsource.scanner.maven</groupId>
           <artifactId>sonar-maven-plugin</artifactId>
             <version>3.5.0.1254</version>
         </plugin>
   </plugins>
</build>
<maven.compiler.source>11</maven.compiler.source>和<maven.compiler.target>11</maven.compiler.target>表示使用的是JDK 11。


配置完毕在项目根目录处运行下面命令,手工测试一下。

C:\Users\xiang\.jenkins\workspace\pmd>mvn cleanverify sonar:sonar

SonarQube的“项目”菜单内显示图17界面。

image.png

17 SonarQube运行结果页面


请确认图17右上方运行时间是否正确。


2.5.3 下载Jenkins SonarQube Scanner插件

进入Jenkins插件中心,安装SonarQube Scanner插件,如图18所示。

image.png

18 安装Jenkins SonarQube Scanner插件


2.5.4 设置SonarQube中文界面

启动登录SQL Server2014后,再启动SonarQube打开浏览器,在地址栏中输入http://:9000(其中SonarQube所在的IP地址),进入SonarQubeWeb页面,用./conf/sonar.properties设立的账号登录系统。在插件管理中心(“admin->Application market”)中搜索“Chinese PackLOCALIZATION”,如图19所示,最后选择后重新启动SonarQube服务,界面变为中文。

image.png

19 SonarQube安装中文插件


2.5.5 设置SonarQube安全权限

选择菜单“配置->权限->权限->enable Force userauthentication”,如图20所示。

image.png

20 enableForce user authentication


然后选择菜单“配置->权限->用户->令牌”,设置用户令牌,如图21所示。

image.png

21 设置用户令牌


请务必记下这里产生的令牌,以后将无法显示获得。(由于截图存在先后循序,图21显示与后面不一致,以后面为准)。

建立完毕,配置完毕在项目根目录处运行下面命令,手工测试一下。

C:\Users\xiang\.jenkins\workspace\pmd>mvnclean verify sonar:sonar -Dsonar.login=6cdf6f3176211d67dbf469712168c005c48f8c4d-Dsonar.host.url=http://127.0.0.1:9000


其参数含义为:

  • -Dsonar.login:为产生的令牌;
  • -Dsonar.host.url:为WEB URL地址。
  • 配置SonarQubeScanner插件

Jenkins的“全局属性”配置SonarQube Scanner插件,如图22所示。

image.png

22 安装Jenkins Jacoco插件


Serverauthentication token建立上一节设置的令牌,如图23所示。

image.png

23 建立令牌


上图中Secret中输入上一节产生的令牌。


2.6.7设置Webhooks

进入SonarQube,选择菜单“配置->配置->网络调用( Webhooks)- >新建”,如图24所示。

image.png

24 设置Webhooks


其中:

  • 名称:任意;
  • URLJenkins URL/sonarqube-webhook/

设置Webhooks的目的是让SonarQube能够找到Jenkins,在分析完毕后可以把结果返回Jenkins


2.6.8建立Jenkinsfile

pipeline{
    agent any
    tools{
        maven 'mvn-3.6.3'
    }
    stages{
     stage('Code Analysis'){
         steps{
         withSonarQubeEnv('sonarqube'){
                  bat  '''
                  mvn clean verify sonar:sonar \
                  -Dsonar.login=6cdf6f3176211d67dbf469712168c005c48f8c4d\
                  -Dsonar.host.url=http://127.0.0.1:9000
                  '''
             }
         }
     }
     stage('Quality Gate'){
         steps{
             script {
                  timeout(time:1,unit:'HOURS'){
                  sleep(5)
                  def qg = waitForQualityGate()
                  if (qg.status != 'OK') {
                      echo "Status:${qg.status}"
                      error "Pipelineaborted due to quality gate failure: ${qg.status}"
                  }
             }
             }
         }
     }
    }
}


2.6.7 查看质量阈

选择菜单“质量阈”,可以查看默认的质量阈值,如图25所示。

image.png

25 质量阈


2.6.7构建

构建完毕,在项目的左边会产生一个

image.png

图标,点击进入可以查看SonarQube整体分析结果。如果这次分析不符合图25设置的质量阈,这次构建会产生失败信息。


2.6.9 注意事项

令牌是SonarQube是中非常重要的选项,前后必须保持一致,否则构建中会返回http 401错误从而导致构建失败。关于这方面资料网上几乎没有,我在这里栽了个大跟头,各位千万需要注意。


2.6SonarQube中加入PMD规则

在第2.1节中,我们介绍了PMD,这里来介绍如何让SonarQube中加入PMD规则。


2.6.1 下载SonarQubePMD插件

https://download.csdn.net/download/qq_36752632/11237005下载p3c-pmd-1.3.6.jar,下载完毕将其放入%SONAR_HOME%\extensions\plugins\目录下(千万注意,每次重新启动SonarQube必须把p3c-pmd-1.3.6.jar放在其他路径下,启动成功然后再放进来,否则SonarQube会启动失败)


2.6.2 配置PMD插件

选择菜单“质量配置->创建”,按图26设置,语言选择JAVA

image.png

26新建质量配置


按照图27 选择“默认”(由于此图已经设置了默认,所以没有这个选项了)和“激活更多规则”。

image.png

27 设置质量配置


如图28,点击批量修改。

image.png

28 接受PMD规则


建立完毕会显示P3M已经过时,不要理它。在运行构建的时候可以从“Console Output”中存在:

[INFO] Qualityprofile for java: p3cPMD

信息证明,SonarQube已经对这个规则进行了检验。


2.7集成Allure测试报告

Allure2是一个非常完美的测试报告生成器,这里我们来讲解如何使用Allure产生TestNG的测试报告。


2.7.1 Allure的下载和安装

下载allure-commandline,拷贝在本地目录下(C:\allure-2.10.0)。

设置环境参数%ALLURE_HOME%,将%ALLURE_HOME%\bin加入path中,如图29所示。

image.png

29 设置Allure环境参数


2.7.2 安装配置Jenkins Allure插件

进入Jenkins插件中心,安装Allure插件,如图30所示。

image.png

30 安装Jenkins Allure插件


在“全局工具配置中(Global Tool Configuration)”找到图31选项。

image.png

31 配置Allure Commandline


注意:安装目录为“C:\allure-2.10.0”,千万不要包含bin目录,即:C:\allure-2.10.0\bin,否则会引起构建失败。

进入配置,设置“全局属性”-工具位置列表,配置Allure Commandline,如图32所示。

image.png

32 AllureCommandline全局属性配置


这里目录仍旧为“C:\allure-2.10.0”,不要包含bin路径。


2.7.3修改pom.xml文件

<groupId>com.jerry</groupId>
  <artifactId>mytest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
<build>
 <plugins>
     <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20</version>
             <configuration>
                <systemPropertyVariables>
                    <allure.results.directory>${project.build.directory}/allure-results/${maven.build.timestamp}</allure.results.directory>
                   <allure.link.issue.pattern>https://example.org/browse/{}</allure.link.issue.pattern>
                    <allure.link.tms.pattern>https://example.org/browse/{}</allure.link.tms.pattern>
               </systemPropertyVariables>
                  <includes>
                      <include>**/**.java</include>
                  </includes>
                <argLine>
                   -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                 </argLine>
             </configuration>
             <dependencies>
                <dependency>
                   <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                   <version>${aspectj.version}</version>
                </dependency>
             </dependencies>
      </plugin>
   </plugins>
</build>
<properties>
    <aspectj.version>1.8.10</aspectj.version>
    <maven.build.timestamp.format></maven.build.timestamp.format>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
     <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
 </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
     <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.qameta.allure</groupId>
       <artifactId>allure-testng</artifactId>
        <version>2.12.1</version>
        </dependency>
  </dependencies>


这里特别需要注意,maven-surefire-plugin要求,测试文件必须是以下格式:

"**/Test*.java"

"**/*Test.java"

"**/*Tests.java"

"**/*TestCase.java“

加入“**/**.java”可以不受这个条件控制。


2.7.4建立Jenkinsfile

pipeline{
    agent any
    tools{
        maven 'mvn-3.6.3'
    }
    stages{
     stage('junit'){
            steps {
         bat "mvn test"
            }
         }
    }
    post{
        always{
       junit testResults: "**/target/surefire-reports/*.xml"
       script{
          allure([
            includeProperties:false,
            jdk:'',
             properties: [],
          reportBuildPolicy:'ALWAYS',
          results: [[path: 'target/allure-results']]
         ])
       }
        }
    }
}


2.7.5构建

构建完毕,点击Allure Report链接即可以查看Allure报告,如图33所示。

image.png

33 Allure报告


2.8 所有功能测试的集成

这里我们来介绍除了JMeter的功能测试的集成。


2.8.1修改pom.xml文件

由于JacocoAllure都要使用maven-surefire-plugin,所以需要在maven-surefire-plugin配置中加入${jacocoArgLine} -Xmx256M参数:

<argLine>
     ${jacocoArgLine} -Xmx256M
                 -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>


否则Jacoco不起作用。注意,由于Allure也存在参数,必须把这两个参数放在一起。我开始是这样设置的,结果Jacoco仍旧不可运行。

<argLine>
     ${jacocoArgLine} -Xmx256M
</argLine>
<argLine>
                 -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>


追踪的pom.xml如下:

<groupId>com.jerry</groupId>
  <artifactId>mytest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
<name>mytest</name>
<url>http://maven.apache.org</url>
<build>
 <plugins>
     <plugin>
       <groupId>org.jacoco</groupId>
       <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.2</version>
         <executions>
        <execution>
            <id>pre-unit-test</id>
            <goals>
               <goal>prepare-agent</goal>
            </goals>
            <configuration>
               <propertyName>jacocoArgLine</propertyName>
            </configuration>
          </execution>
          <execution>
             <id>post-unit-report</id>
              <phase>package</phase>
              <goals>
                 <goal>report</goal>
               </goals>
          </execution>
          <execution>
            <id>prepare-agent</id>
            <goals>
             <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>report</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
          <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
              <goal>report</goal>
            </goals>
             <configuration>
                  <dataFile>target/jacoco.exec</dataFile>
                  <outputDirctory>target.jacoco.exec</outputDirctory>
             </configuration>
          </execution>
        </executions>
         <configuration>
             <syatemPropertyVariable>
                  <jacoco-agent.destfile>target/jacoco.exec/</jacoco-agent.destfile>
             </syatemPropertyVariable>
         </configuration>
     </plugin>
     <plugin>
         <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20</version>
          <configuration>
                <!--<argLine>${jacocoArgLine} -Xmx256M</argLine>-->
              <skip>false</skip>
              <testFailureIgnore>false</testFailureIgnore>
                <skipTests>false</skipTests>
                <systemPropertyVariables>
                   <allure.results.directory>${project.build.directory}/allure-results/${maven.build.timestamp}</allure.results.directory>
                    <allure.link.issue.pattern>https://example.org/browse/{}</allure.link.issue.pattern>
                   <allure.link.tms.pattern>https://example.org/browse/{}</allure.link.tms.pattern>
               </systemPropertyVariables>
                  <includes>
                      <include>**/**.java</include>
                  </includes>
                <argLine>
                      ${jacocoArgLine} -Xmx256M
                   -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                 </argLine>
                   <!--<excludes>
                      <exclude>**/controller/*ControllerTest.java</exclude>
                </excludes>-->
             </configuration>
             <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                   <artifactId>aspectjweaver</artifactId>
                   <version>${aspectj.version}</version>
                </dependency>
             </dependencies>
      </plugin>
   </plugins>
</build>
<properties>
    <aspectj.version>1.8.10</aspectj.version>
    <maven.build.timestamp.format></maven.build.timestamp.format>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
 </properties>
  <dependencies>
    <dependency>
         <groupId>com.alibaba.p3c</groupId>
         <artifactId>p3c-pmd</artifactId>
         <version>1.3.5</version>
         <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
     <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
         <groupId>org.testng</groupId>
         <artifactId>testng</artifactId>
         <version>6.14.2</version>
    </dependency>
    <dependency>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-resources-plugin</artifactId>
         <version>2.4.3</version>
     </dependency>
  </dependencies>


2.8.2 建立Jenkinsfile

pipeline{
    agent any
    tools{
        maven 'mvn-3.6.3'
    }
    stages{
         stage('pmd'){
            steps {
                bat "mvn pmd:pmd"
            }
        }
     stage('junit'){
            steps {
         bat "mvn clean test"
            }
         }
     stage('jacoco'){
            steps{
            bat "mvn clean install"
        jacoco()
        }
     }
     stage('Code Analysis'){
         steps{
         withSonarQubeEnv('sonarqube'){
                  bat  '''
                  mvn clean verify sonar:sonar \
                  -Dsonar.login=6cdf6f3176211d67dbf469712168c005c48f8c4d\
                  -Dsonar.login=admin \
                  -Dsonar.password=admin \
                  -Dsonar.host.url=http://127.0.0.1:9000
                  '''
             }
         }
     }
     stage('Quality Gate'){
         steps{
             script {
                  timeout(time:1,unit:'HOURS'){
                  sleep(5)
                  def qg = waitForQualityGate()
                  echo qg.status
                  if (qg.status != 'OK') {
                      echo "Status:${qg.status}"
                      error "Pipelineaborted due to quality gate failure: ${qg.status}"
                  }
             }
             }
         }
     }
    }
    post{
        always{
       junit testResults: "**/target/surefire-reports/*.xml"
       pmd(canRunOnFailed:true,pattern:'**/target/pmd.xml')
       script{
          allure([
            includeProperties:false,
            jdk:'',
            properties: [],
          reportBuildPolicy:'ALWAYS',
          results: [[path: 'target/surefire-reports']]
         ])
       }
        }
    }
}


2.7.3构建

构建完毕,如图34设置。

image.png

34 所有功能测试的集成构建结果


3.让一个系统同时支持多个版本的JDK


前面说过,由于SonarQube需要使用JDK11,但是我们许多产品还是仅支持JDK8,在这里来介绍如何让一个系统同时支持多个版本的JDK

  1. 安装JDK8 JDK 10
  2. 进入C:\Program Files (x86)\Common Files\Oracle\Java\javapath,删除里面的三个文件
  3. 以管理员身份运行CMD

C:\>cd C:\Program Files\Java\jdk-11.0.5

  1. 使用命令

C:\Program Files\Java\jdk-11.0.5>bin\jlink.exe--module-path jmods --add-modules java.desktop --output jre

手动生成JRE


  1. 配置环境变量,如图35所示。

image.png

35 设置多版本的JDK


JAVA的其他环境变量按照以前方法设置。如果要切换到JDK,把JAVA_HOME的值设置为:%JAVA_HOME11%


科学是一种强有力的工具。怎样用它,究竟是给人带来幸福还是带来灾难,全取决于人自己,而不取决于工具。刀子在人类生活上是有用的,但它也能用来杀人。

——爱因斯坦

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
1天前
|
jenkins 持续交付 API
上传gitlab代码后jenkins自动进行发布的配置
上传gitlab代码后jenkins自动进行发布的配置
12 1
|
1天前
|
DataWorks NoSQL 关系型数据库
DataWorks操作报错合集之在使用 DataWorks 进行 MongoDB 同步时遇到了连通性测试失败,实例配置和 MongoDB 白名单配置均正确,且同 VPC 下 MySQL 可以成功连接并同步,但 MongoDB 却无法完成同样的操作如何解决
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
33 1
|
1天前
|
JavaScript API
【vue】分环境构建(开发/测试/生产)配置
【vue】分环境构建(开发/测试/生产)配置
18 1
|
1天前
|
Ubuntu Linux 测试技术
Linux(32)Rockchip RK3568 Ubuntu22.04上部署 Docker: 详细配置与功能测试(下)
Linux(32)Rockchip RK3568 Ubuntu22.04上部署 Docker: 详细配置与功能测试
51 1
|
1天前
|
Ubuntu Linux 测试技术
Linux(32)Rockchip RK3568 Ubuntu22.04上部署 Docker: 详细配置与功能测试(上)
Linux(32)Rockchip RK3568 Ubuntu22.04上部署 Docker: 详细配置与功能测试
75 0
|
1天前
|
分布式计算 Hadoop Scala
Spark【环境搭建 01】spark-3.0.0-without 单机版(安装+配置+测试案例)
【4月更文挑战第13天】Spark【环境搭建 01】spark-3.0.0-without 单机版(安装+配置+测试案例)
32 0
|
1天前
|
关系型数据库 Java 测试技术
云效产品使用常见问题之流水线Maven单元测试链接rds要配置白名单如何解决
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
1天前
|
jenkins 测试技术 持续交付
Jenkins配置测试报告后无法正常显示或显示空的解决方法(问题集锦)
根据具体情况逐一排查上述问题,往往可以解决Jenkins配置测试报告无法正常显示或显示空的问题。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
24 0
|
1天前
|
jenkins Java 持续交付
【项目集成工具】Jenkins
【项目集成工具】Jenkins

热门文章

最新文章