pytest-xdist icon indicating copy to clipboard operation
pytest-xdist copied to clipboard

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

Open sam-ghosh opened this issue 1 year ago • 3 comments

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")

sam-ghosh avatar May 20 '24 21:05 sam-ghosh

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

Enabling this is most unlikely

RonnyPfannschmidt avatar May 21 '24 03:05 RonnyPfannschmidt

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

Mogost avatar Jun 06 '24 16:06 Mogost

Related to #272

azmeuk avatar Nov 14 '24 22:11 azmeuk