接⼝⾃动化·分享·第三篇·单接⼝的批量测试如何实现
⼀、痛点:⼀条测试数据对应⼀个测试⽅法
前⾯的章节中我们已经写代码实现了登录接⼝的处理调⽤,但是⼀个接⼝往往是需要多条测试⽤例才能完整的覆盖到每⼀种情况,针对于单接⼝多条测试⽤例需要执⾏的情况,该如何处理呢,可能很多⼈最先想到的就是:⼀条测试数据对应⼀个测试⽅法?下⾯有⼀个注册接⼝,我们基于此思路来编码实现接⼝的完整测试。
1) 接⼝业务:会员注册
2) 接⼝地址:
3) 接⼝参数:mobilephone+pwd
4) 接⼝响应:json格式的报⽂
  关于这个接⼝的更多详细的细节我们可以从公司提供的接⼝⽂档中获取,在公司⾥⾯做接⼝项⽬的测试,不管是⾃动化测试还是⾮⾃动化,都会有这样的⼀个⽂档描述接⼝项⽬中的每⼀个接⼝的细节,包括业务,接⼝地址,参数,响应报⽂中的字段以及格式,这是我们在测试前编写测试⽤例的重要参考⽂献,
⼤部分接⼝⽂档的撰写都⼤同⼩异,关于接⼝的⼀些重要信息⼀般都会涵盖到,⽐如以下接⼝⽂档关于注册接⼝的描述:
  现在我们整理得到⼀条正向⽤例和⼏条反向⽤例:
1.合格⼿机号,合格密码
2.重复以上的⼿机号,密码
3.传⼿机号,不传密码
4.不传⼿机号,传密码
5.不合格的⼿机号
6.合格的⼿机号,不合格密码
  编写测试类,每条⽤例,准备⼀个测试⽅法:test1-test6对应上⾯每种测试情况:
  test1⽅法(mobilephone:189********;pwd:123456):
@Test
public void test1(){
String restUrl = "119.23.241.154:8080/futureloan/mvc/api/member/register";
//1.创建post对象,以post⽅式提交接⼝请求
HttpPost httpPost = new HttpPost(restUrl);
//2.准备提交参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
BasicNameValuePair basicNameValuePair1 = new BasicNameValuePair("mobilephone", "189********");        BasicNameValuePair basicNameValuePair2 = new BasicNameValuePair("pwd", "123456");
params.add(basicNameValuePair1);
params.add(basicNameValuePair2);
//3.参数封装到请求体当中
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
System.out.println("method:"+Method());
//4.准备客户端
CloseableHttpClient httpClient = ateDefault();
//5.提交请求
CloseableHttpResponse httpResponse = ute(httpPost);
//6.解析接⼝返回数据,返回字符串
String result = Entity());
//7.输出结果到控制台验证数据
System.out.println("*********返回数据:"+result);
} catch (Exception e) {
/
/ TODO Auto-generated catch block
e.printStackTrace();
}
}
  test2⽅法(mobilephone:189********;pwd:123456)
