自动化测试===requests+unittest+postman的接口测试
- postman是一个跨平台的接口测试工具,下载链接在这里:https://www.getpostman.com/
- unittest是一个单元测试框架,python中安装:pip install unittest
- requests是一个发送http请求的库,安装:pip install requests
官方文档:http://docs.python-requests.org/en/master/user/quickstart/,
中文文档:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
以测试http://www.kuaidi100.com/query 为例:
有一个汇通快递的单号:350757819118
请求的url为http://www.kuaidi100.com/query?type=huitongkuaidi&postid=350757819118
用postman调试接口:

点击右上角的code,如下图:

以python--requests的格式复制:

复制得到的代码如下:
import requests
url = "http://www.kuaidi100.com/query"
querystring = {"type":"huitongkuaidi","postid":""}
headers = {
'cache-control': "no-cache",
'postman-token': "9794775a-f3eb-0322-1365-01b775fa9925"
}
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)
新建py文件,命名为kuaidi.py,对复制的代码稍作调整:
import requests
import unittest class KuaiDi(unittest.TestCase):
def test_huitong_api(self):
url = "http://www.kuaidi100.com/query"
querystring = {"type":"huitongkuaidi","postid":""}
headers = {
'cache-control': "no-cache",
'postman-token': "9794775a-f3eb-0322-1365-01b775fa9925"
}
response = requests.request("GET", url, headers=headers, params=querystring).json()
#print(response)
self.assertEqual(response['status'],'') #断言
self.assertEqual(response['message'],'ok') #断言 if __name__ == '__main__':
unittest.main()
运行结果:

