給定一個既有夾具又有引數化引數的測驗函式,如何獲得引數化引數的字典及其值?我可以使用request.node.name或訪問序列化的值串列os.environ.get('PYTEST_CURRENT_TEST'),但這并沒有給出相應的引數名稱。PyTest 列印它們,但我需要在自定義錯誤處理掛鉤中訪問它們。
@pytest.mark.parametrize('a', [False, True])
@pytest.mark.parametrize('b', [1, 2])
def test_foo(request, fixt_y, fixt_z, a, b): # fixt_y/z are some fixtures
print(request.node.name) # e.g., test_foo[0-1-False]
print(os.environ.get('PYTEST_CURRENT_TEST')) # e.g., test_file.py::test_foo[0-1-False] (call)
我想要的是以某種方式{'a': False, 'b': 1}進入test_foo。
uj5u.com熱心網友回復:
我不確定是否有更簡單的方法,但這有效
@pytest.mark.parametrize('a', [False, True])
@pytest.mark.parametrize('b', [1, 2])
def test_foo(request, fixt_y, fixt_z, a, b): # fixt_y/z are some fixtures
mark_param_names = [
mark.args[0]
for mark in reversed(request.node.keywords["pytestmark"])
]
# the params dict below contains the params names and their values
params = {p: request.getfixturevalue(p) for p in mark_param_names}
print(request.node.name) # e.g., test_foo[0-1-False]
print(os.environ.get('PYTEST_CURRENT_TEST')) # e.g., test_file.py::test_foo[0-1-False] (call)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/368892.html
上一篇:我在索引資料幀時遇到了麻煩