@Test
public void test1(){
String restUrl = "119.23.241.154:8080/futureloan/mvc/api/member/register";
//1.创建post对象,以post⽅式提交接⼝请求
HttpPost httpPost = new HttpPost(restUrl);
//2.准备提交参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
BasicNameValuePair basicNameValuePair1 = new BasicNameValuePair("mobilephone", "189********");        BasicNameValuePair basicNameValuePair2 = new BasicNameValuePair("pwd", "123456");
params.add(basicNameValuePair1);
params.add(basicNameValuePair2);
//3.参数封装到请求体当中
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
System.out.println("method:"+Method());
//4.准备客户端
CloseableHttpClient httpClient = ateDefault();
//5.提交请求
CloseableHttpResponse httpResponse = ute(httpPost);
//6.解析接⼝返回数据,返回字符串
String result = Entity());
//7.输出结果到控制台验证数据
System.out.println("*********返回数据:"+result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
  test3⽅法(mobilephone:189********;pwd:空)
  @Test
public void test3(){
String restUrl = "119.23.241.154:8080/futureloan/mvc/api/member/register";
//1.创建post对象,以post⽅式提交接⼝请求
HttpPost httpPost = new HttpPost(restUrl);
//2.准备提交参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
BasicNameValuePair basicNameValuePair1 = new BasicNameValuePair("mobilephone", "189********");        BasicNameValuePair basicNameValuePair2 = new BasicNameValuePair("pwd", "");
params.add(basicNameValuePair1);
params.add(basicNameValuePair2);
//3.参数封装到请求体当中
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
System.out.println("method:"+Method());
//4.准备客户端
CloseableHttpClient httpClient = ateDefault();
//5.提交请求
CloseableHttpResponse httpResponse = ute(httpPost);
//6.解析接⼝返回数据,返回字符串
String result = Entity());
//7.输出结果到控制台验证数据
System.out.println("*********返回数据:"+result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
  test4⽅法(mobilephone:空;password:123456)
  @Test
public void test3(){
String restUrl = "119.23.241.154:8080/futureloan/mvc/api/member/register";
//1.创建post对象,以post⽅式提交接⼝请求
HttpPost httpPost = new HttpPost(restUrl);
//2.准备提交参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
BasicNameValuePair basicNameValuePair1 = new BasicNameValuePair("mobilephone", "");
BasicNameValuePair basicNameValuePair2 = new BasicNameValuePair("pwd", "123456");
params.add(basicNameValuePair1);
params.add(basicNameValuePair2);
//3.参数封装到请求体当中
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
System.out.println("method:"+Method());
/
/4.准备客户端
CloseableHttpClient httpClient = ateDefault();
//5.提交请求
CloseableHttpResponse httpResponse = ute(httpPost);
//6.解析接⼝返回数据,返回字符串
String result = Entity());
//7.输出结果到控制台验证数据
System.out.println("*********返回数据:"+result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
  test5⽅法(mobilephone:189;password:123456)
  @Test
public void test3(){
String restUrl = "119.23.241.154:8080/futureloan/mvc/api/member/register";
//1.创建post对象,以post⽅式提交接⼝请求
HttpPost httpPost = new HttpPost(restUrl);
//2.准备提交参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
BasicNameValuePair basicNameValuePair1 = new BasicNameValuePair("mobilephone", "189");        BasicNameValuePair basicNameValuePair2 = new BasicNameValuePair("pwd", "123456");
params.add(basicNameValuePair1);
params.add(basicNameValuePair2);
//3.参数封装到请求体当中
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
System.out.println("method:"+Method());
//4.准备客户端
CloseableHttpClient httpClient = ateDefault();
//5.提交请求
CloseableHttpResponse httpResponse = ute(httpPost);
//6.解析接⼝返回数据,返回字符串
接口文档怎么看
String result = Entity());
//7.输出结果到控制台验证数据
System.out.println("*********返回数据:"+result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
  test6(mobilephone:189********;password:123456789)
  @Test
public void test3(){
String restUrl = "119.23.241.154:8080/futureloan/mvc/api/member/register";
//1.创建post对象,以post⽅式提交接⼝请求
HttpPost httpPost = new HttpPost(restUrl);
//2.准备提交参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
BasicNameValuePair basicNameValuePair1 = new BasicNameValuePair("mobilephone", "189********");
BasicNameValuePair basicNameValuePair2 = new BasicNameValuePair("pwd", "123456789");
params.add(basicNameValuePair1);
params.add(basicNameValuePair2);
//3.参数封装到请求体当中
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
System.out.println("method:"+Method());
//4.准备客户端
CloseableHttpClient httpClient = ateDefault();
//5.提交请求
CloseableHttpResponse httpResponse = ute(httpPost);
//6.解析接⼝返回数据,返回字符串
String result = Entity());
//7.输出结果到控制台验证数据
System.out.println("*********返回数据:"+result);
} catch (Exception e) {
/
/ TODO Auto-generated catch block
e.printStackTrace();
}
}
执⾏完毕,我们可以看到六个测试⽅法都有响应数据返回。
说明这种设计是可以达到我们的⽬的,但是问题是什么?
我们看到每个⽅法的代码⼏乎是⼀模⼀样,除了测试数据。⽬前我们只设计了六条⽤例,试想,如果有七组,⼋组,九组...更多组的测试数据,那么将会有更多的重复代码,这样的设计简直就是灾难性的。
⼆、痛点解决
  上⾯已经将问题抛出,我们该如何去解决这个问题呢?
  第⼀种办法是我们⾃⼰去编码实现⼀条解决⽅案,但是成本太⾼,⽽且可能写出来的框架有很多问题。
  第⼆种办法就是去⼀些第三⽅的框架来帮助我们来解决这个问题,⽽testng正式我们想要的这样⼀个框架,testng的数据提供者技术就能帮我们优雅的解决这个痛点,我们准备⼀个测试⽅法就可以了。
测试⽅法代码:
  @Test(dataprrovider=”datas”)
public void test(String mobilephone,String pwd){
String restUrl = "119.23.241.154:8080/futureloan/mvc/api/member/register";
//1.创建post对象,以post⽅式提交接⼝请求
HttpPost httpPost = new HttpPost(restUrl);
//2.准备提交参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
BasicNameValuePair basicNameValuePair1 = new BasicNameValuePair("mobilephone", mobilephone);
BasicNameValuePair basicNameValuePair2 = new BasicNameValuePair("pwd", pwd);
params.add(basicNameValuePair1);
params.add(basicNameValuePair2);
//3.参数封装到请求体当中
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
System.out.println("method:"+Method());
//4.准备客户端
CloseableHttpClient httpClient = ateDefault();
//5.提交请求
CloseableHttpResponse httpResponse = ute(httpPost);
//6.解析接⼝返回数据,返回字符串
String result = Entity());
//7.输出结果到控制台验证数据
System.out.println("*********返回数据:"+result);
} catch (Exception e) {
/
/ TODO Auto-generated catch block
e.printStackTrace();
}
}
代码改动:
1) 测试⽅法上多了两个参数
2) 代码中直接绑定参数
数据提供者代码:
① 数据提供者的作⽤就是提供⼏组测试数据给测试⽅法,⽅法的返回值必须是⼆维数组,并且此⽅法要⽤@DataProvider来标注:
② 测试⽅法如果要引⽤数据提供者则必须指定数据提供者的名字:
  测试⽤例执⾏结果为:

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