实际项目中用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 接口测试思路示例的更多相关文章

  1. python中json的操作示例

    先上一段示例 # -*- coding: cp936 -*- import json #构造一个示例数据,并打印成易读样式 j = {} j["userName"]="a ...

  2. Python操作JSON数据代码示例

    #!/usr/bin/env python import json import os def json_test(): return_dic = {} json_data = { 'appid':' ...

  3. python+requests实现接口测试 - get与post请求使用(转载)

    转自:http://www.cnblogs.com/nizhihong/p/6567928.html 简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Lic ...

  4. python+requests实现接口测试 - cookies的使用

    在很多时候,发送请求后,服务端会对发送请求方进行身份识别,如果请求中缺少识别信息或存在错误的识别信息, 会造成识别失败. 如一些需要用户登录以后才能访问的页面. import requests mya ...

  5. python+requests实现接口测试 - get与post请求使用

    简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 ...

  6. python+requests+excel 接口测试

    1.EXCEL文件接口保存方式,如图. 2.然后就是读取EXCEL文件中的数据方法,如下: import xlrd class readExcel(object): def __init__(self ...

  7. Python+requests+excel接口测试

    2018-06-14   17:00:13 环境准备: - Python 3.7 - requests库 - xlrd 1.创建Excel文件 2.读取Excel文件 import xlrd clas ...

  8. python+requests之接口测试

    最近学习接口测试,测试工具玩的差不多了,想用代码来尝试一下. 发现一个简单的库,requests 一:安装 pip install requests 二:使用 import requests url ...

  9. python+requests实现接口测试 - cookies的使用 (转载)

    出自:https://www.cnblogs.com/nizhihong/p/6699492.html 在很多时候,发送请求后,服务端会对发送请求方进行身份识别,如果请求中缺少识别信息或存在错误的识别 ...

随机推荐

  1. 在Eclipse中添加Servlet-api.jar的方法

    方法一: 正确的加载servlet-api.jar的方法如下: 1:右击项目工程名称 2:Properties 3:  Jvav Build Path 4:  Libraries 5:  Add Ex ...

  2. 安装配置flutter环境

    flutter 的中文文档 https://flutterchina.club/get-started/install/ github 地址 https://github.com/flutter/fl ...

  3. PIE SDK PCA融合

    1.算法功能简介 PCA 融合分三步实现,首先将多光谱数据进行主成分变换,然后用高分辨单波段替换第一主成分波段,最后进行主成份逆变换得到融合图像. PIE支持算法功能的执行,下面对PCA融合算法功能进 ...

  4. Linux-密码复杂度限制

    前言 设置一个复杂的密码,可以有效的提升系统的安全性.在Linux上有PAM(Pluggable Authentication Modules)里面有一个pam_cracklib.so来控制密码的复杂 ...

  5. TOJ 2888 Pearls

    Description In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces ...

  6. C#(Winform)的SaveFileDialog(文件保存对话框)控件使用

       #region 保存对话框   private void ShowSaveFileDialog()   {         //string localFilePath, fileNameExt ...

  7. HDU 5316——Magician——————【线段树区间合并区间最值】

    Magician Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  8. 深入理解JavaScript系列(37):设计模式之享元模式

    介绍 享元模式(Flyweight),运行共享技术有效地支持大量细粒度的对象,避免大量拥有相同内容的小类的开销(如耗费内存),使大家共享一个类(元类). 享元模式可以避免大量非常相似类的开销,在程序设 ...

  9. js实现选中当前元素并改变颜色(js、jq的各种循环)

    1.jq伪类选择器(:not)的使用 2.js jq运用数组循环 3.checkbox的选中循环事件 4.select的选中事件 <select class="ssss" o ...

  10. linux shell内置判断

    内置判断,成功的时候返回0,不成功返回非零 test  判断表达式 [ 判断表达式 ]       注意前后必须留空格哦 数值运算 -eq   等于 -ne   不等于 -gt     大于 -ge ...