背景說明
為了可以在jenkins自動化運行單元測試的代碼,所以使用maven+testng的技術(shù)結(jié)合,達到手動或者定時去執(zhí)行單元測試的代碼,以便提高人工運行的自動化的效率。單元通過該方案也可以套用在httpclient框架去執(zhí)行測試web api接口的自動化測試,原理是一致的。
環(huán)境準備
- 安裝開發(fā)工具:eclipse開發(fā)工具
- 安裝maven:在官方下載maven在開發(fā)環(huán)境和jenkins環(huán)境都需要安裝配置,下載地址:Maven – Download Apache Mavenhttps://maven.apache.org/download.cgi
- 安裝jenkins服務(wù):Jenkins
- 安裝jdk1.7以上的版本:Java Downloads | Oracle
代碼結(jié)構(gòu)
配置pom.xml
創(chuàng)建測試項目時,以maven類型項目創(chuàng)建
<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>jenkins.testng.demo</groupId>
<artifactId>TestDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestDemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<!-- 加入maven-surefire-plugin插件用來使用maven執(zhí)行用例,其中suiteXmlFile配置的就是testNG用例執(zhí)行文件的地址 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<!-- 忽略測試錯誤,繼續(xù)編譯 -->
<testFailureIgnore>true</testFailureIgnore>
<suiteXmlFiles>
<suiteXmlFile>src/main/java/jenkins/testng/demo/TestngSample/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<!-- 加入編碼設(shè)置,否則生成的報告會中文亂碼 -->
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven/maven-plugin-api -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.6.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven/maven-artifact -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>3.6.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugin-tools/maven-plugin-annotations -->
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.6.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.uncommons/reportng -->
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
測試代碼
package jenkins.testng.demo.TestngSample;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
public class TestDemong {
@Test
public void demo() {
assertEquals(true, true);
}
@Test
public void demo2() {
assertEquals(true, true);
}
}
配置testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="test">
<test name="test">
<classes>
<class name="jenkins.testng.demo.TestngSample.TestDemong">
</class>
</classes>
</test>
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener
class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>
</suite>
配置jenkins
運行查看
文章來源:http://www.zghlxwxcb.cn/news/detail-694326.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-694326.html
到了這里,關(guān)于Jenkins+maven+testng+htmlreport單元自動化測試的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!