自动化测试===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.为什么需要使用框架实现自动化测试 作为测试工程师,可能在代码能力上相比开发工程师要弱一点,所以我们在写脚本的时候就会相对容易的碰到更多的问题,如果有一个成熟的框架供给我们使用的话,可以帮助我们避免 ...
随机推荐
- 2018 杭电多校3 - M.Walking Plan
题目链接 Problem Description There are $$$n$$$ intersections in Bytetown, connected with $$$m$$$ one way ...
- BZOJ4736 温暖会指引我们前行(LCT+最大生成树)
类似于瓶颈路,满足条件的路径一定在温度的最大生成树上,那么就是一个LCT维护MST的裸题了. #include<iostream> #include<cstdio> #incl ...
- 转:Simple Introduction to Dirichlet Process
来源:http://hi.baidu.com/vyfrcemnsnbgxyd/item/2f10ecc3fc35597dced4f88b Dirichlet Process(DP)是一个很重要的统计模 ...
- 【题解】CF#280 C-Game on Tree
感觉对期望也一无所知……(:′⌒`)╮(╯﹏╰)╭ 一直在考虑怎么dp,最后看了题解——竟然是这样的???[震惊]但是看了题解之后,觉得确实很有道理…… 我们可以考虑最后答案的组成,可以分开计算不同的 ...
- bzoj 3132: 上帝造题的七分钟 (二维树状数组)
推推公式,最后变成四个东西的前缀和 然后不知道为什么一直wa,数据在本地测是没有错的& 好心的管理员还给了某位p党大神a了的代码,感人肺腑(虽然还是没发现到底我的程序是问题) var f1,f ...
- MyBatis代码生成工具mybatis-generator在Myeclipse10中的使用
一.在MyEclipse安装目录下新建myPlugin目录,如下图所示: 二.将 mybatis.zip 里面的文件放在MyEclipse的dropins目录下,如下图所示: 三.在Myeclipse ...
- 仅此一文让你明白ASP.NET MVC 之View的显示
有些人要问题,为什么我要学框架?这里我简单说一下,深入理解一个框架,给你带来最直接的好处: 使用框架时,遇到问题可以快速定位,并知道如何解决: 当框架中有些功能用着不爽时,你可以自由扩展,实现你想要的 ...
- UBOOT启动内核过程
1.摘要 (1).启动4步骤第一步:将内核搬移到DDR中第二步:校验内核格式.CRC等第三步:准备传参第四步:跳转执行内核(2).涉及到的主要函数是:do_bootm和do_bootm_linux(3 ...
- a标签nest问题,即a标签里面嵌套a标签
方法一:使用div模拟a,监听click事件 方法二:使用<object>标签包裹内部a标签 <div style="width: 200px;height: 200px; ...
- AngularJs 中的CheckBox前后台交互
前台页面: <div class="form-group"> <label for="CompanyName" class="col ...