pytest-dev/pytest · error · RuntimeError

Unrecognized runpytest option: {self._method}

Error message

Unrecognized runpytest option: {self._method}

What it means

Raised by Pytester.runpytest when self._method is neither 'inprocess' nor 'subprocess'. The _method is normally set by the Pytester factory based on the --runpytest command-line option or defaults to 'inprocess'. Seeing this indicates the internal method field was set to an unexpected value, almost always via a custom/monkeypatched Pytester or a bug in plugin code that constructs one.

Source

Thrown at src/_pytest/pytester.py:1219

            sys.stdout.write(out)
            sys.stderr.write(err)

        assert reprec.ret is not None
        res = RunResult(
            reprec.ret, out.splitlines(), err.splitlines(), instant.elapsed().seconds
        )
        res.reprec = reprec  # type: ignore
        return res

    def runpytest(self, *args: str | os.PathLike[str], **kwargs: Any) -> RunResult:
        """Run pytest inline or in a subprocess, depending on the command line
        option "--runpytest" and return a :py:class:`~pytest.RunResult`."""
        new_args = self._ensure_basetemp(args)
        if self._method == "inprocess":
            return self.runpytest_inprocess(*new_args, **kwargs)
        elif self._method == "subprocess":
            return self.runpytest_subprocess(*new_args, **kwargs)
        raise RuntimeError(f"Unrecognized runpytest option: {self._method}")

    def _ensure_basetemp(
        self, args: Sequence[str | os.PathLike[str]]
    ) -> list[str | os.PathLike[str]]:
        new_args = list(args)
        for x in new_args:
            if str(x).startswith("--basetemp"):
                break
        else:
            new_args.append(
                "--basetemp={}".format(self.path.parent.joinpath("basetemp"))
            )
        return new_args

    def parseconfig(self, *args: str | os.PathLike[str]) -> Config:
        """Return a new pytest :class:`pytest.Config` instance from given
        commandline args.

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Do not set _method manually; let the fixture decide based on --runpytest.
  2. Use only 'inprocess' or 'subprocess' as the method value.
  3. Pass the --runpytest=inprocess|subprocess command-line option instead of poking internals.
  4. Update or remove the third-party plugin that sets an unsupported method.

Example fix

// before
pytester._method = "inline"  # invalid
pytester.runpytest()
// after
pytester._method = "inprocess"
pytester.runpytest()
Defensive patterns

Strategy: validation

Validate before calling

VALID_METHODS = {"inprocess", "subprocess"}
assert pytester._method in VALID_METHODS, f"bad method: {pytester._method}"
pytester.runpytest()

Type guard

def is_valid_method(m: object) -> bool:
    return m in ("inprocess", "subprocess")

Prevention

When it happens

Trigger: Manually constructing a Pytester and setting _method to a typo or unsupported string; a third-party fixture that wraps Pytester and sets an invalid method; a stale plugin incompatible with the installed pytest version.

Common situations: Plugin upgrades where the set of valid methods changed; monkeypatching _method in a test; a custom pytester subclass that overrides construction.

Related errors


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