什么是接口测试
基于HTTP协议,通过工具或者代码仿照要求,检讨相应结果是否符合接口解释文档的一种测试
接口测试分类

系统对外的接口
系统内部的接口
接口测试的意义
安全性、实行效率、参与韶光、稳定性
HTTP协议的报文
要求报文:要求行(要求办法、网址、协议版本)、信息头、体
相应报文:状态行(协议版本、状态码、状态名称)、信息头、体
如何做接口测试
找开拓要接口解释文档
根据文档,设计测试用例
编写代码实现接口测试
定期实行接口测试代码
检讨天生的测试报告
编写测试总结报告
通过HttpClient仿照get要求的代码
1.创建HttpClient
CloseableHttpClient client = HttpClients.creatDefault();
2.构建网址
URI uri = new URIBuilder() .setScheme("协议") .setHost("域名") .setPort(端口号) .setPath("路径") .setParameter("参数的键", "参数的值") ...... .build();
3.创建要求
HttpGet get = new HttpGet(uri);
4.实行要求,获取相应
CloseableHttpResponse response = client.execute(get);
5.检讨相应结果
状态行
response.getStatusLine()
信息头
response.getAllHeaders()
体
response.getEntity()
练习
零七生活API - 供应免费接口调用平台(https://api.oick.cn/)
在浩瀚接口中选择自己感兴趣的来试一试,编写接口代码
这里我以毒鸡汤为例
可以得到接口地址,要求办法等其他信息
代码如下
import org.apache.http.Header;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.annotations.Test;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;public class DuJiTang { @Test public void testJiTang() throws URISyntaxException, IOException { CloseableHttpClient client = HttpClients.createDefault(); //接口地址 //https://api.oick.cn/dutang/api.php URI uri = new URIBuilder() .setScheme("https") .setHost("api.oick.cn") //端口号可省略不写 //.setPort(443) .setPath("dutang/api.php") //没有须要输入的参数 //.setParameter() .build(); HttpGet get = new HttpGet(uri); CloseableHttpResponse response = client.execute(get); System.out.println(response.getStatusLine()); Header[] headers = response.getAllHeaders(); for (Header h : headers){ System.out.println(h); } String responseText = EntityUtils.toString(response.getEntity()); System.out.println(responseText); }}
实行结果
POST要求前面讲的是get要求,接下来讲post要求
get要乞降post要求的差异
没有实质差异,只有协议约定的差异
1.参数位置不同:get要求的参数通过网址通报;post要求可以放在体中
2.安全性不同:对付用户来讲,post要求更安全,post要求的参数放在体中,可以更好的保护用户隐私;对付做事器来讲,get要求更安全,get要求一样平常用于查询操作,get要求天然幂等,不会为做事器引入脏数据
3.参数的长度限定不同:get要求的参数受限于浏览器的地址栏,一样平常在10kb旁边:post要求可以认为没有长度限定
4.发包次数不同:get要求只须要发一次包给做事器;post要求须要发两次包,第一次发要求行和信息头,做事器会返回一个临时状态码100,第二次发送体,再从做事器得到终极状态码
5.手工操作的影响:get要求有历史记录;get要求可以保存到收藏夹中;点击浏览器的退却撤退按钮,post要求会二次发送
常见的状态码
200:要求成功
301:永久重定向;希望用户往后采取新的网址,旧的网址即将过期
302:临时重定向;希望用户连续利用旧的网址
400:缺点的要求;一样平常要求的信息头写错了
401:未认证;未登录
402:暂时未利用,预留往后利用
403:禁止访问;登录,但是没权限
404:未找到;路径缺点
500:做事器内部缺点
501:未实现
502:网关缺点
503:做事器不可见
通过HttpClient仿照post要求
API数据接口_免费数据调用_API接口平台_API分类大全-聚合数据(https://www.juhe.cn/docs/index/otherid/1)
注册登录,获取自己的key
这里我以随机笑话接口为例,创建RandomJoke.java
1.创建一个HttpClient
CloseableHttpClient client = HttpClients.createDefault();
2. 构建网址
get要求推举利用URI
post要求推举利用String uri
String uri = " ";
3.创建要求
HttpPost post = new HttpPost(uri);
4. 把参数组合成列表
4.1创建一个新的,空的列表
List<NameValuePair> list = new ArrayList<NameValuePair>();
4.2为列表添加参数
list.add(new BasicNameValuePair("username","123"))list.add(new BasicNameValuePair("password","123456"))....
5. 把列表转成体的类型
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list);
6. 为要求设置体
post.setEntity(entity)
7. 实行要求,获取相应结果
CloseableHttpResponse response = client.execute(post)
8. 检讨相应结果
String responseText = EntityUtils.toString(response.getEntity())
代码整合
import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.testng.annotations.Test;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class RandomJoke { @Test public void testJoke() throws IOException { //创建HttpClient CloseableHttpClient client = HttpClients.createDefault(); //构建网址 String uri = "http://v.juhe.cn/joke/randJoke.php"; //创建要求 HttpPost post = new HttpPost(uri); //把参数组合成列表 List<NameValuePair> list = new ArrayList<NameValuePair>(); list.add(new BasicNameValuePair("key"," ")); //把列表转成体 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list); //为要求设置体 post.setEntity(entity); //实行要求,获取相应 CloseableHttpResponse response = client.execute(post); //检讨相应结果 String responseText = EntityUtils.toString(response.getEntity()); System.out.println(responseText); }}
实行结果
练习星座配对
代码如下
import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.testng.annotations.Test;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.List;public class StarPair { @Test public void testStar() throws IOException { CloseableHttpClient client = HttpClients.createDefault(); String uri = "http://apis.juhe.cn/xzpd/query"; HttpPost post = new HttpPost(uri); List<NameValuePair> list = new ArrayList<NameValuePair>(); list.add(new BasicNameValuePair("key"," ")); list.add(new BasicNameValuePair("men","双鱼")); list.add(new BasicNameValuePair("women","巨蟹")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"UTF-8"); post.setEntity(entity); CloseableHttpResponse response = client.execute(post); String responseText = EntityUtils.toString(response.getEntity()); System.out.println(responseText); }}
实行结果
接口关联接口关联也叫接口依赖
例:要想调用<获取微信服务器IP地址>这个接口,那么要依赖于<获取access_token>这个接口的返回值。也便是说,我们必须先调用<获取access_token>,得到acces_token的之后,才能调用微信"大众平台上的其他接口,那么,就可以说这两个接口有关联关系
实现步骤
打开pom.xml下载代码库
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.79</version> </dependency>
代码String responseText = EntityUtils.toString(response.getEntity()) 相称于 responseText = {"access_token":"ACCESS_TOKEN","expires_in":7200};
1.把相应文本转成json工具
JSONObject json = JSONObject.parseObject(responseText);
2.根据固定不变的键"access_token",提取动态变革的值"ACCESS_TOKEN"
String ACCESS_TOKEN; ACCESS_TOKEN = json.getString("access_token");
3.指定两个接口的实行顺序
@Test(dependsOnMethod="第一个方法名")
4.把access_token的值,传给第二个接口利用
.setParameter("access_token", ACCESS_TOKEN)
代码如下
import com.alibaba.fastjson.JSONObject;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 org.testng.annotations.Test;import java.io.IOException;public class WeiXinTest { String ACCESS_TOKEN; @Test public void getAccessToken() throws IOException { //调用第一个接口,提取accesstoken的值, 赋值给成员变量ACCESS_TOKEN CloseableHttpClient client = HttpClients.createDefault(); String uri = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&" + "appid= &secret= "; HttpGet get = new HttpGet(uri); CloseableHttpResponse response = client.execute(get); String responseText = EntityUtils.toString(response.getEntity()); //把responseText转成json工具 JSONObject json = JSONObject.parseObject(responseText); //从json工具中根据固定的键"access_token", 取对应的值 ACCESS_TOKEN = json.getString("access_token"); } @Test(dependsOnMethods = "getAccessToken") public void getServerIPAddress() throws IOException { //利用access_token,获取微信服务器所有的Ip列表 CloseableHttpClient client = HttpClients.createDefault(); String uri = "https://api.weixin.qq.com/cgi-bin/get_api_domain_ip?" + "access_token="+ ACCESS_TOKEN; HttpGet get = new HttpGet(uri); CloseableHttpResponse response = client.execute(get); String responseText = EntityUtils.toString(response.getEntity()); System.out.println(responseText); }}
实行结果
更直不雅观的代码
import com.alibaba.fastjson.JSONObject;import org.apache.http.Header;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.annotations.Test;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;public class WeiXinTest2 { String ACCESS_TOKEN; @Test public void getAccessToken() throws URISyntaxException, IOException { //创建HttpClient CloseableHttpClient client = HttpClients.createDefault(); //构建网址 URI uri = new URIBuilder() .setScheme("https") .setHost("api.weixin.qq.com") .setPort(443) .setPath("/cgi-bin/token") .setParameter("grant_type","client_credential") .setParameter("appid"," ") .setParameter("secret"," ") .build(); //创建要求 HttpGet get = new HttpGet(uri); //实行要求 CloseableHttpResponse response = client.execute(get); //检讨相应是否精确 //状态行 System.out.println(response.getStatusLine()); //信息头 Header[] headers = response.getAllHeaders(); for (Header h : headers){ System.out.println(h); } //体 String responseText = EntityUtils.toString(response.getEntity()); System.out.println(responseText); //把相应文本转成json工具 JSONObject json = JSONObject.parseObject(responseText); //根据键”access_token“,取对应的值 ACCESS_TOKEN = json.getString("access_token"); } @Test(dependsOnMethods = "getAccessToken") public void getServerIPAddress() throws URISyntaxException, IOException { //创建HttpClient CloseableHttpClient client = HttpClients.createDefault(); //构建网址 URI uri = new URIBuilder() .setScheme("https") .setHost("api.weixin.qq.com") .setPort(443) .setPath("/cgi-bin/get_api_domain_ip") .setParameter("access_token",ACCESS_TOKEN) .build(); //创建要求 HttpGet get = new HttpGet(uri); //实行要求,获取相应 CloseableHttpResponse response = client.execute(get); //检讨相应结果是否精确 //状态行 System.out.println(response.getStatusLine()); //信息头 Header[] headers = response.getAllHeaders(); for (Header h : headers){ System.out.println(h); } //体 String responseText = EntityUtils.toString(response.getEntity()); System.out.println(responseText); }}
实行结果