Pytest - 自定义测试ID
# @Time: 2023/1/13 20:29
# @Authot: zzc
# @Project: pytestDemo3
# @File:test_a.py
import pytest
# @Time: 2023/1/13 20:29
# @Authot: zzc
# @Project: pytestDemo3
# @File:test_a.py
import pytest
@pytest.fixture(params=[0,'a'], ids = ['number', 'charactor'])
def fixture_a(request):
return request.param
def test_a(fixture_a):
print(fixture_a)
def idfn(fixture_value):
if fixture_value == 0:
return "eggs"
elif fixture_value == 1:
return False
elif fixture_value == 2:
return None
else:
return fixture_value
@pytest.fixture(params=[0,1,2,3], ids = idfn)
def fixture_b(request):
return request.param
def test_b(fixture_b):
print(fixture_b)
class C:
pass
@pytest.fixture(params=[(1,2), {'d':1},C()])
def fixture_c(request):
return request.param
def test_c(fixture_c):
print(fixture_c)
# 普通的参数化
@pytest.mark.parametrize('var', [0,1,2,3])
def test_d(var):
print(var)
命令行执行:pytest -s -v 结果如下
pycharm 执行结果如下:
总结:
- ids 参数可以接收一个指定的函数,用于生成测试ID
- 当测试ID指定为None 时,使用的是params 原先对应的值
- 当params中包含元组、字典、或者对象时·测试ID 使用的是fixture 函数名 + param在 params 中的下标
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/17050687.html