目录
1. 概述
2. 编写Get自动化测试用例
2.1 TestNG XML文件
1. 概述
首先将HttpClient和TestNG进行集成,如何进行HTTP接口自动化测试。
创建maven工程,引入如下依赖:
1. TestNG: 自动化测试框架TestNG的依赖。
2. JSON: 用于处理JSON的数据,在Restful POST请求体以及响应体中均可使用。
3. DOM4J: 用于处理XML数据,在SOAP请求的请求体和响应体中均可使用。
4. JAXEN: 当DOM4J用XPath方式获取节点时需要依赖该jar.
在依赖下载完成后,在工程右键, TestNG->Convert to TestNG, 从而生成TestNG xml测试驱动文件。
org.testng
testng
6.11
org.apache.httpcomponents
httpclient
4.5.9
org.json
json
20220320
org.dom4j
dom4j
2.1.3
jaxen
jaxen
1.2.0
2. 编写Get自动化测试用例
1. 创建GetMobilePhoneTest.
2. init()方法初始化了一个HTTP客户端,并在sendHttpGetRequests()方法中使用,最后在clear()方法中关闭。
3. 将构建URI, 发送GET请求和转换服务器响应等过程封装在sendHttpGetRequest()方法中,通过传递HTTP客户端和参数来使用该方法。
4. 在clear()方法中对HTTP客户端和服务器进行关闭操作,在操作中可能出现异常,所以需要对异常进行捕获和处理。
package com.pig.httpinterfacetest;
import java.net.URI;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
public class GetMobilePhoneTest {
private CloseableHttpClient client;
private CloseableHttpResponse response;
@BeforeClass
public void init(){
client = HttpClients.createDefault();
}
@Test
public void testCase1(){
Assert.assertEquals("{"brand":"Apple","model":"iPhone 6S","os":"IOS"}", sendHttpGetRequest(client, "iPhone 6S"));
}
@Test
public void testCase2(){
Assert.assertEquals("", sendHttpGetRequest(client, ""));
}
@Test
public void testCase3(){
Assert.assertEquals("", sendHttpGetRequest(client, null));
}
@Test
public void testCase4(){
Assert.assertEquals("", sendHttpGetRequest(client, "0123456789012345678901234567890123456789"));
}
@AfterClass
public void clear(){
try{
response.close();
client.close();
}catch (Exception e){
e.printStackTrace();
}
}
private String sendHttpGetRequest(CloseableHttpClient client, String model){
String result = null;
try {
URI uri = new URIBuilder().setScheme("http").setHost("localhost").setPort(8080).setPath("/mobilePhone")
.setParameter("model", model).build();
response = client.execute(new HttpGet(uri));
result = EntityUtils.toString(response.getEntity());
}catch (Exception e){
e.printStackTrace();
}
return result
}
}
2.1 TestNG XML文件
首先将HttpClient和TestNG进行集成,如何进行HTTP接口自动化测试。
创建maven工程,引入如下依赖:
1. TestNG: 自动化测试框架TestNG的依赖。
2. JSON: 用于处理JSON的数据,在Restful POST请求体以及响应体中均可使用。
3. DOM4J: 用于处理XML数据,在SOAP请求的请求体和响应体中均可使用。
4. JAXEN: 当DOM4J用XPath方式获取节点时需要依赖该jar.
在依赖下载完成后,在工程右键, TestNG->Convert to TestNG, 从而生成TestNG xml测试驱动文件。
1. 创建GetMobilePhoneTest.
2. init()方法初始化了一个HTTP客户端,并在sendHttpGetRequests()方法中使用,最后在clear()方法中关闭。
3. 将构建URI, 发送GET请求和转换服务器响应等过程封装在sendHttpGetRequest()方法中,通过传递HTTP客户端和参数来使用该方法。
4. 在clear()方法中对HTTP客户端和服务器进行关闭操作,在操作中可能出现异常,所以需要对异常进行捕获和处理。
package com.pig.httpinterfacetest;
import java.net.URI;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
public class GetMobilePhoneTest {
private CloseableHttpClient client;
private CloseableHttpResponse response;
@BeforeClass
public void init(){
client = HttpClients.createDefault();
}
@Test
public void testCase1(){
Assert.assertEquals("{"brand":"Apple","model":"iPhone 6S","os":"IOS"}", sendHttpGetRequest(client, "iPhone 6S"));
}
@Test
public void testCase2(){
Assert.assertEquals("", sendHttpGetRequest(client, ""));
}
@Test
public void testCase3(){
Assert.assertEquals("", sendHttpGetRequest(client, null));
}
@Test
public void testCase4(){
Assert.assertEquals("", sendHttpGetRequest(client, "0123456789012345678901234567890123456789"));
}
@AfterClass
public void clear(){
try{
response.close();
client.close();
}catch (Exception e){
e.printStackTrace();
}
}
private String sendHttpGetRequest(CloseableHttpClient client, String model){
String result = null;
try {
URI uri = new URIBuilder().setScheme("http").setHost("localhost").setPort(8080).setPath("/mobilePhone")
.setParameter("model", model).build();
response = client.execute(new HttpGet(uri));
result = EntityUtils.toString(response.getEntity());
}catch (Exception e){
e.printStackTrace();
}
return result
}
}
2.1 TestNG XML文件
运行结果



