[英]How to set dynamic marks parameter in pytest.param when using pytest_generate_tests()
[英]how to set the pytest.param id automatically?
我有一個參數化測試:
import pytest
@pytest.mark.parametrize('args', [
[1,2,3],
['a'],
[[],[1],['a']],
])
def test_bla(args):
assert all(args)
運行它時,我得到以下 output:
platform linux -- Python 3.6.12, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /home/bla/venv/bin/python
cachedir: .pytest_cache
metadata: {'Python': '3.6.12', 'Platform': 'Linux-4.15.0-132-generic-x86_64-with-Ubuntu-16.04-xenial', 'Packages': {'pytest': '6.0.2', 'py': '1.9.0', 'pluggy': '0.13.1'}, 'Plugins': {'metadata': '1.10.0'}}
rootdir: ...
plugins: metadata-1.10.0
collected 3 items
my_test.py::test_bla[args0] PASSED
my_test.py::test_bla[args1] PASSED
my_test.py::test_bla[args2] FAILED <---------- Note that this is not very descriptive :(
=================================================================================================== FAILURES ===================================================================================================
_______________________________________________________________________________________________ test_bla[args2] ________________________________________________________________________________________________
args = [[], [1], ['a']]
@pytest.mark.parametrize('args', [
[1,2,3],
['a'],
[[],[1],['a']],
])
def test_bla(args):
> assert all(args)
E AssertionError: assert False
E + where False = all([[], [1], ['a']])
my_test.py:8: AssertionError
=========================================================================================== short test summary info ============================================================================================
FAILED my_test.py::test_bla[args2] - AssertionError: assert False
========================================================================================= 1 failed, 2 passed in 0.11s ==========================================================================================
我想要的是在 output 中獲取實際使用的參數,例如:
my_test.py::test_bla[args0] PASSED
my_test.py::test_bla[args1] PASSED
my_test.py::test_bla[[], [1], ["a"]] FAILED <---------- Note the change here
這是我通過編輯parameterization
得到的:
pytest.param([[],[1],['a']], id='[], [1], ["a"]'),
pytest 有沒有辦法只取參數並使其成為id = str(param)
? (基本上自動化我剛剛在測試中自己編輯的內容)
您可以將ID function傳遞給 @pytest.mark.parametrize 裝飾器的ids
參數:
import pytest
@pytest.mark.parametrize('args', [
[1,2,3],
['a'],
[[],[1],['a']],
], ids=str)
def test_bla(args):
assert all(args)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.