pytest-dev / pytest-dev/pytest

Custom plugins with required options break CLI parsing when using pytest.main()

Open
#11,240 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: config
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 via pytest.main(), plugins which add a required options no longer parse their options correctly. This doesn't happen when using pytest script as the runner

  • output of pip list from the virtual environment you are using

    Package        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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.