Skip to content

Custom option (--some_path) fails when path exists #13411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
4 tasks done
adentinger opened this issue May 8, 2025 · 0 comments
Closed
4 tasks done

Custom option (--some_path) fails when path exists #13411

adentinger opened this issue May 8, 2025 · 0 comments

Comments

@adentinger
Copy link

  • a detailed description of the bug or problem you are having

When adding a custom option via pytest_addoption, passing a path that doesn't exist has it passed correctly, but when the path does exist, pytest fails. I'd have expected both to work. Seems like such a basic issue I really feel like I'm missing something obvious...

$ pytest -rP --some_path /usr/bin/nonexistent
# output "some_path: /usr/bin/nonexistent"
$pytest -rP --some_path /usr
# ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
# pytest: error: unrecognized arguments: --some_path
#   inifile: None
#   rootdir: /usr/bin
  • output of pip list from the virtual environment you are using

Package Version


iniconfig 2.1.0
packaging 25.0
pip 24.0
pluggy 1.5.0
pytest 8.3.5

  • pytest and operating system versions

Ubuntu, also Win11

  • minimal example if possible
mkdir foo
cd foo
python3 -m venv .venv
.venv/bin/python3 -m pip install pytest
mkdir tests
# tests/conftest.py
import pytest

def pytest_addoption(parser: pytest.Parser):
    parser.addoption(
        "--some_path",
        action="store",
        help="Path to something.",
        type=str,
    )

@pytest.fixture
def some_path(request) -> str:
    path_raw: str = request.config.getoption("--some_path")
    return path_raw
# tests/test_foo.py
def test_foo(some_path):
    print(f"some_path: {some_path}")

Then we are ready. Non-existent path:

$ .venv/bin/pytest -rP --some_path /usr/bin/hola
====================== test session starts ======================
platform linux -- Python 3.12.3, pytest-8.3.5, pluggy-1.5.0
rootdir: /home/me/foo
collected 1 item

tests/test_foo.py .                                       [100%]

============================ PASSES =============================
___________________________ test_foo ____________________________
--------------------- Captured stdout call ----------------------
some_path: /usr/bin/hola
======================= 1 passed in 0.00s =======================

Path that exists:

$ .venv/bin/pytest -rP --some_path /usr/bin
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --some_path
  inifile: None
  rootdir: /usr/bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants