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 "Pipeline aborted due to quality gate failure: ${qg.status}" } } } } } } } |
2.6.7 查看质量阈
选择菜单“质量阈”,可以查看默认的质量阈值,如图25所示。
图25 质量阈
2.6.7构建
构建完毕,在项目的左边会产生一个图标,点击进入可以查看SonarQube整体分析结果。如果这次分析不符合图25设置的质量阈,这次构建会产生失败信息。
2.6.9 注意事项
令牌是SonarQube是中非常重要的选项,前后必须保持一致,否则构建中会返回http 401错误从而导致构建失败。关于这方面资料网上几乎没有,我在这里栽了个大跟头,各位千万需要注意。
2.6 SonarQube中加入PMD规则
在第2.1节中,我们介绍了PMD,这里来介绍如何让SonarQube中加入PMD规则。
2.6.1 下载SonarQube的PMD插件
到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。
图26新建质量配置
按照图27 选择“默认”(由于此图已经设置了默认,所以没有这个选项了)和“激活更多规则”。
图27 设置质量配置
如图28,点击批量修改。
图28 接受PMD规则
建立完毕会显示P3M已经过时,不要理它。在运行构建的时候可以从“Console Output”中存在:
[INFO] Quality profile 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所示。
图29 设置Allure环境参数
2.7.2 安装配置Jenkins Allure插件
进入Jenkins插件中心,安装Allure插件,如图30所示。
图30 安装Jenkins Allure插件
在“全局工具配置中(Global Tool Configuration)”找到图31选项。
图31 配置Allure Commandline
注意:安装目录为“C:\allure-2.10.0”,千万不要包含bin目录,即:C:\allure-2.10.0\bin,否则会引起构建失败。
进入配置,设置“全局属性”-工具位置列表,配置Allure Commandline,如图32所示。
图32 Allure Commandline全局属性配置
这里目录仍旧为“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“
加入“<include>**/**.java</include>”可以不受这个条件控制。
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所示。
图33 Allure报告
2.8 所有功能测试的集成
这里我们来介绍除了JMeter的功能测试的集成。
2.8.1修改pom.xml文件
由于Jacoco和Allure都要使用maven-surefire-plugin,所以需要在maven-surefire-plugin配置<argLine>…</argLine>中加入${jacocoArgLine} -Xmx256M参数:
… <argLine> ${jacocoArgLine} -Xmx256M -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar" </argLine> … |
否则Jacoco不起作用。注意,由于Allure也存在<argLine>…</argLine>参数,必须把这两个参数放在一起。我开始是这样设置的,结果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 "Pipeline aborted 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设置。
图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
4.使用命令
C:\Program Files\Java\jdk-11.0.5>bin\jlink.exe --module-path jmods --add-modules java.desktop --output jre
手动生成JRE
5.配置环境变量,如图35所示。
图35 设置多版本的JDK
JAVA的其他环境变量按照以前方法设置。如果要切换到JDK,把JAVA_HOME的值设置为:%JAVA_HOME11%。
版权声明:本文出自51Testing会员投稿,51Testing软件测试网及相关内容提供者拥有内容的全部版权,未经明确的书面许可,任何人或单位不得对本网站内容复制、转载或进行镜像,否则将追究法律责任。