![](/img/trans.png)
[英]How to apply parameterization for whole test suite rather than using it for individual single test case in pytest Selenium
[英]pytest test parameterization override
我目前正在使用pytest_generate_tests
參數化我的所有測試用例,這很好用。
我現在想做的是為特定測試覆蓋此行為。 如果我嘗試在測試本身上使用pytest.mark.parametrize
裝飾器, pytest.mark.parametrize
收到ValueError: duplicate
錯誤,這是可以理解的,因為我現在試圖在兩個地方對測試進行參數化。
有沒有辦法可以覆蓋這個測試用例的“默認”參數化?
我可以通過執行以下操作來實現這一點,但這是一種非常hacky的方法:
def pytest_generate_tests(metafunc):
fixture_modes = ['mode1', 'mode2']
if 'fixture' in metafunc.fixturenames:
fixture = metafunc.config.getoption('fixture')
if fixture:
fixture_modes = [fixture]
if metafunc.function.__name__ != 'func_to_skip':
metafunc.parametrize('fixture_mode', fixture_modes, indirect=True)
有一個更好的方法嗎?
例如,您可以檢查測試是否為fixture_mode
定義了自己的參數化標記
def pytest_generate_tests(metafunc):
fixture_modes = ['spam', 'eggs']
mark = metafunc.definition.get_closest_marker('parametrize')
if not mark or 'fixture_mode' not in mark.args[0]:
metafunc.parametrize('fixture_mode', fixture_modes, indirect=True)
現在,顯式參數化將覆蓋默認參數化:
def test_spam(fixture_mode):
...
@pytest.mark.parametrize('fixture_mode', (1, 2, 3))
def test_eggs(fixture_mode):
...
test_spam
將獲得默認參數化, test_eggs
自定義參數化:
test_mod.py::test_spam[spam]
test_mod.py::test_spam[eggs]
test_mod.py::test_eggs[1]
test_mod.py::test_eggs[2]
test_mod.py::test_eggs[3]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.