目录

接口测试笔记

接口测试笔记

4、接口测试自动化

接口自动化概述

HttpClient

HttpClient开发过程

创建Java工程

新建libs库目录

HttpClient 工具下载及引入

  • 工程中引入jar包

Get请求

HttpGet方法—发起Get请求

创建HttpClient对象

  • CloseableHttpClient httpclient = HttpClients.createDefault();

创建带请求地址的HttpGet对象

  • HttpGet httpGet = new HttpGet(“http://xxxxxx”);
  • //ClassicHttpRequest get = ClassicRequestBuilder.get(url).build();

执行HttpGet请求,获得响应

  • client.execute(get, response -> {})

获取响应实体

  • HttpEntity entity = response.getEntity();

获取响应内容

  • EntityUtils.toString(entity);

断开连接

  • response.close();
  • httpclient.close();

请求Header类型:Content-Type=application/json

skulist-1测试用例详解

用例:获取所有商品的sku列表成功

skulist-2测试用例详解

用例:获取goodsId=1的商品sku信息成功

https://i-blog.csdnimg.cn/direct/06f66e19901a411691cb76234084974c.png

https://i-blog.csdnimg.cn/direct/7c0a21ee905442a291a459f364039bbf.png

skulist-3测试用例详解

用例:获取goodsId=2147483648的商品sku信息失败

https://i-blog.csdnimg.cn/direct/d24bb278f769453b98541db0897832d3.png

HttpGet用法总结

注意事项:

  • 请求地址中有多个参数用&符号连接,例如http://aaaaa?id=1&name=tom
  • 请求参数如包含非英文字符,需要encode转码

例如:

  • URLEncoder.encode("{"pId":"123456"}", “UTF-8”);
  • String url3 = “http://localhost:8899/common/getTransportFee?id=1&addressDetail=”;
  • String ulr3Param = URLEncoder.encode(“浙江省_杭州市_滨江区”, “UTF-8”);

POST

HTTP请求格式-POST方法

请求(Request)

请求行POST / xinhu / index.php?a = check&m =login HTTP/1.1
请求头Accept : t ext/ html,application / xhtml+xml Accept-Encoding: gzip , deflate Accept-Language:zh-CN,zh;q =0.8,en-US;q=0.5,en;q=0.3 Connection:keep-alive Host : localhost:8032
请求正文adminuser =YWRtaW4%3A&=123456&rempass=0&button=& jmpass = false&device =1517376146707&adminpass=MTIzNDU2

响应(Response)

状态行HTTP1.1 200 OK
响应头Connection : Keep-Alive Content-Encoding : gzip Content-Length : 1234 Content-Type : text/ html;charset =utf-8 Date : Mon, 05 Feb 2018 02:43:40 GMT
响应正文{" success":true,“face”:“http ://localhost:8032/ xinhu /upload/face/1.jpg”}

创建HttpClient对象

  • CloseableHttpClient  httpclient = HttpClients.createDefault();

创建带请求地址的HttpPost 对象

  • HttpPost httpPost = new HttpPost(“ ”);

设置HttpPost对象Header属性

  • httpPost.setHeader(“Content-Type”,“application/json”);

设置HttpPost 参数

  • StringEntity entity = new StringEntity(“para”,“utf-8”);
  • httpPost.setEntity(entity);

执行HttpPost请求,获取post请求的响应

  • httpclient.execute(httpPost,resp ->{});

获取响应实体

  • HttpEntity entity = response.getEntity();

获取响应内容

  • EntityUtils.toString(entity);

断开连接

  • response.close();
  • httpclient.close();

请求Header类型:Content-Type=application/json

https://i-blog.csdnimg.cn/direct/c21a45ec57f540288fd6af9a53512f1f.png

login-1测试用例详解

https://i-blog.csdnimg.cn/direct/b5764edc6cba4233aeb8645ca112a860.png

https://i-blog.csdnimg.cn/direct/401974e517684cf897e31c20133281f9.png

响应结果

{“message”:“success”,“code”:200}

HttpClient登录模拟总结

注意事项:

  • 根据具体登录请求选择HttpEntity具体类型(HttpEntity   的两个实现类: StringEntity和 UrlEncodedFormEntity)
  • 登录请求的Content-Type需要正确设置
  • 如果不想使用同一个HttpClient对象传递登录信息,可以考虑对需要登录信息请求分别设置Cookie:

httpPost.setHeader(“Cookie”," mindsparktb_232530392=true; mindsparktbsupport_232530392=true");

HttpClient设置代理
  • HttpHost proxy = new HttpHost(“127.0.0.1”, 8888);
  • CloseableHttpClient client = HttpClients.custom().setProxy(proxy).build();

引入测试框架

测试框架:TestNG

测试验证点、用例集的组织、测试报告

TestNG简介
  • The next generation of unit testing
  • Cedric Beust
  • 基于Junit、Nunit并支持注解、数据驱动、多线程执行等特性的Java测试框架
TestNG基础:注解(Annotation)

JDK5引入注解,TestNG用以方便的标注测试方法和组件

  • @Test 标注测试方法
  • @BeforeTest 标注全部测试方法 执行前 需要执行的方法
  • @AfterClass 标注测试类全部方法 执行之后 需执行的方法
  • @DataProvider 数据驱动

注意:TestNG执行测试方法之前,都会重新实例化测试类

https://i-blog.csdnimg.cn/direct/b779b6e2f2a348f58bf19bc8293004e2.png

TestNG基础:断言(Assert)

org.testng.Assert

  • fail 直接失败测试用例
  • assertTrue    判断是否为true
  • assertNull    判断是否为null
  • assertEquals    判断是否相等

https://i-blog.csdnimg.cn/direct/a096d257206044649f9bf0c019cc429a.png

TestNG基础:Test属性使用

TestNG基础:@DataProvider

https://i-blog.csdnimg.cn/direct/e2cc34a3a5f9499e890902732e59b56f.png

TestNG基础:DataProvider属性使用
  • 使用数据驱动,复用测试方法

https://i-blog.csdnimg.cn/direct/e2df5cd515f94ac09522861666cef08d.png

TestNG基础:testng.xml用例集
  • 指定测试类

https://i-blog.csdnimg.cn/direct/c561c8d94df342d68c335c2d0483994f.png

TestNG自带测试报告

https://i-blog.csdnimg.cn/direct/e130f28c04e9426b93c609d857dc4c34.png

TestNG+ReportNG报告

https://i-blog.csdnimg.cn/direct/7e38d453ba844f0c95cffaab886ff82c.png

Allure–自动化测试报告生成

https://i-blog.csdnimg.cn/direct/e193f1c4339f402ea023e41fd46eb96e.png

TestNG+FreeMarker测试报告

https://i-blog.csdnimg.cn/direct/bf07b54e45b14a70861f2cd90ea38e75.png

接口测试结果验证

如何验证

Java中解析常用的类库有FastJson、JSON-Java、Gson等

解析JSON数据

  • fastjson2-2.0.39.jar

验证JSON数据正确

  • TestNG、Assert

解析JSON

https://i-blog.csdnimg.cn/direct/1ab681d6c1cc4a08909ffd1f76c804b7.png

面向场景的接口自动化测试

用例回顾

用例步骤–登录成功

https://i-blog.csdnimg.cn/direct/60fd7d02e26b43be91aaf08f62de2e25.png

用例步骤-查询收货地址

用例步骤-计算运费

https://i-blog.csdnimg.cn/direct/90cbc01f1a61468085febd2181186424.png

用例步骤-提交订单

https://i-blog.csdnimg.cn/direct/200c92af73de4b05a35d197332a2efe9.png

https://i-blog.csdnimg.cn/direct/6ab05c1a866b41bcb960c3d57b03f448.png

场景测试总结

注意事项:

  • 使用TestNG的BeforeClass和AfterClass去初始化和关闭HttpClient对象
  • 使用dependsOnMethods属性关联场景的接口

https://i-blog.csdnimg.cn/direct/7cc4f7a70ecc407b88f88dffbd892ab5.png

注意事项:

  • 根据具体场景分析关联的接口,使用TestNG功能来安排用例执行顺序
  • 使用成员变量来传递接口返回数据
  • 在不同的HttpClient中间显示传递Cookie
返回Cookie

CookieStore cookie =new BasicCookieStore();

httpClient =HttpClients.custom().

setDefaultCookieStore(cookie).build();

设置Cookie

httpClient =HttpClients.custom().

setDefaultCookieStore(cookie).build();