Overriding collection paths via PyTest plugin

Viewed 81

With PyTest, you can limit the scope of test collection by passing directories/files/nodeids as command line arguments, e.g., pytest tests, pytest tests/my_tests.py and pytest tests/my_tests.py::test_1. Is it possible to override this behavior from within a plugin, i.e., to set them to something else programmatically?

So far I've attempted setting file_or_dir to my own list within config.option and config.known_args_namespace from the pytest_configure hook, but this appears to have no effect on anything.

1 Answers

You are probably looking for config.args:

# conftest.py

def pytest_configure(config):
    config.args = ['foo', 'bar/baz.py::test_spam']

Running pytest now will be essentially the same as running pytest foo bar/baz.py::test_spam. However, putting stuff in pytest.ini would be IMO a better solution:

# pytest.ini
[pytest]
addopts = foo bar/baz.py::test_spam
Related