my question is - is it possible to use return value from fixture as a value in parametrize? The problem is - I'd like to dynamically get possible values (for example, available systems on a virtual server) for parametrize. I can access these when a virtual server is created by one of the fixtures. Tests look like this (pseudo-code-ish):
[conftest.py]
@pytest_fixture(scope='session')
def test_server(request):
test_server = Server([default_params])
test_server.add()
def fin():
test_server.delete()
request_addfinalizer(fin)
return test_server()
[tests.py]
def test_basic_server(test_server):
systems = test.server.get_available_systems()
for system in systems:
test_server.install(system)
test_server.run_checks()
test_server.uninstall(system)
def test_other(test_server):
[other tests]
etc
This way, one server is added for each session, then all tests run on it, and after session ends, server is removed. But is there a way to get the available systems in @pytest.mark.parametrize without explicitly listing them (statically as a list in parametrize), using the method from server that is added when the session begins? That way each system would run in a separate test.
I tried using test_server in another fixture and then returning the list (the same way test_server is returned by test_server fixture, but I cannot use that as a value in parametrize - since decorator is evaluated before the test_server fixture is called in any test, and getting the list depends on test_server fixture.
This would be ideal:
[tests.py]
@pytest.mark.parametrize('system',[systems_list <- dynamically generated
when the server is created])
def test_basic_server(test_server,system):
test_server.install(system)
test_server.run_checks()
test_server.uninstall(system)
This is just a very basic example, in my tests I need to parametrize based on multiple scenarios and values and I end up with giant arrays when I do it statically.
But the principle remains the same - basically: can I call the fixture before the first test using this fixture runs, or how can pytest.mark.parametrize() access fixture values?