接⼝⾃动化框架(Pytest+request+Allure)前⾔:
接⼝⾃动化是指模拟程序接⼝层⾯的⾃动化,由于接⼝不易变更,维护成本更⼩,所以深受各⼤公司的喜爱。
接⼝⾃动化包含2个部分,功能性的接⼝⾃动化测试和并发接⼝⾃动化测试。
本次⽂章着重介绍第⼀种,功能性的接⼝⾃动化框架。
框架升级版请移步:接⼝⾃动化框架2-升级版(Pytest+request+Allure)
⼀、简单介绍
环境:Mac、Python 3,Pytest,Allure,Request
pytest==3.6.0
pytest-allure-adaptor==1.7.10(弃⽤)
pytest-rerunfailures==5.0
configparser==3.5.0
PyYAML==3.12
requests==2.18.4
simplejson==3.16.0
----------------------------------------
2020-4-30更新
pytest==5.3.1
allure-pytest==2.8.6
allure-python-commons==2.8.6
⚠ 注:pytest-allure-adaptor已弃⽤,改为allure-pytest;
安装allure-pytest时,需将pytest-allure-adaptor卸载
流程:读取Yaml测试数据-⽣成测试⽤例-执⾏测试⽤例-⽣成Allure报告
模块类的设计说明:
Request.py 封装request⽅法,可以⽀持多协议扩展(get\post\put)
Config.py读取配置⽂件,包括:不同环境的配置,email相关配置
Log.py 封装记录log⽅法,分为:debug、info、warning、error、critical
Email.py 封装smtplib⽅法,运⾏结果发送邮件通知
Assert.py 封装assert⽅法
run.py 核⼼代码。定义并执⾏⽤例集,⽣成报告
Yaml测试数据格式如下:
---
Basic:
dec: "基础设置"
parameters:
-
url: /settings/basic.json
data: slug=da1677475c27
header: {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko)\
Chrome/67.0.3396.99 Safari/537.36",
"Content-Type": "keep-alive"
}
⼆、代码结构与框架流程
1、代码结构见下图:
代码结构.png
2、框架流程见下图:
框架流程.jpg
三、详细功能和使⽤说明
1、定义配置⽂件config.ini
该⽂件中区分测试环境[private_debug]和正式环境[online_release]分别定义相关配置项,[mail]部分为邮件相关配置项
# http接⼝测试框架配置信息
[private_debug]
# debug测试服务
tester = your name
environment = debug
versionCode = your version
host = www.jianshu
loginHost = /Login
loginInfo = email=wang@user&password=123456
[online_release]
# release正式服务
tester = your name
environment = release
versionCode = v1.0
host = www.jianshu
loginHost = /Login
loginInfo = email=wang@user&password=123456
[mail]
#发送邮件信息
smtpserver = smtp.163
sender = test1@163
receiver = wang@user
username = wang@user
password = 123456
2、读取yaml测试数据后封装
yaml测试数据例⼦见第⼀节,⼀条接⼝可定义多条case数据,get_parameter为已封装好的读取yaml数据⽅法,循环读取后将多条case数据存在list中。
class Basic:
params = get_parameter('Basic')
url = []
data = []
header = []
for i in range(0, len(params)):
url.append(params[i]['url'])
data.append(params[i]['data'])
header.append(params[i]['header'])
3、编写⽤例
class TestBasic:
@allure.feature('Home')
@allure.severity('blocker')
@allure.story('Basic')
def test_basic_01(self, action):
"""
⽤例描述:未登陆状态下查看基础设置
"""
conf = Config()
data = Basic()
test = Assert.Assertions()
request = Request.Request(action)
host = conf.host_debug
req_url = '' + host
urls = data.url
params = data.data
headers = data.header
api_url = req_url + urls[0]
response = _request(api_url, params[0], headers[0])
assert test.assert_code(response['code'], 401)
assert test.assert_body(response['body'], 'error', u'继续操作前请注册或者登录.')
assert test.assert_time(response['time_consuming'], 400)
Consts.RESULT_LIST.append('True')
4、运⾏整个框架run.py
if __name__ == '__main__':
# 定义测试集
args = ['-s', '-q', '--alluredir', xml_report_path]
self_args = sys.argv[1:]
pytest.main(args)
cmd = 'allure generate %s -o %s' % (xml_report_path, html_report_path)
try:
linux mysql教程视频shell.invoke(cmd)
except:
<('执⾏⽤例失败,请检查环境配置')
raise
try:
mail = Email.SendMail()
mail.sendMail()
except:
<('发送邮件失败,请检查邮件配置')
raise
5、err.log实例
[ERROR 2018-08-24 09:55:37]Response body != expected_msg, expected_msg is {"error":"继续操作前请注册或者登录9."}, body is {"error":"继续操作前请注册或者[ERROR 2018-08-24 10:00:11]Response time > expected_time, expected_time is 400, time is 482.745
[ERROR 2018-08-25 21:49:41]statusCode error, expected_code is 208, statusCode is 200
6、Assert部分代码

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。