Skip to content

Confusing error messages when specifying a path that doesn't exist when specifying a custom argument #13607

@jmrgibson

Description

@jmrgibson

Hi,

  1. If the user specifies a path to collect on the CLI which doesn't exist, the error message is good.
  2. If the user specifies a path to collect on the CLI which doesn't exist AND they provide an argument that was added via a conftest.py , the resulting error message is confusing and misdirecting.

Example layout:

tests/
tests/conftest.py
tests/test_cool_thing.py
# contents of conftest.py
def pytest_addoption(parser) -> None:
    parser.addoption("--potato", default="")

Happy path example, with useful output

pytest tests/file_does_not_exist.py
ERROR: file or directory not found: tests/file_does_not_exist.py

However, adding the correct CLI arg leads to a confusing error message. The error message complains about an unrecognized argument, even though the argument is correct, and the root cause is that the specified path doesn't exist. Presumably with the missing path, conftest.py doesn't get loaded, and the -- argument is evaluated before the collection path.

pytest tests/file_does_not_exist.py --potato=yum
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --potato=yum

Tested on

  • ubuntu 20.04, python3.11
  • pytest-7.4.2, pytest-8.4.1

Wasted a few hours with other engineers today on this :)

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: configrelated to config handling, argument parsing and config filetopic: reportingrelated to terminal output and user-facing messages and errors

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions