Reputation: 9748
I want to add a command line parameter to my tests which have effect on (but not only on) test parameters. So I've added an option to my conftest.py
:
def pytest_addoption(parser):
parser.addoption("--target-name", action="store")
I know how to make fixtures depend on command line values but not how to do this for test parameters
@pytest.mark.parametrize(
"target_specific_data", json.parse(open("target-%s.json" % target_name)))
def test_foo(target_specific_data):
...
...
How can I do this with pytest?
Upvotes: 1
Views: 628
Reputation: 2561
Pytest considers value of parameters as list of values. You can make one function which would return your data and assign to them in list at run time or make the function return the list it self as below.
conftest.py
import os
def pytest_addoption(parser):
parser.addoption("--target-name", action="store")
def pytest_configure(config):
if config.getoption('target-name'):
os.environ["target-name"] = config.getoption('target-name')
test file
import json
import pytest
import os
def get_test_param():
return json.parse(open("target-{}.json".format(os.getenv('target-name')))
@pytest.mark.parametrize('target_specific_data', [get_test_param()]))
def test_foo(target_specific_data):
pass
Hope it would help !!
Upvotes: 1