• 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的接口测试的更多相关文章

  1. python+requests+unittest执行自动化接口测试

    1.安装requests.xlrd.json.unittest库 <1>pip 命令安装: pip install requestspip install xlrdpip install ...

  2. 对比3种接口测试的工具:jmeter+ant;postman;python的requests+unittest或requests+excel

    这篇随笔主要是对比下笔者接触过的3种接口测试工具,从实际使用的角度来分析下3种工具各自的特点 分别为:jmeter.postman.python的requests+unittest或requests+ ...

  3. Python3 + requests + unittest接口测试

    一.缘 起 笔者最近完成了基于Python3 + requests + unittest的接口测试脚本,故在此做一下记录,于己为复盘,于彼为学习和参考 二.思 路 接口测试无非三步: 首先,造数据 - ...

  4. python之unittest框架实现接口测试实例

    python之unittest框架实现接口测试实例 接口测试的方法有很多种,具体到工具有postman,jmeter,fiddler等,但是工具的局限性是测试数据的组织较差,接口的返回工具的判断有限, ...

  5. 利用unittest+ddt进行接口测试(二):使用yaml文件管理测试数据

    知道ddt的基本使用方法之后,练习把之前用excel文件来维护的接口测试用例改用unittest+ddt来实现. 这里我选用yaml文件来管理接口参数,开始本来想用json,但是json无法添加注释, ...

  6. requests,unittest——多接口用例,以及需要先登录再发报的用例

    之前写过最简单的接口测试用例,本次利用unittest进行用例管理,并出测试报告,前两个用例是两个不同网页的get用例,第三个是需要登录才能访问的网页A,并在其基础上访问一个需要在A页面点开链接才能访 ...

  7. requests+unittest+ddt+xlrd+pymysql+BeautifulReport数据驱动

    # ddcapitestpython XXX接口自动化测试 # 一.数据驱动的思路 1.采用requests+unittest+ddt+xlrd+pymysql+BeautifulReport 2.r ...

  8. postman自动化接口测试

    背景描述 有一个项目要使用postman进行接口测试,接口所需参数有: appid: 应用标识: sign:请求签名,需要使用HMACSHA1加密算法计算,签名串是:{appid}${url}${st ...

  9. 通过实例介绍Android App自动化测试框架--Unittest

    1.为什么需要使用框架实现自动化测试 作为测试工程师,可能在代码能力上相比开发工程师要弱一点,所以我们在写脚本的时候就会相对容易的碰到更多的问题,如果有一个成熟的框架供给我们使用的话,可以帮助我们避免 ...

随机推荐

  1. 【其他】VS提示不一致的行尾

    应该是用不同的编辑器或平台编辑过同一个文件,比如Windows是\r\n,有的系统只有一个\n, 需要都统一,否则代码可能会堆成一堆.

  2. 【bzoj2318】Spoj4060 game with probability Problem 概率dp

    题目描述 Alice和Bob在玩一个游戏.有n个石子在这里,Alice和Bob轮流投掷硬币,如果正面朝上,则从n个石子中取出一个石子,否则不做任何事.取到最后一颗石子的人胜利.Alice在投掷硬币时有 ...

  3. NetScaler ‘Counters’ Grab-Bag!

    NetScaler ‘Counters’ Grab-Bag! https://www.citrix.com/blogs/author/andrewre/ https://www.citrix.com/ ...

  4. 【题解】HNOI2018转盘

    何学长口中所说的‘一眼题’……然而实际上出出来我大HN全省也只有一个人A…… 首先我们需要发现一个性质:我们永远可以在最后一圈去标记所有的物品.倘若我们反复转圈,那么这完全是可以省下来的.所以我们破环 ...

  5. [洛谷P4735]最大异或和

    题目大意:有一串初始长度为$n$的序列$a$,有两种操作: $A\;x:$在序列末尾加一个数$x$ $Q\;l\;r\;x:$找一个位置$p$,满足$l\leqslant p\leqslant r$, ...

  6. BZOJ1857:[SCOI2010]传送带——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1857 Description 在一个2维平面上有两条传送带,每一条传送带可以看成是一条线段.两条传送 ...

  7. 洛谷4578 & LOJ2520:[FJOI2018]所罗门王的宝藏——题解

    https://www.luogu.org/problemnew/show/P4578 https://loj.ac/problem/2520 有点水的. 先转换成图论模型,即每个绿宝石,横坐标向纵坐 ...

  8. 四连测Day3

    题目链接:https://pan.baidu.com/s/1_vsHfMI_qO-9IDxmFLkHfg 密码: uza8 T1: 小奥的一笔画,判连通性,查奇偶点即可 #include<ios ...

  9. HDU 2136 素数打表+求质数因子

    Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  10. Codeforces Round #332 (Div. 2)B. Spongebob and Joke

    B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standa ...