Python+Requests接口自动化测试之引入Pytest框架
前段时间公司业务需要,准备把之前的接口自动化的脚本整理一下,把之前的冗长的代码整理成一个个的用例,这样代码的可读性就变得高了,而且相应的维护的成本降低。我们用到的是Pytest框架,关于框架的介绍及优点我这边就不赘述了,这边直接从实际的框架运用着手,相信大家对Pytest框架有个更加深刻的了解。
成都创新互联是一家专业提供安丘企业网站建设,专注与成都网站建设、成都做网站、H5开发、小程序制作等业务。10年已为安丘众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。
环境搭建
pip install pytest
安装好了后直接
pytest version
实际运用
import pytest
import requests
import json
import time
@pytest.fixture()
def feeds_get():
feeds = requests.get(url='https://api.xxxx.xxxx.tv/feeds/feed/mix?devid=328ExxA-5954-4C29-9x48-AE0ExxB722B4&pageSize=9&uid=70192',
headers={'version': '32'})
JsonToDic = json.loads(feeds.text)
return JsonToDic
@pytest.fixture()
def feed_detail(feeds_get):
feedList = feeds_get['data']['list']
return feedList
@pytest.fixture()
def feeds_parsing_date(feed_detail):
crtTimeSortList = []
for feed_count in range(0,9):
feedList = (feed_detail[feed_count])
crtTimeSortList.append ( feedList['crtTimeSort'] )
return crtTimeSortList
@pytest.fixture()
def feeds_get_pidlist(feed_detail):
pidlist = []郑州人流多少钱 http://www.hnmt120.com/
for feed_count in range(0,9):
feedList = (feed_detail[feed_count])
pidlist.append(feedList['pid'] )
return pidlist
@pytest.fixture()
def feeds_get_userid(feed_detail):
userid = []
for feed_count in range(0,9):
feedList = (feed_detail[feed_count])
userid.append(feedList['userid'])
return userid
@pytest.fixture()
def feeds_get_column(feed_detail):
columnlist = []
for feed_count in range(0,9):
feedList = (feed_detail[feed_count])
columnlist.append(feedList['column_id'] )
return columnlist
#服务器请求是否失败
def test_feeds_connect(feeds_get):
assert feeds_get
#账户ID是否有推送
def test_DeviceId(feeds_get):
assert feeds_get['msg'] != 'recom result is not enough'
#检查本次请求的feed数量是否为9
def test_feedcount(feed_detail):
assert len(feed_detail) == 9
#检查重复内容
def test_repeatcontent(feeds_get_pidlist):
assert len(feeds_get_pidlist) == len(set(feeds_get_pidlist))
#检查重复专栏
def test_repeatuser(feeds_get_column):
assert len(feeds_get_column) == len(set(feeds_get_column))
#检查重复用户
def test_repeatuser(feeds_get_userid):
assert len(feeds_get_userid) == len(set(feeds_get_userid))
#检查文章视频是否为三个月之内的
def test_feed_in90day(feeds_parsing_date):
now_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
t = int(now_time)
pass_time = t - 300000000
for feed_count in range(0,9):
assert int(feeds_parsing_date[feed_count]) > pass_time
大家可以发现是不是每一条自动化的case都可以写成一条assert语句,这样自动化代码的可读性是不是提高了而且可维护性也提高了。
分享名称:Python+Requests接口自动化测试之引入Pytest框架
文章源于:http://azwzsj.com/article/jppdoe.html