pytest-dev / pytest-dev/pytest
Custom plugins with required options break CLI parsing when using pytest.main()
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
-
a detailed description of the bug or problem you are having
When running pytest viapytest.main(), plugins which add a required options no longer parse their options correctly. This doesn't happen when usingpytestscript as the runner -
output of
pip listfrom the virtual environment you are usingPackage Version -------------- ------- attrs 23.1.0 exceptiongroup 1.1.2 iniconfig 2.0.0 packaging 23.1 pip 21.2.4 pluggy 1.2.0 py 1.11.0 pytest 7.4.0 setuptools 58.1.0 toml 0.10.2 tomli 2.0.1 -
pytest and operating system versions
Python 3.9.9, Ubuntu 22.04, pytest 7.4.0, but I've been able to reproduce it all the way back to 5.4.3 -
minimal example if possible
runner.py, broken:import sys import pytest class Plugin: def pytest_addoption(self, parser, pluginmanager): parser.addoption("--param", required=True) def main(): sys.exit(pytest.main(plugins=[Plugin()])) if __name__ == "__main__": main()$ python runner.py --param foo ERROR: usage: runner.py [options] [file_or_dir] [file_or_dir] [...] runner.py: error: the following arguments are required: --param (via addopts config)conftest.py, works:import pytest def pytest_addoption(self, parser, pluginmanager): parser.addoption("--param", required=True)$ pytest ============================================================================================================================= test session starts ============================================================================================================================== platform linux -- Python 3.9.9, pytest-5.4.3, py-1.11.0, pluggy-0.13.1 rootdir: /tmp/bugreport collected 0 items ============================================================================================================================ no tests ran in 0.01s =============================================================================================================================
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue with the provided runner.py example and compare it with the working conftest.py setup. Trace how pytest.main() handles the required --param option and add a regression test showing that the option is parsed correctly; done means the runner.py invocation no longer reports --param as missing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli, testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100