实际项目中用python脚本实现接口测试的步骤:
1 发送请求,获取响应 》》2 提取响应里的数据,对数据进行必要的处理 》》3 断言响应数据是否与预期一致
以豆瓣接口为例,做一个简单的接口测试吧。使用到的知识涉及requests库,json库。
1 发送请求,获取响应
#coding:utf-8
'''
dinghanhua
2018-11-10
接口返回数据为json类型,提取数据实例
'''import requests
import jsonq = '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: #打印出所有的keysprint(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 jsonq = '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: #打印出所有的keysprint(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))