我有几个用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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
和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')
Run Code Online (Sandbox Code Playgroud)
它允许为每个区域设置运行测试.
现在,我想创建一个我不需要locales的测试,它必须只运行一次. test_without_locales.py:
class TestNoLocales:
def test_no_locales(self):
# some actions with locale
# assert
Run Code Online (Sandbox Code Playgroud)
它引发了一个错误: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')
Run Code Online (Sandbox Code Playgroud)
或者你可以通过这样做使它更加光滑:
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')
Run Code Online (Sandbox Code Playgroud)
这是因为pytest按顺序加载每个fixture,所以你可以逐个注入它们(如果你有多个param,那就是)
至于排除测试运行,@ pytest.mark.skip()装饰器为你.
| 归档时间: |
|
| 查看次数: |
297 次 |
| 最近记录: |