Jenkins+maven+testng+htmlreport unit automation testing

Background

In order to automatically run the unit test code in Jenkins, the technology combination of maven + testng is used to execute the unit test code manually or regularly, so as to improve the efficiency of manual running automation. Through this solution, the unit can also be applied to the httpclient framework to perform automated testing of the web api interface. The principles are the same.

Environment preparation

  1. Install development tools: eclipse development tools
  2. Install maven: Download maven from the official website and need to install and configure it in both the development environment and jenkins environment. Download address: Maven – Download Apache Mavenicon- default.png?t=N7T8https://maven.apache.org/download.cgi
  3. Install jenkins service: Jenkins
  4. Install jdk1.7 or above: Java Downloads | Oracle

Code Structure

Configuration pom.xml

When creating a test project, create it as a maven type project

<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>
\t
<build>
<plugins>
            <!-- Add the maven-surefire-plugin plug-in to use maven to execute the use case, where suiteXmlFile is configured with the address of the testNG use case execution file -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <!-- Ignore test errors and continue compilation -->
                    <testFailureIgnore>true</testFailureIgnore>
                    <suiteXmlFiles>
                        <suiteXmlFile>src/main/java/jenkins/testng/demo/TestngSample/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <!-- Add encoding settings, otherwise the generated report will be garbled in Chinese -->
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>
          </plugins>
</build>
\t
\t
<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 -->
\t\t
<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>

Test code

package jenkins.testng.demo.TestngSample;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;

public class TestDemong {
\t
@Test
public void demo() {
assertEquals(true, true);
\t\t
}
\t
@Test
public void demo2() {
assertEquals(true, true);
\t\t
}

}

Configuration 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>

Configure jenkins

Run to view