python+requests+json 接口测试思路示例

实际项目中用python脚本实现接口测试的步骤:

1 发送请求,获取响应 》》 2 提取响应里的数据,对数据进行必要的处理 》》 3 断言响应数据是否与预期一致

以豆瓣接口为例,做一个简单的接口测试吧。使用到的知识涉及requests库,json库。

1 发送请求,获取响应

python 复制代码
#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解析响应数据

python 复制代码
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 提取数据及数据处理

python 复制代码
'''取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 断言

python 复制代码
 '''断言'''
    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))

好了,简单的接口测试脚本完成。完整代码:

python 复制代码
#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))
相关推荐
qq_3449205612 分钟前
Qt事件过滤器
开发语言·c++·qt
执明wa28 分钟前
Android RecyclerView 多类型, 多种 Item
android·xml·开发语言·设计模式·android studio
程序员yu39 分钟前
数智码力:Python作用域完全梳理,全局变量局部变量不再混乱
开发语言·python·学习
北城笑笑1 小时前
Python Dev 02 & Tkinter webbrowser 实战,手写第一个 Python 桌面 GUI 小工具
人工智能·python·pip
CTA量化套保1 小时前
搜索“2026年交易工具推荐”时,先问清它要解决什么问题
人工智能·python
Ticnix1 小时前
答案一个字一个字往外蹦,聊天记录却一条都没存下来
python·agent
Ticnix1 小时前
"多数新闻站都禁止内嵌"——这句话我信了很久,直到测了 16 个站点
python·agent
不灭的黄金瞳1231 小时前
Java数据类型与变量
java·开发语言·intellij-idea
贝猫说python1 小时前
阿里云部署qwen 、第5步:阿里云服务器上大模型运行demo.py,固定提示词模板 拼接用户输入,输出符合要求格式结果
python