跨多个测试用例持久化 PyTest 数据参数
Persisting PyTest data parameters across multiple test cases
我有没有办法在同一 Python 脚本中跨多个测试用例保留“ref”参数?
此设置“有效”,但必须 copy/paste 每个测试用例的相同数据感觉不是最佳选择。
@pytest.mark.parametrize('ref', [
'crmpicco1872',
'crmpicco2001',
'crmpicco2008',
'crmpicco2013',
'crmpicco2021',
])
def test_data_directory_does_not_already_exist(ref):
datadir = f"/var/lib/data/sites/{ref}"
assert not os.path.exists(datadir)
@pytest.mark.parametrize('ref', [
'crmpicco1872',
'crmpicco2001',
'crmpicco2008',
'crmpicco2013',
'crmpicco2021',
])
def test_data_directory_is_created(logger_fixture, config_parser_fixture, clean_context_fixture, ref):
datadir = f"/var/lib/data/sites/{ref}"
clean_context_fixture.data = "data0"
clean_context_fixture.ref = ref
create_data = CreateData(logger_fixture, config_parser_fixture)
create_data.execute(clean_context_fixture)
assert os.path.exists(datadir)
shutil.rmtree(datadir)
我看到两种可能性:将参数提取到函数中并在 parametrize
:
中使用它
def my_params():
return [
'crmpicco1872',
'crmpicco2001',
'crmpicco2008',
'crmpicco2013',
'crmpicco2021'
]
@pytest.mark.parametrize('ref', my_params())
def test_data_directory_is_created(logger_fixture, config_parser_fixture, clean_context_fixture, ref):
...
或在 pytest_generate_tests
中设置参数,前提是您始终希望对 ref
参数使用相同的参数:
def pytest_generate_tests(metafunc):
if 'ref' in metafunc.fixturenames:
params = [
'crmpicco1872',
'crmpicco2001',
'crmpicco2008',
'crmpicco2013',
'crmpicco2021'
]
metafunc.parametrize('ref', params)
def test_data_directory_is_created(logger_fixture, config_parser_fixture, clean_context_fixture, ref):
...
请注意,这是我的想法,可能包含拼写错误。
我有没有办法在同一 Python 脚本中跨多个测试用例保留“ref”参数?
此设置“有效”,但必须 copy/paste 每个测试用例的相同数据感觉不是最佳选择。
@pytest.mark.parametrize('ref', [
'crmpicco1872',
'crmpicco2001',
'crmpicco2008',
'crmpicco2013',
'crmpicco2021',
])
def test_data_directory_does_not_already_exist(ref):
datadir = f"/var/lib/data/sites/{ref}"
assert not os.path.exists(datadir)
@pytest.mark.parametrize('ref', [
'crmpicco1872',
'crmpicco2001',
'crmpicco2008',
'crmpicco2013',
'crmpicco2021',
])
def test_data_directory_is_created(logger_fixture, config_parser_fixture, clean_context_fixture, ref):
datadir = f"/var/lib/data/sites/{ref}"
clean_context_fixture.data = "data0"
clean_context_fixture.ref = ref
create_data = CreateData(logger_fixture, config_parser_fixture)
create_data.execute(clean_context_fixture)
assert os.path.exists(datadir)
shutil.rmtree(datadir)
我看到两种可能性:将参数提取到函数中并在 parametrize
:
def my_params():
return [
'crmpicco1872',
'crmpicco2001',
'crmpicco2008',
'crmpicco2013',
'crmpicco2021'
]
@pytest.mark.parametrize('ref', my_params())
def test_data_directory_is_created(logger_fixture, config_parser_fixture, clean_context_fixture, ref):
...
或在 pytest_generate_tests
中设置参数,前提是您始终希望对 ref
参数使用相同的参数:
def pytest_generate_tests(metafunc):
if 'ref' in metafunc.fixturenames:
params = [
'crmpicco1872',
'crmpicco2001',
'crmpicco2008',
'crmpicco2013',
'crmpicco2021'
]
metafunc.parametrize('ref', params)
def test_data_directory_is_created(logger_fixture, config_parser_fixture, clean_context_fixture, ref):
...
请注意,这是我的想法,可能包含拼写错误。