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": "錯誤的參數:少了渠道號"
}
]