pytest-dev/pytest · error · ValueError

no option named

Error message

no option named {name!r}

What it means

Raised by Config.getoption() when the requested option name is neither declared via pytest_addoption nor present on the config's option namespace, and no default was supplied. This is a user-facing API: plugins and conftest.py code call config.getoption('name') to retrieve CLI/ini values. The error is only raised when default is NOTSET and skip is False.

Solutions

  1. Check the option name matches the addoption dest (e.g., --my-flag has dest my_flag)
  2. Pass a default: config.getoption('name', default=False) to avoid the error for optional flags
  3. Use skip=True if the option being absent should skip the test rather than error: config.getoption('name', skip=True)
  4. Ensure the plugin that registers the option via pytest_addoption is installed and loaded

Example fix

# before
val = config.getoption('my_flag')

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

Strategy: try-catch

Validate before calling

# Check option is declared before calling getoption
declared = name in dir(config.option) if hasattr(config, 'option') else False
if not declared:
    print(f"Option {name!r} is not declared; consider adding a default or skip=True")

Try / catch

try:
    val = config.getoption('my_flag', default=False)
except ValueError:
    # Option not declared — handle gracefully
    val = False

Prevention

When it happens

Trigger: Calling config.getoption('some_flag') in a fixture, hook, or plugin where 'some_flag' was never registered with parser.addoption('--some-flag') in a pytest_addoption hook. The lookup getattr(self.option, name) raises AttributeError, default is NOTSET, and skip is False.

Common situations: Typo in the option name string. Referencing an option provided by a plugin that is not installed. Calling getoption before pytest_addoption has run. Forgetting to register the option in conftest.py.

Related errors


AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11). Data as JSON: /api/errors/dfef492135b9894f. Report an issue: GitHub.

Appendix: 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 0d6fbdeffa)