python+requests+json 接口测试思路示例
实际项目中用python脚本实现接口测试的步骤:
1 发送请求,获取响应 》》2 提取响应里的数据,对数据进行必要的处理 》》3 断言响应数据是否与预期一致
以豆瓣接口为例,做一个简单的接口测试吧。使用到的知识涉及requests库,json库。
1 发送请求,获取响应
#coding:utf-8
'''
dinghanhua
2018-11-10
接口返回数据为json类型,提取数据实例
''' import requests
import json q = 'python'
count = 3
url = 'https://api.douban.com/v2/book/search?q={0}&count={1}'.format(q,count)
response = requests.get(url) #请求并获取响应
2 json解析响应数据
jsonstr = json.loads(response.text) #json解析响应文本
#或者jsonstr = response.json() '''解析后的数据格式'''
print('响应解析后的类型:',type(jsonstr)) #dict
print('响应解析后的键值对个数:',len(jsonstr)) #字典键值对个数
for key in jsonstr: #打印出所有的keys
print(key ,end=' ')
3 提取数据及数据处理
'''取json串里的值'''
books = jsonstr['books'] #取books对应的值
# print(type(books)) #list 数组
print('books共有%d本书'%len(books)) #数组元素个数 for book in books: #编辑books取每本书的信息
# print(type(book)) # book的类型
# for key in book: # book的keys
# print(key)
'''取出所需的字段'''
index = books.index(book) #索引
NO = str(index+1) #第几本书
average= book['rating']['average'] author = book['author'] #author是数组,可能有多个作者
authors = ','.join(author) pubdate = book['pubdate']
title = book['title']
author_intro = book['author_intro']
summary = book['summary']
price = book['price'] '''格式化输出'''
print('NO.{NO}\n书名:{title}\n出版日期:{pubdate}\n平均分:{average}\n定价:{price}\n'
'作者:{author}\n{author_intro}\n内容简介:{summary}'.format(title = title,
NO = NO,
pubdate = pubdate,
author = authors,
author_intro = author_intro,
average = average,
price = price,
summary = summary))
4 断言
'''断言'''
expectedtitle = ['Python编程:从入门到实践','利用Python进行数据分析','Python基础教程'] #预期结果(接口数据会变,根据实际情况添加预期结果) if title == expectedtitle[index]:
print('test pass')
else:
print('test fail. The expected title is %s,but the actual title is: %s.'%(expectedtitle[index],title))
好了,简单的接口测试脚本完成。完整代码:
#coding:utf-8
'''
dinghanhua
2018-11-10
接口返回数据为json类型,提取数据实例
''' import requests
import json q = 'python'
count = 3
url = 'https://api.douban.com/v2/book/search?q={0}&count={1}'.format(q,count)
response = requests.get(url) #请求并获取响应 jsonstr = json.loads(response.text) #json解析响应文本
#jsonstr = response.json() '''解析后的数据格式'''
print('响应解析后的类型:',type(jsonstr)) #dict
print('响应解析后的键值对个数:',len(jsonstr)) #字典键值对个数
for key in jsonstr: #打印出所有的keys
print(key ,end=' ') '''取json串里的值'''
books = jsonstr['books'] #取books对应的值
# print(type(books)) #list 数组
print('books共有%d本书'%len(books)) #数组元素个数 for book in books: #编辑books取每本书的信息
# print(type(book)) # book的类型
# for key in book: # book的keys
# print(key)
'''取出所需的字段'''
index = books.index(book) #索引
NO = str(index+1) #第几本书
average= book['rating']['average'] author = book['author'] #author是数组,可能有多个作者
authors = ','.join(author) pubdate = book['pubdate']
title = book['title']
author_intro = book['author_intro']
summary = book['summary']
price = book['price'] '''格式化输出'''
print('NO.{NO}\n书名:{title}\n出版日期:{pubdate}\n平均分:{average}\n定价:{price}\n'
'作者:{author}\n{author_intro}\n内容简介:{summary}'.format(title = title,
NO = NO,
pubdate = pubdate,
author = authors,
author_intro = author_intro,
average = average,
price = price,
summary = summary)) '''断言'''
expectedtitle = ['Python编程:从入门到实践','利用Python进行数据分析','Python基础教程'] #预期结果 if title == expectedtitle[index]:
print('test pass')
else:
print('test fail. The expected title is %s,but the actual title is: %s.'%(expectedtitle[index],title))
the end!
python+requests+json 接口测试思路示例的更多相关文章
- python中json的操作示例
先上一段示例 # -*- coding: cp936 -*- import json #构造一个示例数据,并打印成易读样式 j = {} j["userName"]="a ...
- Python操作JSON数据代码示例
#!/usr/bin/env python import json import os def json_test(): return_dic = {} json_data = { 'appid':' ...
- python+requests实现接口测试 - get与post请求使用(转载)
转自:http://www.cnblogs.com/nizhihong/p/6567928.html 简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Lic ...
- python+requests实现接口测试 - cookies的使用
在很多时候,发送请求后,服务端会对发送请求方进行身份识别,如果请求中缺少识别信息或存在错误的识别信息, 会造成识别失败. 如一些需要用户登录以后才能访问的页面. import requests mya ...
- python+requests实现接口测试 - get与post请求使用
简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 ...
- python+requests+excel 接口测试
1.EXCEL文件接口保存方式,如图. 2.然后就是读取EXCEL文件中的数据方法,如下: import xlrd class readExcel(object): def __init__(self ...
- Python+requests+excel接口测试
2018-06-14 17:00:13 环境准备: - Python 3.7 - requests库 - xlrd 1.创建Excel文件 2.读取Excel文件 import xlrd clas ...
- python+requests之接口测试
最近学习接口测试,测试工具玩的差不多了,想用代码来尝试一下. 发现一个简单的库,requests 一:安装 pip install requests 二:使用 import requests url ...
- python+requests实现接口测试 - cookies的使用 (转载)
出自:https://www.cnblogs.com/nizhihong/p/6699492.html 在很多时候,发送请求后,服务端会对发送请求方进行身份识别,如果请求中缺少识别信息或存在错误的识别 ...
随机推荐
- nodejs 休眠 / 暂停执行指定毫秒
//************** 暂停 / 休眠指定毫秒值 *********************************// milliSeconds 暂停 / 休眠多少毫秒async func ...
- postgresql数据库primary key约束/not null约束/unique约束及default值的添加与删除、列的新增/删除/重命名/数据类型的更改
如果在建表时没有加primary key约束.not null约束.unique约束.default值,而是创建完表之后在某个字段添加的话 1.primary key约束的添加与删除 给red_pac ...
- SpringBoot---开发的热部署
1.模板的热部署 在SpringBoot中,模板引擎的页面默认是开启缓存的: 如果修改了页面的内容,则刷新不到修改后的页面: 可以在application.properties中关闭模板引擎的缓存: ...
- Java基础22-Static关键字
1.static关键字 public class Test{ public static void main(String[] args){ Persion p1=new Persion(); Per ...
- HRBUST 1909——理工门外的树——————【离线处理,差分前缀和】
理工门外的树 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %lld , %llu Java class n ...
- node.js获取命令参数
假如有个加密程序test.js,不想每次加密的时候都修改代码,直接通过控制台输入参数 var createHash = require('sha.js') var sha1 = createHash( ...
- 进程和程序(Process and Program)
原出处:http://oss.org.cn/kernel-book/ch04/4.1.htm ----------------------------------个人理解分割线------------ ...
- 2.storm的安装
1.前提是linux系统已经安装了上一篇讲的Zookeeper和jdk[1.7及以上版本]还有python[centos已经自带,2.6及以上版本] 2.解压storm压缩包 sudo tar -zx ...
- Maven学习篇一:eclipse构建运行maven web项目
1.new->other->maven project->next 2.选择创建简单项目(或者直接去掉勾,在后面选择maven-archetype-webapp) 3.设置坐标,名称 ...
- Django——stark组件
stark组件是仿照django的admin模块开发的一套组件,它的作用是在网页上对注册的数据表进行增删改查操作. 一.配置 1.创建stark应用,在settings.py中注册stark应用 st ...