I'm trying to set up a custom command line option with the pytest_addoption hook in a non-root conftest.py file but it looks like that somehow my conftest isn't taken into account as an initial one if the testpaths is set through a configuration file (pyproject.toml in my example but it was also reproduced with a pytest.ini).
However, things work if the testpaths is explicitly set by command line argument.
Versions used are:
- Python 3.10.1
- Pytest 8.1.1 (also reproduced with 8.3.2)
$ pip freeze
exceptiongroup==1.2.2
iniconfig==2.0.0
packaging==24.1
pluggy==1.5.0
pytest==8.1.1
tomli==2.0.1
$ python --version
Python 3.10.13
$
The OS is a Linux environment:
$ uname -a
Linux vmonteco-desktop 6.8.1-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 16 Mar 2024 17:15:35 +0000 x86_64 GNU/Linux
$
I made a SO post on this issue yesterday and someone pointed out a similar issue (#10988). Could it be a regression?
MRE :
Command pytest --foo bar run in a directory containing the files:
- ./pyproject.toml:
[tool.pytest.ini_options]
testpaths = [
"my_project/my_tests",
]
./my_project/my_tests/conftest.py:
import pytest
def pytest_addoption(parser):
parser.addoption("--foo", action="store")
@pytest.fixture
def my_val(request):
return request.config.getoption("--foo")
./my_project/my_tests/test_foo.py:
def test_foo(my_val):
assert my_val == "foo"
- An empty
./my_project/my_tests/__init__.py file.
Expected behaviour:
An output similar to the one a simple workaround (giving the testpath explicitly by command line) gives:
$ pytest --foo bar my_project/my_tests
=============================== test session starts ===============================
platform linux -- Python 3.10.13, pytest-8.1.1, pluggy-1.5.0
rootdir: /home/vmonteco/code/MREs/pytest__addoption__pyproject_toml/01_adding_pyproject_toml
configfile: pyproject.toml
collected 1 item
my_project/my_tests/test_foo.py F [100%]
==================================== FAILURES =====================================
____________________________________ test_foo _____________________________________
my_val = 'bar'
def test_foo(my_val):
> assert my_val == "foo"
E AssertionError: assert 'bar' == 'foo'
E
E - foo
E + bar
my_project/my_tests/test_foo.py:2: AssertionError
============================= short test summary info =============================
FAILED my_project/my_tests/test_foo.py::test_foo - AssertionError: assert 'bar' == 'foo'
================================ 1 failed in 0.02s ================================
$
Observed behaviour:
I get the following usage error:
$ pytest --foo bar
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --foo
inifile: /home/vmonteco/code/MREs/pytest__addoption__pyproject_toml/02_attempt_to_solve/pyproject.toml
rootdir: /home/vmonteco/code/MREs/pytest__addoption__pyproject_toml/02_attempt_to_solve
$
I'm trying to set up a custom command line option with the
pytest_addoptionhook in a non-rootconftest.pyfile but it looks like that somehow my conftest isn't taken into account as an initial one if thetestpathsis set through a configuration file (pyproject.tomlin my example but it was also reproduced with apytest.ini).However, things work if the
testpathsis explicitly set by command line argument.Versions used are:
The OS is a Linux environment:
I made a SO post on this issue yesterday and someone pointed out a similar issue (#10988). Could it be a regression?
MRE :
Command
pytest --foo barrun in a directory containing the files:./my_project/my_tests/conftest.py:./my_project/my_tests/test_foo.py:./my_project/my_tests/__init__.pyfile.Expected behaviour:
An output similar to the one a simple workaround (giving the testpath explicitly by command line) gives:
Observed behaviour:
I get the following usage error: