When I run all pytests I do want to fully benefit from spreading the load on all m cores (xdist) but when I run a subset of them is almost always for development/debugging purposes, case in which I do want to avoid the downsides of xdist as:
To examplify: calling pytest -k foo
should effectively run pytest -n0 -k foo
. Keep in mind that calling pytest without arguments should not affect the -n value.
You can dynamically add the option, for example:
conftest.py
import sys
def pytest_cmdline_preparse(args):
if "xdist" in sys.modules and "-k" in args:
for i, arg in enumerate(args):
# remove -n # option
if arg == "-n":
del args[i]
del args[i]
break
# remove -n# option
if re.match(r"-n\d+", arg):
del args[i]
break
args[:] = ["-n0"] + args
This removes an existing "-n" option (both of the form -n #
and -n#
- you may not need both, depending on how your command line looks), and adds the "-n0" option instead.
Update:
pytest_cmdline_preparse
is deprecated in current (6.x) pytest versions, pytest_load_initial_conftests
can be used instead in the same manner.