[英]Exclude test from pytest_generate_tests
我有幾個用pytest編寫的測試文件:
test_foo.py :
class TestFoo:
def test_foo_one(self, current_locale):
# some actions with locale
# assert
def test_foo_two(self, current_locale):
# some actions with locale
# assert
test_bar.py :
class TestBar:
def test_bar_one(self, current_locale):
# some actions with locale
# assert
def test_bar_two(self, current_locale):
# some actions with locale
# assert
和conftest.py :
locales = ["da-DK", "de-DE", "en-GB", "en-US", "es-AR", "es-CO", "es-ES", "es-MX", "fi-FI"]
def pytest_generate_tests(metafunc):
metafunc.parametrize('current_locale', locales, scope='session')
它允許為每個語言環境運行測試。
現在,我想創建一個不需要區域設置的測試,並且該測試只能運行一次。 test_without_locales.py :
class TestNoLocales:
def test_no_locales(self):
# some actions with locale
# assert
它引發一個錯誤: ValueError:不使用參數'current_locale'
如何不使用current_locales編寫測試?
locales = ["da-DK", "de-DE", "en-GB", "en-US", "es-AR", "es-CO", "es-ES", "es-MX", "fi-FI"]
def pytest_generate_tests(metafunc):
if 'current_locale' in metafunc.fixturenames:
metafunc.parametrize('current_locale', locales, scope='session')
或者您可以通過執行以下操作使它更加流暢:
params = {"current_locale": ["da-DK", "de-DE", "en-GB", "en-US", "es-AR", "es-CO", "es-ES", "es-MX", "fi-FI"]}
def pytest_generate_tests(metafunc):
for k,v in params:
if k in metafunc.fixturenames:
metafunc.parametrize(k, v, scope='session')
之所以可行,是因為pytest以串行方式加載每個燈具,因此您可以一個一個地注入它們(如果您有多個參數,那就是)
至於將測試排除在外,@ pytest.mark.skip()裝飾器適合您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.