java接⼝⾃动化测试框架及断⾔详解
在,我们介绍了Get⽅法的设计过程和测试结果,现在我们需要对前⾯代码进⾏重构和修改,本篇需要完成以下⽬标。
1)重构Get⽅法
2)如何进⾏JSON解析
3)使⽤TestNG⽅法进⾏测试断⾔
1.重构Get⽅法
在前⾯⽂章,说过,之前写的Get⽅法⽐较繁琐,不光写了如何进⾏Get请求,还写了获取http响应状态码和JSON转换。现在我们需要抽取出来,设计Get请求⽅法,就只⼲⼀件事情,那就是如何发送get请求,其他的不要管。
我们知道,请求之后会返回⼀个HTTP的响应对象,所以,我们把get⽅法的返回值类型改成了响应对象,并带上返回语句,重构代码之后,get⽅法代码如下。
package stclient;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.hods.CloseableHttpResponse;
import org.apache.hods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class RestClient {
//1. Get 请求⽅法
public CloseableHttpResponse get(String url) throwsClientProtocolException, IOException {
//创建⼀个可关闭的HttpClient对象
CloseableHttpClienthttpclient = ateDefault();
/
/创建⼀个HttpGet的请求对象
HttpGethttpget = newHttpGet(url);
//执⾏请求,相当于postman上点击发送按钮,然后赋值给HttpResponse对象接收
CloseableHttpResponsehttpResponse = ute(httpget);
return httpResponse;
}
}
由于我们不想在代码⾥写死例如像HTTP响应状态码200这样的硬编码,所以,这⾥我们在TestBase.java⾥把状态码给⽤常量写出来,⽅便每⼀个TestNG测试⽤例去调⽤去断⾔。
package com.qa.base;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class TestBase {
public Properties prop;
public int RESPNSE_STATUS_CODE_200 = 200;
public int RESPNSE_STATUS_CODE_201 = 201;
public int RESPNSE_STATUS_CODE_404 = 404;
public int RESPNSE_STATUS_CODE_500 = 500;
//写⼀个构造函数
public TestBase() {
try{
prop= new Properties();
FileInputStreamfis = new Property("user.dir")+
"/src/main/java/com/qa/config/config.properties");
prop.load(fis);
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
现在我们的测试类代码修改之后如下。
package sts;
import java.io.IOException;
importorg.apache.http.client.ClientProtocolException;
importorg.apache.hods.CloseableHttpResponse;
stng.Assert;
stng.annotations.BeforeClass;
stng.annotations.Test;
import com.qa.base.TestBase;
import stclient.RestClient;
public class GetApiTest extends TestBase{
TestBase testBase;
String host;
String url;
RestClient restClient;
CloseableHttpResponse closeableHttpResponse;
@BeforeClass
public void setUp() {
testBase = new TestBase();
host = Property("HOST");
url = host + "/api/users";
}
@Test
fastjson字符串转数组public void getAPITest() throws ClientProtocolException, IOException {
restClient = new RestClient();
closeableHttpResponse= (url);
//断⾔状态码是不是200
int statusCode = StatusLine().getStatusCode();
Assert.assertEquals(statusCode,RESPNSE_STATUS_CODE_200, "response status code is not 200");
}
}
测试运⾏通过,没⽑病。
2.写⼀个JSON解析的⼯具类
在上⾯部分,我们只是写了执⾏Get请求和状态码是否200的断⾔。接下来,我们需要写有⼀个JSON解析⼯具类,这样就⽅便我们去json内容的断⾔。
下⾯这个JSON数据截图
上⾯是⼀个标准的json的响应内容截图,第⼀个红圈”per_page”是⼀个json对象,我们可以根据”per_page”来到对应值是3,⽽第⼆个红圈“data”是⼀个JSON数组,⽽不是对象,不能直接去拿到⾥⾯值,需要遍历数组。
下⾯,我们写⼀个JSON解析的⼯具⽅法类,如果是像第⼀个红圈的JSON对象,我们直接返回对应的值,如果是需要解析类似data数组⾥⾯的json对象的值,这⾥我们构造⽅法默认解析数组第⼀个元素的内容。
在src/main/java下新建⼀个包:com.qa.util,然后在新包下创建⼀个TestUtil.java类。
package com.qa.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class TestUtil {
/**
*
* @param responseJson ,这个变量是拿到响应字符串通过json转换成json对象
* @param jpath,这个jpath指的是⽤户想要查询json对象的值的路径写法
* jpath写法举例:1) per_page 2)data[1]/first_name ,data是⼀个json数组,[1]表⽰索引
* /first_name 表⽰data数组下某⼀个元素下的json对象的名称为first_name
* @return,返回first_name这个json对象名称对应的值
*/
//1 json解析⽅法
public static String getValueByJPath(JSONObject responseJson, String jpath){
Objectobj = responseJson;
for(String s : jpath.split("/")) {
if(!s.isEmpty()) {
if(!(s.contains("[") || s.contains("]"))) {
obj = ((JSONObject) obj).get(s);
}else ains("[") || s.contains("]")) {
obj =((JSONArray)((JSONObject)obj).get(s.split("\\[")[0])).get(Integer.parseInt(s.split("\\[")[1].replaceAll("]", "")));
}
}
}
String();
}
}
简单解释下上⾯的代码,主要是查询两种json对象的的值,第⼀种最简单的,这个json对象在整个json串的第⼀层,例如上⾯截图中的per_page,这个per_page就是通过jpath这个参数传⼊,返回的结果就是3. 第⼆种jpath的查询,例如我想查询data下
第⼀个⽤户信息⾥⾯的first_name的值,这个时候jpath的写法就是data[0]/first_name,查询结果应该是Eve。
3.TestNG测试⽤例
下⾯,我们TestNG测试⽤例代码如下
package sts;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.hods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
stng.Assert;
stng.annotations.BeforeClass;
stng.annotations.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qa.base.TestBase;
import stclient.RestClient;
import com.qa.util.TestUtil;
public class GetApiTest extends TestBase{
TestBase testBase;
String host;
String url;
RestClient restClient;
CloseableHttpResponse closeableHttpResponse;
@BeforeClass
public void setUp() {
testBase = new TestBase();
host = Property("HOST");
url = host + "/api/users?page=2";
}
@Test
public void getAPITest() throws ClientProtocolException, IOException {
restClient = new RestClient();
closeableHttpResponse = (url);
//断⾔状态码是不是200
int statusCode = StatusLine().getStatusCode();
Assert.assertEquals(statusCode, RESPNSE_STATUS_CODE_200, "response status code is not 200");
//把响应内容存储在字符串对象
String responseString = Entity(),"UTF-8");
//创建Json对象,把上⾯字符串序列化成Json对象
JSONObject responseJson = JSON.parseObject(responseString);
//System.out.println("respon json from API-->" + responseJson);
//json内容解析
String s = ValueByJPath(responseJson,"data[0]/first_name");
System.out.println(s);
}
}
运⾏测试结果:
[RemoteTestNG] detected TestNGversion 6.14.3
Eve
PASSED: getAPITest
你还可以多写⼏个jpath来测试这个json解析⼯具类。
String s = ValueByJPath(responseJson,"data[1]/id");
String s = ValueByJPath(responseJson,"per_page");
4.TestNG⾃带的测试断⾔⽅法
这⾥简单提⼀下TestNG的断⾔⽅法,我们⼀般测试都需要写断⾔的代码,否则这样的单元测试代码就没有意义。下⾯,我在statusCode和json解析的first_name进⾏断⾔。
package sts;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.hods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
stng.Assert;
stng.annotations.BeforeClass;
stng.annotations.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qa.base.TestBase;
import stclient.RestClient;
import com.qa.util.TestUtil;
public class GetApiTest extends TestBase{
TestBase testBase;
String host;
String url;
RestClient restClient;
CloseableHttpResponse closeableHttpResponse;
@BeforeClass
public void setUp() {
testBase = new TestBase();
host = Property("HOST");
url = host + "/api/users?page=2";
}
@Test
public void getAPITest() throws ClientProtocolException, IOException {
restClient = new RestClient();
closeableHttpResponse = (url);
/
/断⾔状态码是不是200
int statusCode = StatusLine().getStatusCode();
Assert.assertEquals(statusCode, RESPNSE_STATUS_CODE_200, "response status code is not 200");  //把响应内容存储在字符串对象
String responseString = Entity(),"UTF-8");
//创建Json对象,把上⾯字符串序列化成Json对象
JSONObject responseJson = JSON.parseObject(responseString);
//System.out.println("respon json from API-->" + responseJson);
//json内容解析
String s = ValueByJPath(responseJson,"data[0]/first_name");
System.out.println(s);
Assert.assertEquals(s, "Eve","first name is not Eve");
}
}
经常使⽤的测试断⾔:
Assert.assertEquals(“现实结果”, "期待结果","断⾔失败时候打印⽇志消息");
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。