码迷,mamicode.com
首页 > 编程语言 > 详细

python+requests+json 接口测试实战

时间:2018-11-10 21:17:06      阅读:1492      评论:0      收藏:0      [点我收藏+]

标签:python脚本   exp   打印   summary   mat   网络   get   发送   格式化输出   

实际项目中用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)
request = requests.get(url) #请求并获取响应

 

2 json解析响应数据

jsonstr = json.loads(request.text) #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 = ‘‘
    for au in author:
        authors += au

    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)
request = requests.get(url) #请求并获取响应

jsonstr = json.loads(request.text) #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 = ‘‘
    for au in author:
        authors += au

    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网络数据采集1] #预期结果

    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脚本   exp   打印   summary   mat   网络   get   发送   格式化输出   

原文地址:https://www.cnblogs.com/dinghanhua/p/9940327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!