Skip to content

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

Open
@sam-ghosh

Description

@sam-ghosh

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions