pytest-dev / pytest-xdist

pytest plugin for distributed testing and loop-on-failures testing modes.
https://pytest-xdist.readthedocs.io
MIT License
1.49k stars 232 forks source link

Any option to run parallelized testing ONLY if the number of tests collected is more than say 20? #1086

Open sam-ghosh opened 6 months ago

sam-ghosh commented 6 months ago

For smaller number of tests, setting up 12 workers in 12 cores in my machine is overkill, is there a way to have pytest run xdist/parallel testing only if the number of tests collected is more than 20?

I tried something like below - but it seems the workers spin up before the test collection starts

conftest.py


import pytest

def pytest_addoption(parser):
    parser.addoption(
        "--min-tests-count-for-parallel",
        action="store",
        default=10,
        type=int,
        help="Min number of tests that should be collected before running tests in parallel",
    )

def pytest_configure(config):
    min_tests_count_for_parallel = config.getoption("--min-tests-count-for-parallel")
    config.num_tests_collected = 0
    config.min_tests_count_for_parallel = min_tests_count_for_parallel

def pytest_collection_modifyitems(config, items):
    if len(items) > config.min_tests_count_for_parallel:
        config.option.numprocesses = "auto"
    else:
        config.option.numprocesses = 0

@pytest.hookimpl(tryfirst=True)
def pytest_cmdline_preparse(config, args):
    if "-n" in args:
        args.remove("-n")
RonnyPfannschmidt commented 6 months ago

As collection happens on workers, we can't enable xdist after collection

Enabling this is most unlikely

Mogost commented 5 months ago

It's a shame. The idea sounds cool because sometimes you don't get profit from parallelization because of overheads.

azmeuk commented 1 week ago

Related to #272