Bird
Raised Fist0
PyTesttesting~20 mins

Worker distribution strategies in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Worker Distribution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pytest-xdist with loadscope distribution
Given the following pytest test file and running pytest with pytest -n 2 --dist=loadscope, what will be the order of test execution across workers?
PyTest
import pytest

@pytest.mark.scope('module1')
def test_a():
    pass

@pytest.mark.scope('module1')
def test_b():
    pass

@pytest.mark.scope('module2')
def test_c():
    pass

@pytest.mark.scope('module2')
def test_d():
    pass
ATests are randomly distributed without scope grouping
BWorker 1 runs test_a and test_b; Worker 2 runs test_c and test_d
CWorker 1 runs test_a and test_d; Worker 2 runs test_b and test_c
DWorker 1 runs test_a and test_c; Worker 2 runs test_b and test_d
Attempts:
2 left
💡 Hint
Loadscope groups tests by their scope marker to minimize setup overhead.
assertion
intermediate
1:30remaining
Correct assertion for balanced worker load
You want to assert that pytest-xdist distributed 10 tests evenly across 2 workers. Which assertion correctly verifies this?
Aassert worker_test_counts[0] == 10 and worker_test_counts[1] == 0
Bassert all(worker_load == 5 for worker_load in worker_test_counts)
Cassert sum(worker_test_counts) == 10 and max(worker_test_counts) - min(worker_test_counts) <= 1
Dassert min(worker_test_counts) == 0
Attempts:
2 left
💡 Hint
Balanced load means tests are nearly evenly split, allowing a difference of one.
🔧 Debug
advanced
2:00remaining
Identify the cause of uneven test distribution
A test suite with 12 tests is run with pytest-xdist using --dist=loadfile and 3 workers. The distribution is uneven: one worker runs 8 tests, others run 2 each. What is the most likely cause?
AThe number of workers is greater than the number of test files
Bpytest-xdist randomly assigns tests ignoring files
CTests are grouped by markers, causing imbalance
DTests are grouped by file, and one file contains 8 tests while others have 2 each
Attempts:
2 left
💡 Hint
Loadfile groups tests by their file location.
framework
advanced
1:30remaining
Choosing the right pytest-xdist distribution mode
Which pytest-xdist distribution mode is best when tests have expensive setup per module and you want to minimize setup duplication?
Aloadscope
Bloadgroup
Cno distribution (run sequentially)
Dloadfile
Attempts:
2 left
💡 Hint
Consider grouping tests by their setup scope.
🧠 Conceptual
expert
2:30remaining
Impact of worker distribution on flaky tests
How can improper worker distribution in pytest-xdist contribute to flaky test results?
ATests sharing state run on different workers causing race conditions
BTests run sequentially causing timeout errors
CWorkers share the same memory space causing data corruption
DTests are always isolated, so distribution has no impact on flakiness
Attempts:
2 left
💡 Hint
Think about shared resources and parallel execution.

Practice

(1/5)
1. What does the --dist=loadscope option do in pytest-xdist worker distribution?
easy
A. It distributes tests randomly to all workers.
B. It runs all tests sequentially on a single worker.
C. It groups tests by their scope and distributes them to workers.
D. It groups tests by file size before distribution.

Solution

  1. Step 1: Understand the meaning of loadscope

    The loadscope mode groups tests by their scope, such as class or module, so related tests run together.
  2. Step 2: Compare with other distribution modes

    Unlike random or file-based grouping, loadscope keeps related tests together for better caching and setup reuse.
  3. Final Answer:

    It groups tests by their scope and distributes them to workers. -> Option C
  4. Quick Check:

    loadscope = group by scope [OK]
Hint: Loadscope groups tests by scope like class or module [OK]
Common Mistakes:
  • Confusing loadscope with random distribution
  • Thinking loadscope groups by file size
  • Assuming loadscope runs tests sequentially
