I would like to be able to dynamically parametrize a test function passed on the command line and override any previously set parametrize value.
Example of command line execution: python -m pytest --TEST_FUNCTION=test_dummy
test_file.py
:
@pytest.mark.parametrize('vm_name', ['vm1', 'vm2'])
def test_dummy(self, vm_name):
DEBUG(f'Hi {vm_name}!')
conftest.py
:
def pytest_generate_tests(metafunc):
test_function = metafunc.config.getoption('--TEST_FUNCTION')
if test_function is not None and metafunc.definition.name == test_function:
own_markers = []
for own_marker in metafunc.definition.own_markers:
if own_marker.args[0] == 'vm_name':
own_markers.append(pytest.Mark(name=own_marker.name, args=('vm_name', ['vm3', 'vm4']), kwargs=own_marker.kwargs))
else:
own_markers.append(own_marker)
metafunc.definition.own_markers = own_markers
This works fine and at execution time, test_dummy runs with vm3 and vm4 even though it's been marked with vm1/vm2 in the code, but the problem with my approach is that pytest.Mark()
is private and this may not work forever.
Is there another or a better way to override what may have already been set with the parametrize decorator with something else?