Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sam-ghosh opened this issue May 20, 2024 · 2 comments

Comments

@sam-ghosh
Copy link

sam-ghosh commented May 20, 2024

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
Copy link
Member

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

Enabling this is most unlikely

@Mogost
Copy link

Mogost commented Jun 6, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants