Trying to set an option in a pytest.ini file dynamically. The option testpaths determines what directories pytest will gather tests from. I want to give users the ability to select test directory a
or b
.
conftest.py
The first hook creates a parser option. The second hook pulls the parser option reads the value and adds the testpath to the testpaths option under the config object.
@pytest.hookimpl()
def pytest_addoption(parser):
"""Creates a parser option"""
# Allows the user to select the test suite they want to run
parser.addoption("--suite", action="store", default="None"
, choices=['a', 'b']
, help="Choose which test suite to run.")
@pytest.hookimpl()
def pytest_configure(config):
print("Determining test directory")
suite = config.getoption("--suite")
if suite == "a":
config.addinivalue_line("testpaths", "tests/a")
elif suite == "b":
config.addinivalue_line("testpaths", "tests/b")
So if i run pytest --suite a
it should load all tests under the a test suite. It does not. It loads all tests like the option doesnt exist.