2. Which of the following is the correct pytest command to run tests with 4 workers using file-based distribution?
easy
A. pytest -n 4 --dist=loadfile
B. pytest --dist=loadfile -n four
C. pytest -n=4 --dist=loadscope
D. pytest -n 4 --dist=loadgroup

Solution

  1. Step 1: Identify correct syntax for number of workers

    The correct syntax is -n 4 to specify 4 workers; spelling out 'four' is invalid.
  2. Step 2: Match distribution mode to file-based

    The file-based distribution mode is loadfile, so --dist=loadfile is correct.
  3. Final Answer:

    pytest -n 4 --dist=loadfile -> Option A
  4. Quick Check:

    -n 4 and --dist=loadfile correct syntax [OK]
Hint: Use -n number and --dist=loadfile for file grouping [OK]
Common Mistakes:
  • Using spelled-out numbers like 'four'
  • Mixing distribution modes incorrectly
  • Using equals sign with -n option
3. Given this pytest command: pytest -n 3 --dist=loadfile, and three test files test_a.py, test_b.py, test_c.py, how will tests be distributed?
medium
A. Tests run sequentially on a single worker.
B. All workers run tests from all files randomly.
C. Tests are grouped by class across files.
D. Each worker runs tests from one file exclusively.

Solution

  1. Step 1: Understand loadfile distribution

    Loadfile mode assigns tests grouped by file to different workers, so each worker gets whole files.
  2. Step 2: Match number of workers to files

    With 3 workers and 3 files, each worker will get one file's tests exclusively.
  3. Final Answer:

    Each worker runs tests from one file exclusively. -> Option D
  4. Quick Check:

    loadfile = group by file [OK]
Hint: Loadfile means one file per worker [OK]
Common Mistakes:
  • Thinking tests are split randomly
  • Confusing loadfile with loadscope
  • Assuming tests run sequentially
4. You run pytest -n 2 --dist=loadscope but notice tests from the same class run on different workers. What is the likely cause?
medium
A. Tests are not properly grouped because the class scope is not detected.
B. The -n option must be set to 1 for loadscope.
C. The --dist option is ignored when using multiple workers.
D. Tests are always distributed randomly regardless of options.

Solution

  1. Step 1: Understand loadscope grouping behavior

    Loadscope groups tests by scope like class or module, so tests in the same class should run together.
  2. Step 2: Identify why grouping fails

    If tests from the same class run on different workers, pytest likely failed to detect the class scope properly, causing wrong grouping.
  3. Final Answer:

    Tests are not properly grouped because the class scope is not detected. -> Option A
  4. Quick Check:

    Undetected scope breaks loadscope grouping [OK]
Hint: Undetected scope causes loadscope to fail grouping [OK]
Common Mistakes:
  • Thinking -n must be 1 for loadscope
  • Believing --dist is ignored with multiple workers
  • Assuming distribution is always random
5. You want to run tests in custom groups using pytest-xdist. Which command and option combination allows you to define and use custom test groups for worker distribution?
hard
A. pytest -n 3 --dist=loadgroup --tx group1 --tx group2 --tx group3
B. pytest -n 3 --dist=loadgroup
C. pytest -n 3 --dist=loadfile --group=custom
D. pytest -n 3 --dist=loadscope --group=custom

Solution

  1. Step 1: Identify the distribution mode for custom groups

    The loadgroup mode is designed for custom grouping of tests for distribution.
  2. Step 2: Understand correct command usage

    Using --dist=loadgroup with -n 3 enables pytest-xdist to distribute tests based on user-defined groups configured elsewhere (e.g., in pytest hooks).
  3. Final Answer:

    pytest -n 3 --dist=loadgroup -> Option B
  4. Quick Check:

    loadgroup enables custom test groups [OK]
Hint: Use --dist=loadgroup to enable custom test groups [OK]
Common Mistakes:
  • Adding invalid --group option
  • Using --tx incorrectly for grouping
  • Confusing loadgroup with loadfile or loadscope