基于Java+TestNG+Allure實現(xiàn)接口測試框架的自動化報告
做一個簡單的基于Java+TestNG+Allure的自動化報告demo
1 環(huán)境準備
- JDK 1.8+
- Maven 3.6+
- IntelliJ IDEA(推薦)
2 項目結(jié)構(gòu)
src
├── main
│ └── java
│ └── com
│ └── example
│ └── utils
│ └── HttpUtil.java
└── test
├── java
│ └── com
│ └── example
│ └── tests
│ └── ApiTest.java
└── resources
├── testng.xml
└── allure.properties
3 添加依賴(pom.xml)
<dependencies>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
</dependency>
<!-- Allure -->
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.13.8</version>
</dependency>
<!-- HTTP Client -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<!-- JSON處理 -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven編譯插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- TestNG執(zhí)行插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/1.9.7/aspectjweaver-1.9.7.jar"
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
</dependencies>
</plugin>
<!-- Allure報告插件 -->
<plugin>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-maven</artifactId>
<version>2.10.0</version>
<configuration>
<reportVersion>2.13.8</reportVersion>
</configuration>
</plugin>
</plugins>
</build>
4 工具類實現(xiàn)(HttpUtil.java)
package com.example.utils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpUtil {
public static String doGet(String url) throws Exception {
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
return EntityUtils.toString(response.getEntity());
}
}
public static String doPost(String url, String jsonBody) throws Exception {
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpPost request = new HttpPost(url);
request.setHeader("Content-Type", "application/json");
request.setEntity(new StringEntity(jsonBody));
HttpResponse response = client.execute(request);
return EntityUtils.toString(response.getEntity());
}
}
}
5 測試用例(ApiTest.java)
package com.example.tests;
import com.example.utils.HttpUtil;
import io.qameta.allure.*;
import org.testng.Assert;
import org.testng.annotations.Test;
public class ApiTest {
@Test(description = "測試GET請求")
@Description("驗證GET請求返回狀態(tài)和內(nèi)容")
@Severity(SeverityLevel.CRITICAL)
@Story("基礎(chǔ)API測試")
@Feature("API功能測試")
public void testGetRequest() throws Exception {
String url = "https://jsonplaceholder.typicode.com/posts/1";
String response = HttpUtil.doGet(url);
// 驗證響應包含關(guān)鍵字段
Assert.assertTrue(response.contains("\"userId\": 1"), "驗證userId");
Assert.assertTrue(response.contains("\"id\": 1"), "驗證id");
}
@Test(description = "測試POST請求")
@Description("驗證POST請求創(chuàng)建資源")
@Severity(SeverityLevel.NORMAL)
@Story("數(shù)據(jù)創(chuàng)建測試")
@Feature("API功能測試")
public void testPostRequest() throws Exception {
String url = "https://jsonplaceholder.typicode.com/posts";
String jsonBody = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";
String response = HttpUtil.doPost(url, jsonBody);
// 驗證響應包含創(chuàng)建的數(shù)據(jù)
Assert.assertTrue(response.contains("\"title\": \"foo\""), "驗證title");
Assert.assertTrue(response.contains("\"id\": 101"), "驗證新資源ID");
}
}
6 TestNG 配置文件(testng.xml)
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="API_Test_Suite">
<test name="API_Tests">
<classes>
<class name="com.example.tests.ApiTest"/>
</classes>
</test>
</suite>
7 Allure配置文件(allure.properties)
allure.results.directory=target/allure-results
allure.link.issue.pattern=https://example.com/issue/{}
allure.link.tms.pattern=https://example.com/tms/{}
8 配置實現(xiàn)(關(guān)鍵點):maven的settings.xml文件
文件路徑
${user.home}/.m2/settings.xml(例如:C:\Users\你的用戶名.m2\settings.xml)
具體配置
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- 本地倉庫位置 -->
<localRepository>${user.home}/.m2/repository</localRepository>
<!-- 插件組配置(包含Allure插件) -->
<pluginGroups>
<pluginGroup>io.qameta.allure</pluginGroup>
</pluginGroups>
<!-- 代理設(shè)置(根據(jù)實際需要配置) -->
<!--
<proxies>
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
</proxies>
-->
<!-- 鏡像設(shè)置(推薦使用阿里云鏡像加速下載) -->
<mirrors>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共倉庫</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
<!-- 配置文件激活條件 -->
<profiles>
<!-- JDK 1.8 配置 -->
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
<!-- Allure 配置 -->
<profile>
<id>allure</id>
<properties>
<allure.results.directory>${project.build.directory}/allure-results</allure.results.directory>
</properties>
</profile>
</profiles>
<!-- 激活的配置文件 -->
<activeProfiles>
<activeProfile>jdk-1.8</activeProfile>
<activeProfile>allure</activeProfile>
</activeProfiles>
</settings>
說明:具體有三點:
1)本地倉庫位置:
<localRepository>${user.home}/.m2/repository</localRepository>
2)Allure 插件組配置:
<pluginGroups>
<pluginGroup>io.qameta.allure</pluginGroup>
</pluginGroups>
3)Allure 結(jié)果目錄
<profile>
<id>allure</id>
<properties>
<allure.results.directory>${project.build.directory}/allure-results</allure.results.directory>
</properties>
</profile>
9 測試執(zhí)行
方式一:使用Maven命令
# 運行測試生成 Allure 結(jié)果數(shù)據(jù)
# 這會在 target/allure-results 目錄下生成原始結(jié)果數(shù)據(jù)
mvn clean test
# 生成Allure報告(HTML 格式)
mvn allure:serve
方式二:使用TestNG直接運行
1.右鍵點擊testng.xml -> Run
會在target/allure-results/下生成測試結(jié)果的 .json 文件
2.生成allure報告
# 生成Allure報告
mvn allure:serve
會自動打開測試報告的html地址: http://192.168.82.224:49195/
10 報告效果


說明:
Allure報告將包含:
- 測試套件概覽
- 用例詳細步驟
- 請求/響應詳情
- 嚴重級別標記
- 歷史趨勢分析
- 環(huán)境信息
浙公網(wǎng)安備 33010602011771號