java接口自动化框架_java接口自动化测试框架及断言详解
在上篇文章,我们介绍了Get方法的设计过程和测试结果,现在我们需要对前面代码进行重构和修改,本篇需要完成以下目标。
1)重构Get方法
2)如何进行JSON解析
3)使用TestNG方法进行测试断言
1.重构Get方法
在前面文章,说过,之前写的Get方法比较繁琐,不光写了如何进行Get请求,还写了获取http响应状态码和JSON转换。现在我们需要抽取出来,设计Get请求方法,就只干一件事情,那就是如何发送get请求,其他的不要管。
我们知道,请求之后会返回一个HTTP的响应对象,所以,我们把get方法的返回值类型改成了响应对象,并带上返回语句,重构代码之后,get方法代码如下。
package com.qa.restclient;
import java.io.IOException;
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;
public class RestClient {
//1. Get 请求方法
public CloseableHttpResponse get(String url) throwsClientProtocolException, IOException {
//创建一个可关闭的HttpClient对象
CloseableHttpClienthttpclient = HttpClients.createDefault();
//创建一个HttpGet的请求对象
HttpGethttpget = newHttpGet(url);
//执行请求,相当于postman上点击发送按钮,然后赋值给HttpResponse对象接收
CloseableHttpResponsehttpResponse = httpclient.execute(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 FileInputStream(System.getProperty("user.dir")+
"/src/main/java/com/qa/config/config.properties");
prop.load(fis);
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
现在我们的测试类代码修改之后如下。
package com.qa.tests;
import java.io.IOException;
importorg.apache.http.client.ClientProtocolException;
importorg.apache.http.client.methods.CloseableHttpResponse;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.qa.base.TestBase;
import com.qa.restclient.RestClient;
public class GetApiTest extends TestBase{
TestBase testBase;
String host;
String url;
RestClient restClient;
CloseableHttpResponse closeableHttpResponse;
@BeforeClass
public void setUp() {
testBase = new TestBase();
host = prop.getProperty("HOST");
url = host + "/api/users";
}
@Test
public void getAPITest() throws ClientProtocolException, IOException {
restClient = new RestClient();
closeableHttpResponse= restClient.get(url);
//断言状态码是不是200
int statusCode = closeableHttpResponse.getStatusLine().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 if(s.contains("[") || s.contains("]")) {
obj =((JSONArray)((JSONObject)obj).get(s.split("\\[")[0])).get(Integer.parseInt(s.split("\\[")[1].replaceAll("]", "")));
}
}
}
return obj.toString();
}
}
简单解释下上面的代码,主要是查询两种json对象的的值,第一种最简单的,这个json对象在整个json串的第一层,例如上面截图中的per_page,这个per_page就是通过jpath这个参数传入,返回的结果就是3. 第二种jpath的查询,例如我想查询data下第一个用户信息里面的first_name的值,这个时候jpath的写法就是data[0]/first_name,查询结果应该是Eve。
3.TestNG测试用例
下面,我们TestNG测试用例代码如下
package com.qa.tests;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qa.base.TestBase;
import com.qa.restclient.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 = prop.getProperty("HOST");
url = host + "/api/users?page=2";
}
@Test
public void getAPITest() throws ClientProtocolException, IOException {
restClient = new RestClient();
closeableHttpResponse = restClient.get(url);
//断言状态码是不是200
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
Assert.assertEquals(statusCode, RESPNSE_STATUS_CODE_200, "response status code is not 200");
//把响应内容存储在字符串对象
String responseString = EntityUtils.toString(closeableHttpResponse.getEntity(),"UTF-8");
//创建Json对象,把上面字符串序列化成Json对象
JSONObject responseJson = JSON.parseObject(responseString);
//System.out.println("respon json from API-->" + responseJson);
//json内容解析
String s = TestUtil.getValueByJPath(responseJson,"data[0]/first_name");
System.out.println(s);
}
}
运行测试结果:
[RemoteTestNG] detected TestNGversion 6.14.3
Eve
PASSED: getAPITest
你还可以多写几个jpath来测试这个json解析工具类。
String s = TestUtil.getValueByJPath(responseJson,"data[1]/id");
String s = TestUtil.getValueByJPath(responseJson,"per_page");
4.TestNG自带的测试断言方法
这里简单提一下TestNG的断言方法,我们一般测试都需要写断言的代码,否则这样的单元测试代码就没有意义。下面,我在statusCode和json解析的first_name进行断言。
package com.qa.tests;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qa.base.TestBase;
import com.qa.restclient.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 = prop.getProperty("HOST");
url = host + "/api/users?page=2";
}
@Test
public void getAPITest() throws ClientProtocolException, IOException {
restClient = new RestClient();
closeableHttpResponse = restClient.get(url);
//断言状态码是不是200
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
Assert.assertEquals(statusCode, RESPNSE_STATUS_CODE_200, "response status code is not 200");
//把响应内容存储在字符串对象
String responseString = EntityUtils.toString(closeableHttpResponse.getEntity(),"UTF-8");
//创建Json对象,把上面字符串序列化成Json对象
JSONObject responseJson = JSON.parseObject(responseString);
//System.out.println("respon json from API-->" + responseJson);
//json内容解析
String s = TestUtil.getValueByJPath(responseJson,"data[0]/first_name");
System.out.println(s);
Assert.assertEquals(s, "Eve","first name is not Eve");
}
}
经常使用的测试断言:
Assert.assertEquals(“现实结果”, "期待结果","断言失败时候打印日志消息");
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持谷谷点程序。
java接口自动化框架_java接口自动化测试框架及断言详解相关推荐
- java接口自动化监控_java接口自动化(三) - 手工接口测试到自动化框架设计之鸟枪换炮...
1.简介 上一篇宏哥介绍完了接口用例设计,那么这一章节,宏哥就趁热打铁介绍一下,接口测试工具.然后小伙伴们或者童鞋们就可以用接口测试工具按照设计好的测试用例开始执行用例进行接口手动测试了.关于手动测试 ...
- java接口自动化书籍_java接口自动化优化(一)
优化extentreports在线样式改为离线加载自己项目下的样式 主要解决extentreports在线加载失败问题 上篇文章介绍了通过testng编写用例后使用extentreports作为测试报 ...
- java 接口 返回值_java api返回值的标准化详解
api返回值的标准化 例如 {"status":200,"message":"操作成功","data":"{\ ...
- soapui工具_python接口自动化(四)--接口测试工具介绍(详解)
简介 "工欲善其事必先利其器",通过前边几篇文章的介绍,大家大致对接口有了进一步的认识.那么接下来让我们看看接口测试的工具有哪些. 目前,市场上有很多支持接口测试的工具.利用工具进 ...
- python接口自动化(四)--接口测试工具介绍(详解)
简介 "工欲善其事必先利其器",通过前边几篇文章的介绍,大家大致对接口有了进一步的认识.那么接下来让我们看看接口测试的工具有哪些. 目前,市场上有很多支持接口测试的工具.利用工具进 ...
- java语言链栈_Java语言实现数据结构栈代码详解
近来复习数据结构,自己动手实现了栈.栈是一种限制插入和删除只能在一个位置上的表.最基本的操作是进栈和出栈,因此,又被叫作"先进后出"表. 首先了解下栈的概念: 栈是限定仅在表头进行 ...
- java 队列已满_JAVA中常见的阻塞队列详解
在之前的线程池的介绍中我们看到了很多阻塞队列,这篇文章我们主要来说说阻塞队列的事. 阻塞队列也就是 BlockingQueue ,这个类是一个接 口,同时继承了 Queue 接口,这两个接口都是在JD ...
- java的匿名函数_JAVA语言中的匿名函数详解
本文主要向大家介绍了JAVA语言中的匿名函数详解,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助. 一.使用匿名内部类 匿名内部类由于没有名字,所以它的创建方式有点儿奇怪.创建格式如下: ...
- java 迭代器的优缺点_java迭代器和for循环优劣详解
在进行迭代的时候,程序运行的效率也是我们挑选迭代方法的重要原因.目前有三种迭代方法:for循环.迭代器和Foreach.前两者相信大家都非常熟悉,为了更加直观分析效率的不同,我们还加入Foreach一 ...
- java 检查bytebuf长度_Java学习笔记16-Netty缓冲区ByteBuf详解
Java学习笔记16-Netty缓冲区ByteBuf详解 Netty自己的ByteBuf ByteBuf是为解决ByteBuffer的问题和满足网络应用程序开发人员的日常需求而设计的. JDK Byt ...
最新文章
- C++ [](){} 匿名函数 lambda表达式
- Android开发工具——ADB(Android Debug Bridge) 二HOST端
- MySQL能够运行于多种操作系统平台_快速的掌握可以运行MySQL的操作系统
- 基于nginx实现反向代理
- 在區塊鏈上建立可更新的智慧合約(二)
- webService 使用CXF 实现简单的helloworld
- arcgis 圈选获取图层下点位_ArcGIS小技巧——提取面要素的质心点
- JS 判断URL中是否含有 http:// 如果没有则自动为URL加上
- 第 8 天 多线程与多进程
- sncr脱硝技术流程图_SNCR烟气脱硝技术工艺流程示意图
- UVA750 UVALive5358 8 Queens Chess Problem题解
- 【GYM-100889 C】Chunin Exam【左右手路径问题】
- 速领,阿里巴巴Java开发手册终极版
- 最新版火狐浏览器无法下载 firebug 和 firepath 插件的问题
- Windows系统删除文件时提示找不到该项目,无法删除时的解决办法
- oracle优化器analyzed,Oracle Optimizer:迁移到使用基于成本的优化器—–系列1.2-数据库专栏,ORACLE...
- HDU 5698:瞬间移动(排列组合)
- 格林尼治时间(GMT)格式化
- CANopen基本原理及其应用(二)——对象字典和通讯机制
- 王者归来,低代码的逆袭之路