接口自动化:HttpClient + TestNG + Java(三) - 初步封装和testng断言

时间:2023-02-03 14:22:36

在上一篇中,我们写了第一个get请求的测试类,这一篇我们来对他进行初步优化和封装

3.1 分离请求发送类

首先想到的问题是,以后我们的接口自动化测试框架会大量用到发送http请求的功能。

那么这一部分的处理,可以将他分离出来,以后的测试类只需要调用请求类的方法实现发送请求和接收反馈的功能。

在我们的项目目录src/main/java下,新建一个包名为com.test.client,在包下新建restfulClient.java。

这个类我们把上一篇写的发送请求和处理反馈的代码迁移过来,并做出一些改动:

package com.test.client;

import java.io.IOException;
import java.util.HashMap; import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; public class RestfulClient {
CloseableHttpClient httpclient;
HttpGet httpGet;
CloseableHttpResponse httpResponse;
int responseCode;
JSONObject responseBody;
HashMap<String, String> responseHeads; //通过httpclient获取请求的反馈
public void getResponse(String url) throws ClientProtocolException, IOException{
httpclient = HttpClients.createDefault();
httpGet = new HttpGet(url);
httpResponse = httpclient.execute(httpGet);
} //以JSON格式获取到反馈的主体
public JSONObject getBodyInJSON() throws ParseException, IOException{
HttpEntity entity;
String entityToString;
entity = httpResponse.getEntity();
entityToString = EntityUtils.toString(entity);
responseBody = JSON.parseObject(entityToString); System.out.println("This is your response body" + responseBody); return responseBody;
} //以哈希图的方式获取到反馈头部
public HashMap<String, String> getHeaderInHash(){
Header[] headers;
headers = httpResponse.getAllHeaders(); responseHeads = new HashMap<String, String>(); for(Header header:headers){
responseHeads.put(header.getName(), header.getValue());
} System.out.println("This is your response header" + responseHeads); return responseHeads;
} //获取反馈状态码
public int getCodeInNumber(){
responseCode = httpResponse.getStatusLine().getStatusCode(); System.out.println("This is your response code" + responseCode); return responseCode;
}
}

我们将代码重新构造后,写了四个方法:

  • getResponse:发送请求并获取反馈;
  • getBodyInJSON:获取JSON格式的反馈主体;
  • getCodeInNumber:获取反馈状态码;
  • getHeaderInHash:获取哈希图形式的反馈头;

后续我们考虑在测试类里面,直接调用这些方法。

3.2 引入JSON解析工具类

由于我们在获取反馈主体时,是将其存为了JSON对象,在我们后续做验证时,就需要去解读这个JSON对象。

我们考虑创建一个工具类,专门用来做JSON解析。

在项目目录src/main/java下创建一个包名为com.test.utils,新建JSONParser.java,代码如下:

package com.test.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; public class JSONParser {
JSONObject internalJSON; public String getCity(JSONObject jo){
     String city = "";
try {
       //先获取反馈中的result这个一个内部JSON对象 
JSONObject internalJSON = jo.getJSONObject("result");
//再根据键名查找键值
province = internalJSON.getString("city") ;
}catch (Exception e){
e.printStackTrace();
}
return city;
}
}

这里用到了fastjson库,提前在pom中加入依赖即可。

由于我们测试的这个接口主要是用来查找手机归属地,所以考虑暂时我们只需要通过反馈中最关键的手机所属城市来做验证。

于是这个所谓的JSONParser工具类里,我们暂时只实现了查找‘城市’。

后续我们再考虑将这个工具进一步完善。

3.3 引入TestNG

上一篇中我们还没有很好的执行测试和验证结果,这里我们考虑引入testNG来帮助完成这部分功能。

3.3.1 创建TestNG测试

在项目目录src/test/java下新建名为com.test.api的包,包下新建testNG类名为testGet.java。新建时带上BeforeClass()注释。

写入以下测试代码:

package com.test.api;

import org.testng.annotations.Test;

import com.alibaba.fastjson.JSONObject;
import com.test.client.RestfulClient;
import com.test.utils.JSONParser; import java.io.IOException;
import java.net.URL; import org.apache.http.ParseException;
import org.testng.Assert;
import org.testng.annotations.BeforeClass; public class testGet {
RestfulClient client;
JSONObject responseBody;
JSONParser jParser;
int responseCode;
String city;
String url = "https://api.apishop.net/communication/phone/getLocationByPhoneNum?apiKey=nMke6NK29c40b1d1331690abb50b3eec8aa0808389b16c4&phoneNum=1861195236";
@Test
public void TestGetRequest() {
      //断言反馈中城市是否正确
Assert.assertEquals(city, "北京");
      //断言反馈中的状态码是否正确
Assert.assertEquals(responseCode, 200);
}
@BeforeClass
public void beforeClass() throws ParseException, IOException {
      //发送请求,获取反馈
client = new RestfulClient();
client.getResponse(url);
responseBody = client.getBodyInJSON();
responseCode = client.getCodeInNumber();

      //调用JSONParser获取反馈中的城市信息
jParser = new JSONParser();
city = jParser.getCity(responseBody);
} }

其中,我们在beforeclass里去做发送请求接收反馈,然后调用上一节中封装好的方法获取反馈头部、主体和状态码。

然后通过JSONParser去获取反馈中的所属城市信息,做为核心验证点。

在test里我们做两个断言:

1.  判断状态码是否正确。

2.  判断手机所属城市是否正确。

3.3.2 TestNG测试结果

运行testNG测试,测试通过。

接口自动化:HttpClient + TestNG + Java(三) - 初步封装和testng断言

下一篇我们实现POST方法请求的测试,并进一步优化代码。