pytest-dev / pytest-dev/pytest-xdist

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

Open
#1,086 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.9k
Forks
287
Avg merge
9h 30m
Merged PRs (30d)
2

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The example is in conftest.py; start by tracing when pytest_cmdline_preparse, pytest_configure, and pytest_collection_modifyitems run relative to xdist worker startup. Done means a configurable threshold keeps small collections serial and enables parallel workers only when the collected test count exceeds it.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.