pytest-dev/pytest · error · RuntimeError
Unrecognized runpytest option
Error message
Unrecognized runpytest option: {self._method} What it means
Thrown by Pytester.runpytest() when self._method is neither 'inprocess' nor 'subprocess'. The _method attribute is set from the --runpytest command-line option and should always be one of those two; this guard catches corruption or custom Pytester subclasses.
Solutions
- Do not set pytester._method manually; rely on the --runpytest={inprocess,subprocess} option.
- If subclassing, set _method to one of the two documented values.
- Check for plugins or conftest.py code that patches _method.
Example fix
// before pytester._method = "custom" pytester.runpytest() // after pytester._method = "subprocess" pytester.runpytest()
Defensive patterns
Strategy: validation
Validate before calling
assert pytester._method in ('inprocess', 'subprocess'), f'bad method: {pytester._method}'
pytester.runpytest() Prevention
- Never set _method manually; use --runpytest option.
- Audit plugins/conftest.py for _method patches.
- Use runpytest_inprocess()/runpytest_subprocess() directly to avoid dispatch.
When it happens
Trigger: Subclassing Pytester and overriding _method with an invalid value; manually setting pytester._method; a future pytest version changing the valid set while an old plugin patches _method; the --runpytest option receiving an unknown value through a config override.
Common situations: Custom test harnesses that wrap Pytester; monkeypatching experiments; version skew between pytest and a plugin that touches _method.
Related errors
- could not find test report matching
- Empty parameter set in
- example " " is not found as a file or directory
- Expected a float string for option
- Expected an int string for option
AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11).
Data as JSON: /api/errors/3f3b8dd8e70c10fa.
Report an issue: GitHub.
Appendix: 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 0d6fbdeffa)