The idea is I have a set of conditions like: positive, negative, even, odd.
And I have a bunch of tests, which can be run on combination of these conditions (‘positive+even’ or ‘negative+odd’)
But I want to run only one tests with any value from each condition. E.g.:
positive+even:
2
negative+odd
-3
In this example, I want to mark tests, so pytest automatically picks value depending on condition and run test only once.
I was thinking about creating a fixture for each condition and pass all fixture to tests or using pytest_generate_tests.
http://doc.pytest.org/en/latest/example/parametrize.html#deferring-the-setup-of-parametrized-resources this example is almost what I want, but instead of run the test for each DB, I’d like it to run once and with supporting of combination conditions like any(‘oracle’, ‘Db2’, ‘mysql’) + any(‘win’, ‘linux’).
Could you please advise about preferable way of doing this? should it be implemented in a complex fixture or using pytest-tags plugin + addition parsing/handling the combination in conftest.py or there are other ways
Update:
Example
@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
Explanation:
1 test, it finds one suitable environment by tags and run test only once, not all combinations
2 test, it finds one suitable environment as well (so it can be ubuntu/debian/gentoo/etc and any of oracle version), but still it should be run only once.
3 test, it picks only one oracle db that satisfies the condition and run it only once as well
Can I get markers or tags in pytest_generate_tests(metafunc) function?
2
Answers
It has been done as the below: test_numbers.py:
conftest.py:
There seem to be two concerns in your question:
Parameterize test
As you already linked in your question, you can parameterize tests.
e.g.
Find all “combinations”
You can simply extend the above by generating the parameter list. e.g.