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!

 

转载于:https://www.cnblogs.com/dinghanhua/p/9940327.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值