pytest-dev/pytest · error · ValueError

no option named {name!r}

Error message

no option named {name!r}

What it means

Raised by Config.getoption (config/__init__.py:2058-2070) when the requested command-line option dest is neither present on the option object nor registered (no matching pytest_addoption), AND no default was supplied AND skip=False. The lookup against self.option raises AttributeError, which is converted to ValueError.

Source

Thrown at src/_pytest/config/__init__.py:2070

        :param default: Fallback value if no option of that name is **declared** via :hook:`pytest_addoption`.
            Note this parameter will be ignored when the option is **declared** even if the option's value is ``None``.
        :param skip: If ``True``, raise :func:`pytest.skip` if option is undeclared or has a ``None`` value.
            Note that even if ``True``, if a default was specified it will be returned instead of a skip.
        """
        name = self._parser._opt2dest.get(name, name)
        try:
            val = getattr(self.option, name)
            if val is None and skip:
                raise AttributeError(name)
            return val
        except AttributeError as e:
            if default is not NOTSET:
                return default
            if skip:
                import pytest

                pytest.skip(f"no {name!r} option found")
            raise ValueError(f"no option named {name!r}") from e

    def getvalue(self, name: str, path=None):
        """Deprecated, use getoption() instead."""
        return self.getoption(name)

    def getvalueorskip(self, name: str, path=None):
        """Deprecated, use getoption(skip=True) instead."""
        return self.getoption(name, skip=True)

    #: Verbosity type for failed assertions (see :confval:`verbosity_assertions`).
    VERBOSITY_ASSERTIONS: Final = "assertions"
    #: Verbosity type for test case execution (see :confval:`verbosity_test_cases`).
    VERBOSITY_TEST_CASES: Final = "test_cases"
    #: Verbosity type for failed subtests (see :confval:`verbosity_subtests`).
    VERBOSITY_SUBTESTS: Final = "subtests"

    _VERBOSITY_INI_DEFAULT: Final = "auto"

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Pass a default: config.getoption('my_opt', default=None) so missing options don't raise.
  2. Register the option in a conftest pytest_addoption hook (parser.addoption('--my-opt', dest='my_opt')).
  3. Fix spelling/casing of the dest name (or use the --flag form).
  4. Use skip=True if a missing option should skip the test rather than error.

Example fix

# before
val = config.getoption('my_opt')   # not declared

# after
val = config.getoption('my_opt', default=False)
Defensive patterns

Strategy: validation

Validate before calling

def safe_getoption(config, name, default=None):
    return config.getoption(name, default=default)

Type guard

def option_exists(config, name: str) -> bool:
    dest = config._parser._opt2dest.get(name, name)
    return hasattr(config.option, dest)

Try / catch

try:
    val = config.getoption(name)
except ValueError:
    val = None  # option not declared; degrade gracefully

Prevention

When it happens

Trigger: Calling config.getoption('my_opt') without a default for an option that no plugin declared via pytest_addoption. Also reachable via the deprecated getvalue(). With skip=True the call would pytest.skip instead.

Common situations: Typo in the option dest; querying an option from a plugin that is not installed/loaded; version upgrades removing/renaming options; forgetting that getoption takes the dest name (or --flag form), not an arbitrary string.

Related errors


AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04). Data as JSON: /data/errors/dfef492135b9894f.json. Report an issue: GitHub.