举例测试:
import requests
import json
import unittest
import time
from HTMLTestRunner import HTMLTestRunner class MyTest(unittest.TestCase):
def setUp(self):
print("[+]start") def tearDown(self):
print("[+]end") def zhongtong(self,type = "zhongtong", id = ""):
self.url = "http://www.kuaidi100.com/query"
self.params = {
"type":type,
"postid":id
}
self.headers={'user-agent': 'my-app/0.0.1'}
r = requests.get(url = self.url, params = self.params , headers = self.headers)
print(r.status_code)
return r class ExpressInquiry(MyTest):
def test001_type_valid(self):
print("")
zhongtong = self.zhongtong(type = "shentong")
self.assertIn("快递公司参数异常",zhongtong.text) def test002_type_invalid(self):
print("")
zhongtong = self.zhongtong(type = "sssssssssssss")
self.assertIn("参数错误",zhongtong.text) def test003_id_valid(self):
print("")
id = self.zhongtong(id = "")
self.assertIn("交通工程学院菜鸟驿站",id.text) def test004_id_invalid(self):
print("")
id = self.zhongtong(id = "")
self.assertIn("参数错误",id.text) def test005_type_id_invalid(self):
print("")
type_and_id = self.zhongtong(type = "dads",id = "")
#print(type_and_id.url)
#print(type_and_id.text)
self.assertIn("参数错误",type_and_id.text) def test006_type_id_null(self):
print("")
null = self.zhongtong(type = "", id = "")
#print(null.url)
#print(null.text)
self.assertIn("参数错误",null.text) def suite():
now = time.strftime("%Y-%m-%d %H_%M_%S")
filename = './' + now + 'test_result.html'
fp = open(filename,'wb')
runner = HTMLTestRunner(stream = fp,
title = "快递查询接口测试报告",
description = "测试用例执行情况:") suite = unittest.TestSuite()
suite.addTest(ExpressInquiry("test001_type_valid"))
suite.addTest(ExpressInquiry("test002_type_invalid"))
suite.addTest(ExpressInquiry("test003_id_valid"))
suite.addTest(ExpressInquiry("test004_id_invalid"))
suite.addTest(ExpressInquiry("test005_type_id_invalid"))
suite.addTest(ExpressInquiry("test006_type_id_null"))
#unittest.TextTestRunner().run(suite) runner.run(suite)
fp.close() if __name__ == '__main__':
#unittest.main(exit = False , verbosity = 2)
#它是全局方法,把它屏蔽后,不在suite的用例就不会跑,exit = False表示中间有用例失败也继续执行;还有比较常用的verbosity=2,表示显示def名字 suite()
总结:
postman可以帮助完成一半的工作
unittest+requests可以实现断言,方便持续集成
自动化测试===requests+unittest+postman的接口测试的更多相关文章
- python+requests+unittest执行自动化接口测试
1.安装requests.xlrd.json.unittest库 <1>pip 命令安装: pip install requestspip install xlrdpip install ...
- 对比3种接口测试的工具:jmeter+ant;postman;python的requests+unittest或requests+excel
这篇随笔主要是对比下笔者接触过的3种接口测试工具,从实际使用的角度来分析下3种工具各自的特点 分别为:jmeter.postman.python的requests+unittest或requests+ ...
- Python3 + requests + unittest接口测试
一.缘 起 笔者最近完成了基于Python3 + requests + unittest的接口测试脚本,故在此做一下记录,于己为复盘,于彼为学习和参考 二.思 路 接口测试无非三步: 首先,造数据 - ...
- python之unittest框架实现接口测试实例
python之unittest框架实现接口测试实例 接口测试的方法有很多种,具体到工具有postman,jmeter,fiddler等,但是工具的局限性是测试数据的组织较差,接口的返回工具的判断有限, ...
- 利用unittest+ddt进行接口测试(二):使用yaml文件管理测试数据
知道ddt的基本使用方法之后,练习把之前用excel文件来维护的接口测试用例改用unittest+ddt来实现. 这里我选用yaml文件来管理接口参数,开始本来想用json,但是json无法添加注释, ...
- requests,unittest——多接口用例,以及需要先登录再发报的用例
之前写过最简单的接口测试用例,本次利用unittest进行用例管理,并出测试报告,前两个用例是两个不同网页的get用例,第三个是需要登录才能访问的网页A,并在其基础上访问一个需要在A页面点开链接才能访 ...
- requests+unittest+ddt+xlrd+pymysql+BeautifulReport数据驱动
# ddcapitestpython XXX接口自动化测试 # 一.数据驱动的思路 1.采用requests+unittest+ddt+xlrd+pymysql+BeautifulReport 2.r ...
- postman自动化接口测试
背景描述 有一个项目要使用postman进行接口测试,接口所需参数有: appid: 应用标识: sign:请求签名,需要使用HMACSHA1加密算法计算,签名串是:{appid}${url}${st ...
- 通过实例介绍Android App自动化测试框架--Unittest
1.为什么需要使用框架实现自动化测试 作为测试工程师,可能在代码能力上相比开发工程师要弱一点,所以我们在写脚本的时候就会相对容易的碰到更多的问题,如果有一个成熟的框架供给我们使用的话,可以帮助我们避免 ...
随机推荐
- WIN7系统插入蓝牙适配器经常断开问题
WIN7 ACER笔记本一台,蓝牙耳机一个,10块钱的蓝牙适配器一个 目的:可以在笔记本上用适配器与蓝牙耳机匹配 出现问题:1.有2个图标,一会左边感叹号,一会右边感叹号,必须有个存在感叹号 解决:第 ...
- 【刷题】HDU 4405 Aeroplane chess
Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled fr ...
- Android 自定义View消除锯齿实现图片旋转,添加边框及文字说明
先看看图片的效果,左边是原图,右边是旋转之后的图: 之所以把这个写出来是因为在一个项目中需要用到这样的效果,我试过用FrameLayout布局如上的画面,然后旋转FrameLayout,随之而来也 ...
- BZOJ1257:[CQOI2007]余数之和——题解+证明
http://www.lydsy.com/JudgeOnline/problem.php?id=1257 Description 给出正整数n和k,计算j(n, k)=k mod 1 + k mod ...
- HDU3480:Division——题解
http://acm.hdu.edu.cn/showproblem.php?pid=3480 将一列数划分成几个集合,这些集合的并集为该数列,求每个数列的(最大值-最小值)^2的和的最小值. 简单的d ...
- React Render Callback Pattern(渲染回调模式)
React Render Callback Pattern,渲染回调模式,其实是将this.props.children当做函数来调用. 例如: 要根据user参数确定渲染Loading还是Profi ...
- Codechef MARCH14 GERALD07加强版
强制在线不代表不能预处理! 考虑暴力怎么干? 开始n个联通块.now=n 不断加入边,如果连接两个联通块,--now 否则不动. 后者的前提是和[l,id-1]的边构成环 所以,我们考虑每个[l,r] ...
- Network LCA修改点权
Problem Description The ALPC company is now working on his own network system, which is connecting a ...
- Dynamic len(set(a[L:R])) UVA - 12345(这么过分一定要写博客)
给出一个有n个元素的数组,有以下两种操作:Q x y,求出区间[x,y)内不同元素的个数, M x y,把第x个元素的值修改为y.注意题目中的下标是从0开始的 这题超级超级坑 妈的一个水题找了几个小时 ...
- stout代码分析之七:Result类
Result类似于Option和Try类的组合,内部有三种状态 enum State { SOME, NONE, ERROR }; SOME表示Result对象有值 NONE表示Result对象值为空 ...