APP接口自动化测试JAVA+TestNG之HTTP接口测试实例

发表于:2017-10-25 15:32

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:程序员    来源:51Testing软件测试网采编

  1.工程结构说明
  2.Common.java源码
  package findyou.Interface;
  import org.codehaus.jettison.json.JSONException;
  import org.codehaus.jettison.json.JSONObject;
  public class Common {
  /**
  * 解析Json内容
  *
  * @author Findyou
  * @version 1.0 2015/3/23
  * @return JsonValue 返回JsonString中JsonId对应的Value
  **/
  public static String getJsonValue(String JsonString, String JsonId) {
  String JsonValue = "";
  if (JsonString == null || JsonString.trim().length() < 1) {
  return null;
  }
  try {
  JSONObject obj1 = new JSONObject(JsonString);
  JsonValue = (String) obj1.getString(JsonId);
  } catch (JSONException e) {
  e.printStackTrace();
  }
  return JsonValue;
  }
  }
  3.getCityWeathe.java源码
  package findyou.Interface;
  import java.io.BufferedReader;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  public class getCityWeather {
  private String url="";
  public String geturl() {
  return url;
  }
  public String getHttpRespone(String cityCode) throws IOException {
  String line = "";
  String httpResults = "";
  url=("http://www.weather.com.cn/data/cityinfo/"
  + cityCode + ".html");
  try {
  HttpURLConnection connection = URLConnection
  .getConnection(url);
  DataOutputStream out = null;
  // 建立实际的连接
  connection.connect();
  out = new DataOutputStream(connection.getOutputStream());
  out.flush();
  out.close();
  BufferedReader reader = new BufferedReader(new InputStreamReader(
  connection.getInputStream()));
  while ((line = reader.readLine()) != null) {
  httpResults = httpResults + line.toString();
  }
  reader.close();
  // 断开连接
  connection.disconnect();
  } catch (Exception e) {
  e.printStackTrace();
  }
  return httpResults;
  }
  }
  4.URLConnection.java源码
  package findyou.Interface;
  import java.net.HttpURLConnection;
  import java.net.URL;
  public class URLConnection {
  public static HttpURLConnection getConnection(String url){
  HttpURLConnection connection = null;
  try {
  // 打开和URL之间的连接
  URL postUrl = new URL(url);
  connection = (HttpURLConnection) postUrl.openConnection();
  // 设置通用的请求属性
  connection.setDoOutput(true);
  connection.setDoInput(true);
  connection.setRequestMethod("GET");
  connection.setUseCaches(false);
  connection.setInstanceFollowRedirects(true);
  connection.setRequestProperty("Content-Type", "application/json");
  connection.setRequestProperty("Charset", "utf-8");
  connection.setRequestProperty("Accept-Charset", "utf-8");
  } catch (Exception e) {
  e.printStackTrace();
  }
  return connection;
  }
  }
  3.1.3 编写测试用例
  1.测试用例(常见"二"一般的写法)
  package findyou.testcase;
  import java.io.IOException;
  import org.testng.Assert;
  import org.testng.Reporter;
  import org.testng.annotations.Test;
  import findyou.Interface.Common;
  import findyou.Interface.getCityWeather;
  public class test {
  public String httpResult= null, weatherinfo= null, city=null,exp_city = null;
  public static String cityCode="";
  public static getCityWeather weather=new getCityWeather();
  @Test(groups = { "BaseCase"})
  public void getShenZhen_Succ() throws IOException{
  exp_city="深圳";
  cityCode="101280601";
  Reporter.log("【正常用例】:获取"+exp_city+"天气成功!");
  httpResult=weather.getHttpRespone(cityCode);
  Reporter.log("请求地址: "+weather.geturl());
  Reporter.log("返回结果: "+httpResult);
  weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
  city=Common.getJsonValue(weatherinfo, "city");
  Reporter.log("用例结果: resultCode=>expected: " + exp_city + " ,actual: "+ city);
  Assert.assertEquals(city,exp_city);
  }
  @Test(groups = { "BaseCase"})
  public void getBeiJing_Succ() throws IOException{
  exp_city="北京";
  cityCode="101010100";
  Reporter.log("【正常用例】:获取"+exp_city+"天气成功!");
  httpResult=weather.getHttpRespone(cityCode);
  Reporter.log("请求地址: "+weather.geturl());
  Reporter.log("返回结果: "+httpResult);
  weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
  city=Common.getJsonValue(weatherinfo, "city");
  Reporter.log("用例结果: resultCode=>expected: " + exp_city + " ,actual: "+ city);
  Assert.assertEquals(city,exp_city);
  }
  @Test(groups = { "BaseCase"})
  public void getShangHai_Succ() throws IOException{
  exp_city="上海";
  cityCode="101020100";
  Reporter.log("【正常用例】:获取"+exp_city+"天气成功!");
  httpResult=weather.getHttpRespone(cityCode);
  Reporter.log("请求地址: "+weather.geturl());
  Reporter.log("返回结果: "+httpResult);
  weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
  city=Common.getJsonValue(weatherinfo, "city");
  Reporter.log("用例结果: resultCode=>expected: " + exp_city + " ,actual: "+ city);
  Assert.assertEquals(city,exp_city);
  }
  }
  2.简化后的用例
  如何返回值格式与请求格式固定,用例优化如下
  package findyou.testcase;
  import java.io.IOException;
  import org.testng.Assert;
  import org.testng.Reporter;
  import org.testng.annotations.Test;
  import findyou.Interface.Common;
  import findyou.Interface.getCityWeather;
  public class test {
  public String httpResult= null, weatherinfo= null, city=null,exp_city = null;
  public static String cityCode="";
  getCityWeather weather=new getCityWeather();
  @Test(groups = { "BaseCase"})
  public void getShenZhen_Succ() throws IOException{
  exp_city="深圳";
  cityCode="101280601";
  resultCheck(cityCode, exp_city);
  }
  @Test(groups = { "BaseCase"})
  public void getBeiJing_Succ() throws IOException{
  exp_city="北京";
  cityCode="101010100";
  resultCheck(cityCode, exp_city);
  }
  @Test(groups = { "BaseCase"})
  public void getShangHai_Succ() throws IOException{
  exp_city="上海";
  cityCode="101020100";
  resultCheck(cityCode, exp_city);
  }
  public void resultCheck(String cityCode_str, String exp_city_str) throws IOException{
  Reporter.log("【正常用例】:获取"+exp_city_str+"天气成功!");
  httpResult=weather.getHttpRespone(cityCode_str);
  Reporter.log("请求地址: "+weather.geturl());
  Reporter.log("返回结果: "+httpResult);
  weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
  city=Common.getJsonValue(weatherinfo, "city");
  Reporter.log("用例结果: resultCode=>expected: " + exp_city_str + " ,actual: "+ city);
  Assert.assertEquals(city,exp_city_str);
  }
  }
  工程下载地址:http://pan.baidu.com/s/1mhyTNny 密码:11ft
  3.1.4 执行测试用例
  TestNG自动化测试系列实例,基本已完毕,Post方法由于篇幅问题,则再不贴出来了,了解了以上实例,Post方法没有太大问题。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号