pytest自定义动态添加描述信息动态Description
程序员文章站
2022-04-11 08:04:57
...
pytest自定义动态添加描述信息动态Description实现截图:
参考:https://www.cnblogs.com/yhleng/p/11225638.html
要动态传参__doc__内容也是可以的.可以通过__doc__动态修改描述.
普通方法: 方法名.__doc__='fixture parameters.'
实例方法: self.方法名.__func__.__doc__='fixture parameters.' **实例方法必须加__func__否则是只读的.**
# -*- coding: UTF-8 -*-
import pytest
import json
import requests
stations = json.loads(open('./param/StationParams.json', 'r',encoding='utf-8').read())
def setup():
print("setup_function:每个用例开始前都会执行1")
def teardown():
print("teardown_function:每个用例结束后都会执行")
@pytest.fixture(params=stations)
def stations(request):
return request.param
def station(stations):
# 添加描述信息
test_station.__doc__ = stations['desc']
requestParam = json.dumps(stations)
# 请求的链接
url = 'xxxx - 隐藏url'
print("请求参数:\n" + requestParam)
# 发送请求
r = requests.post(url, requestParam, headers={'Content-Type': 'application/json'})
print("返回结果:\n" + r.content.decode('utf-8'))
return r
def test_station(stations):
station(stations)
status = station(stations).status_code
responseDict = json.loads(station(stations).content)
assert status == 200
assert station(stations).content
assert int(responseDict['msgCode']) == 100
下面是参数:
[
{
"channel": "12qwe",
"stationType": 0,
"useRange": 1,
"version": "1.2",
"desc": "正确的参数:高铁自营站点"
},
{
"channel": "12qwe",
"stationType": 0,
"useRange": 2,
"version": "1.2",
"desc": "正确的参数:高铁联营站点"
},
{
"channel": "",
"stationType": 0,
"useRange": 2,
"version": "1.2",
"desc": "错误的参数:少了渠道号"
}
]