接口自动化测试实例

前言

本篇主要对举例对国家气象局接口自动化测试进行讲解(Get请求及结果断言),以达到自动化测试入门目的,除了前两篇的一些了解外,需要有一定的JAVA知识(HTTP相关)。

1 待测接口说明

1.国家气象局天气预报接口

例:北京市天气

接口的址:http://www.weather.com.cn/data/cityinfo/101010100.html

请求方式:GET

请求结果:

{

"weatherinfo": {

"city": "北京",

"cityid": "101010100",

"temp1": "15℃",

"temp2": "5℃",

"weather": "多云",

"img1": "d1.gif",

"img2": "n1.gif",

"ptime": "08:00"

}

}

2.测试目标

请求对应cityid代码,返回的城市是否是预期城市。

2 新建JAVA工程

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.测试用例(常见"二"一般的写法)

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);

}

}

4 执行测试用例

TestNG自动化测试系列实例,基本已完毕,Post方法由于篇幅问题,则再不贴出来了,了解了以上实例,Post方法没有太大问题。后续如有时间看心情再上持续集成、自动化部署、自动化用例执行与测试报告输出博文~~~

EOF

本文作者:久曲建的测试窝

本文链接:https://www.cnblogs.com/longronglang/p/8710718.html

关于博主:评论和私信会在第一时间回复。或者直接私信我。

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!

优秀不够,你是否无可替代

软件测试交流QQ群:721256703,期待你的加入!!

欢迎关注我的微信公众号:软件测试君

java自动化测试案例简介_接口自动化测试实例相关推荐

  1. java接口测试框架搭建_接口自动化测试框架搭建

    一.原理及特点 参数放在XML文件中进行管理 用httpClient简单封装一个httpUtils工具类 测试用例管理使用了testNg管理,使用了TestNG参数化测试,通过xml文件来执行case ...

  2. java脚本接口自动化测试_接口自动化测试实践的记录

    接口测试实践的记录 在敏捷开发交付的流程中,自动化测试实际上被放在一个看起来挺重要的位置,而自动化测试中,接口测试是一个投入产出比比较高的 一种自动化测试的形式,而我自己也做了一个这样的脚手架一样的东 ...

  3. 对接接口文档_接口自动化测试框架设计思路

    接口自动化测试--框架设计思路 1 前言 之前文章跟大家分享了一下自己在接口自动化测试中进行测试准备的一些相关知识点,接下来本篇文章详细分享一下接口自动化框架设计的思路总结,希望能对初次探索接口自动化 ...

  4. 养宠物游戏java接口_接口--动物实例

    做一个饲养员给动物喂食物的例子体现JAVA中的面向对象思想,接口(抽象类)的用处 做一个饲养员给动物喂食物的例子体现JAVA中的面向对象思想,接口(抽象类)的用处 package com.softee ...

  5. 接口自动化测试平台Lego ------ 美团接口自动化测试超全实践【3000字长文】

    一.概述 1.1 接口自动化概述 众所周知,接口自动化测试有着如下特点: 低投入,高产出. 比较容易实现自动化. 和UI自动化测试相比更加稳定. 如何做好一个接口自动化测试项目呢? 我认为,一个&qu ...

  6. python接口自动化测试面试题_Python 接口自动化测试实战

    Python接口自动化测试实战 简介 本课程主要围绕Python相关库再服务端接口自动化测试中的应用展开介绍,重点讲解接口自动化基础.编写接口自动化脚本.框架原理.项目实战,此外还扩展介绍多用例管理与 ...

  7. 【1个月快速学习自动化测试】接口自动化测试(4) —— 接口自动化测试工具介绍

    接口测试的全称是应用程序编程接口(API)测试,从原理上来说,接口测试是模拟客户端向服务器端发送请求,然后检查能否获得正确的返回信息.接口测试用于测试RESTful API.SOAP Web服务,这些 ...

  8. python接口自动化测试框架pdf,Python接口自动化测试框架设计到开发完整版2019

    1:课程详细介绍.mp4 ; k& X* V: X! X% \; ]; u- V$ z7 L) h: C; h2:课程答疑.mp4+ J  K* q0 O  x + j( v2 n7 B7 s ...

  9. python自动化测试教程百度云盘_Python接口自动化测试框架实战视频教程百度云下载...

    主流的Fiddler.Requests.Unittest.Mock等接口测试工具/框架应用 进阶自动化框架设计开发 课程目录: 1-1 接口自动化测试从基础到框架-导学 1-2 接口基础知识回顾 1- ...

  10. 接口自动化测试_Python自动化测试学习路线之接口自动化测试「模块四」

    01.接口自动化测试概念 (接口测试的定义与意义;接口测试的实现方式和接口自动化的实现流程) 02.HTTP协议 (通信协议原理;HTTP与HTTP协议详解(请求,响应,请求方法,状态码).sessi ...

最新文章

  1. TinyBERT搜索: 比BERT快10倍,小20倍
  2. php检测http状态码,php回来HTTP状态码
  3. 二、【SAP-PM模块】PM模块(含服务采购)组织架构
  4. 电大法学本科计算机考试题,电大法学本科计算机形成性考核作业
  5. 数字图像处理与python实现_数字图像处理学习(2)—— 图像直方图均衡与图像匹配(python实现)...
  6. as转html5工具,将keras的h5模型转换为tensorflow的pb模型
  7. 48session的生命周期
  8. QEMU 3.0.0 新特性一览
  9. 【李宏毅机器学习】Introduction of ELMO、BERT、GPT(p25) 学习笔记
  10. 安卓应用安全指南 4.1.3 创建/使用活动 高级话题
  11. SEO能给独立站系统带来巨大的搜索流量吗?
  12. java ssm框架做增删改查,使用SSM框架组合实现增删改查的功能
  13. 计算机科学基础word实验一,大学计算机基础综合实验实验报告 参考模板(1)
  14. 各种数据库的分页查询
  15. web测试和app测试的区别你知道吗?
  16. 易了千明之易语言套装视频教程第四套辅助制作
  17. tinymce vue 部分工具不显示_vue项目中使用tinymce编辑器的步骤详解
  18. 2022年,4款值得用的报表工具软件推荐!
  19. 常见机器学习优点和缺点
  20. 一文详解立体匹配(附代码)

热门文章

  1. 我的Delphi开发经验谈
  2. [JS]动态添加删除ROW
  3. arthas:jvm调优神器
  4. libsvm 参数以及计算测试点相似度
  5. Netty源码分析(四):EventLoopGroup
  6. 谷歌被曝出滥用苹果后门收集用户数据
  7. 以Graphicslayer为管理组来管理Element.
  8. java编解码技术,netty nio
  9. Microsoft SQL Server 2008 安装图解(Windows 7)
  10. 【045】HTML初学