Repository navigation
Description
In some cases, a pytest_addoption() in conftest.py is not evaluated before the commandline is executed.
This seems to be the case if the conftest.py resides in an installed package (including tests) and you want to execute the test using
pytest --pyargs some_package --myoption
If possible, I would propose to ignore invalid options in a first run, until actually all relevant conftest.py have been imported and then re-evalute the invalid options.
Example:
The test is supposed to support a customized test option I added via pytest_addoption() in a conftest.py that I placed within the package:
folder tree:
$HOME/install/pytest_testing
$HOME/install/pytest_testing/__init__.py # empty
$HOME/install/pytest_testing/tests/__init__.py # empty
$HOME/install/pytest_testing/tests/conftest.py # contains pytest_addoption()
$HOME/install/pytest_testing/tests/test_package.py # contains a test_package()
Now, I am trying to run the test from somewhere, so not within the actual package folder tree (source code or installation) via
/somewhere > export PYTHONPATH=$HOME/install
/somewhere > pytest --pyargs pytest_testing -s
========================== session starts ========================================
platform linux -- Python 3.8.12, pytest-7.0.1, pluggy-1.0.0
rootdir: /somewhere
collecting ...
-----------------------------------------------------------
$HOME/install/pytest_testing/tests/conftest.py:
pytest_addoption: option added
-----------------------------------------------------------
-----------------------------------------------------------
collected 1 item
-----------------------------------------------------------
test_package: myoption = False
-----------------------------------------------------------
======================= 1 passed in 0.03s ==========================================
BUT
/somewhere > pytest --pyargs pytest_testing -s --myoption
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --myoption
inifile: None
rootdir: /somewhere
So, in the second case --myoption is evaluated before conftest.py has been. However the first call clearly shows that the option is correctly added, however too late.
Activity
RolfBippus commentedon Mar 7, 2022
I might add, that I do see the problem, that identifying arbitrary invalid options might not be possible.
In any case, I would then suggest to actually update the documentation in deprecating pytest_addoption() from any usage in conftest.py in tests integrated into a package. This would then not really make sense?!
RonnyPfannschmidt commentedon Mar 7, 2022
This usage is already deprecated
It's communication however is badly done
We may need to add additional warnings to help people figure this stuff
RolfBippus commentedon Mar 8, 2022
Thanks for the quick response!
But just for clarification:
Due to the above, I see indeed no sense in using pytest_addoption() to add a specific commandline option I want to use to influence a specific test within an installed package when testing the installation (and not possibly development code in a working repo).
Is that what is meant?
RonnyPfannschmidt commentedon Mar 8, 2022
i don't quite follow your assertion as it compresses a number of different things into a very terse sentence
its entirely fine to jsut ensure a entrypoint based pytest plugin and/or something more detailed is loaded
its a shortcoming of pytest that one cannot nicely tell it about testsuites in packages (as those practically need a conftest)
so the "local plugin" story needs work in general
ngie-eign commentedon Nov 20, 2023
I just hit this documentation gap when trying to workaround #3287 using a top-level package conftest.py file and custom arguments on the command-line.
The documentation recommends this pattern under How to invoke pytest :: Control skipping of tests according to command line option. I see that later versions (7.x and above) no longer have this section.
It would be helpful if there was a simple local plugin "recipe" for handling this. I need to confirm that this API is explicitly/loudly warned about and/or broken on 7.x (if it's not, I would consider the deprecation incomplete).