Jenkins+maven+testng+htmlreport单元自动化测试

背景说明

为了可以在jenkins自动化运行单元测试的代码,所以使用maven+testng的技术结合,达到手动或者定时去执行单元测试的代码,以便提高人工运行的自动化的效率。单元通过该方案也可以套用在httpclient框架去执行测试web api接口的自动化测试,原理是一致的。

环境准备

  1. 安装开发工具:eclipse开发工具
  2. 安装maven:在官方下载maven在开发环境和jenkins环境都需要安装配置,下载地址:Maven -- Download Apache Mavenhttps://maven.apache.org/download.cgi
  3. 安装jenkins服务:Jenkins
  4. 安装jdk1.7以上的版本:Java Downloads | Oracle

代码结构

配置pom.xml

创建测试项目时,以maven类型项目创建

复制代码
<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执行用例,其中suiteXmlFile配置的就是testNG用例执行文件的地址 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <!--          忽略测试错误,继续编译         -->
                    <testFailureIgnore>true</testFailureIgnore>
                    <suiteXmlFiles>
                        <suiteXmlFile>src/main/java/jenkins/testng/demo/TestngSample/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <!-- 加入编码设置,否则生成的报告会中文乱码 -->
                    <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

运行查看

相关推荐
真实的菜3 分钟前
Jenkins 插件深度应用:让你的CI/CD流水线如虎添翼 [特殊字符]
servlet·ci/cd·jenkins
fo安方21 分钟前
运维的利器–监控–zabbix–第三步:配置zabbix–中间件–Tomcat–步骤+验证
运维·中间件·zabbix
蓝澈112131 分钟前
迪杰斯特拉算法之解决单源最短路径问题
java·数据结构
Kali_0738 分钟前
使用 Mathematical_Expression 从零开始实现数学题目的作答小游戏【可复制代码】
java·人工智能·免费
rzl021 小时前
java web5(黑马)
java·开发语言·前端
时序数据说1 小时前
为什么时序数据库IoTDB选择Java作为开发语言
java·大数据·开发语言·数据库·物联网·时序数据库·iotdb
超喜欢下雨天1 小时前
服务器安装 ros2时遇到底层库依赖冲突的问题
linux·运维·服务器·ros2
搬码临时工1 小时前
小企业如何搭建本地私有云服务器,并设置内部网络地址提供互联网访问
运维·服务器
guojl1 小时前
深度解读jdk8 HashMap设计与源码
java
guojl1 小时前
深度解读jdk8 ConcurrentHashMap设计与源码
java