【发布时间】:2020-01-08 23:25:22
【问题描述】:
我的想法是我有一组条件,例如:正数、负数、偶数、奇数。
我有一堆测试,可以在这些条件的组合下运行('positive+even' or 'negative+odd')
但我只想对每个条件的任何值运行一个测试。例如:
正+偶数:
2
负数+奇数
-3
在这个例子中,我想标记测试,所以 pytest 会根据条件自动选择值并且只运行一次测试。
我正在考虑为每个条件创建一个夹具并将所有夹具传递给测试或使用 pytest_generate_tests。
http://doc.pytest.org/en/latest/example/parametrize.html#deferring-the-setup-of-parametrized-resources 这个例子几乎是我想要的,但我不想为每个数据库运行测试,而是希望它运行一次并支持诸如 any('oracle', 'Db2', ' 之类的组合条件mysql') + any('win', 'linux')。
能否请您提供有关执行此操作的更好方法的建议?它应该在复杂的夹具中实现还是使用pytest-tags插件+添加解析/处理conftest.py中的组合还是有其他方式
更新:
例子
@pytest.fixture(scope='function')
def connection(request):
oracle_dbs = ['db1', 'db2', 'db3_19c']
mysql_dbs = ['db1', 'db2', 'db3']
connect = Create_connector(request.tag[0])
if request.tag[1] == 'oracle':
if and not request.tag[2]:
connect.oracle(random.choice(oracle_dbs))
else:
connect.oracle(random.choice(db3_19c))
if request.tag[1] == 'mysql':
connect.mysql(random.choice(mysql_dbs))
return connect
@pytest.mark.tag('any_os', 'any_db')
def test_run_any_query(connection):
pass
@pytest.mark.tag('linux', 'oracle')
def test_run_query_lin_oracle(connection):
pass
@pytest.mark.tag('linux', 'oracle', '19c')
def test_run_query_lin_oracle(connection):
pass
解释:
1次测试,它通过标签找到一个合适的环境并只运行一次测试,而不是所有组合
2 测试,它也找到了一个合适的环境(所以它可以是 ubuntu/debian/gentoo/etc 和任何 oracle 版本),但它仍然应该只运行一次。
3测试,它只选择一个满足条件的oracle db并且只运行一次
我可以在 pytest_generate_tests(metafunc) 函数中获取标记或标签吗?
【问题讨论】:
-
请提供一两个示例函数以及在这种情况下测试的样子,以便我们更好地了解所需的行为
-
听起来很像hypothesis
-
@aws_apprentice,我已经更新了示例,现在可以理解了吗?