Bird
Raised Fist0
PyTesttesting~20 mins

Parallel execution in CI 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
🎖️
Parallel Execution Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Understanding pytest-xdist parallel test execution output
Given the pytest command below executed with pytest-xdist for parallel testing:
pytest -n 3 test_sample.py

What is the expected output behavior regarding test distribution?
PyTest
import time
import pytest

def test_one():
    time.sleep(1)
    assert True

def test_two():
    time.sleep(1)
    assert True

def test_three():
    time.sleep(1)
    assert True
ATests run sequentially but pytest-xdist reports parallel execution without speedup.
BAll three tests run sequentially on the same worker, taking about 3 seconds total.
CTests run in parallel but only two workers are used, taking about 2 seconds total.
DTests run in parallel across 3 workers, completing in about 1 second total.
Attempts:
2 left
💡 Hint
pytest-xdist distributes tests across workers to run them simultaneously.
assertion
intermediate
1:30remaining
Choosing the correct assertion for parallel test logs
In a CI pipeline running tests in parallel, which assertion best verifies that all test logs contain the string 'PASSED' exactly once per test?
PyTest
logs = ['test_one PASSED', 'test_two PASSED', 'test_three PASSED']
Aassert logs == ['PASSED', 'PASSED', 'PASSED']
Bassert all('PASSED' in log for log in logs)
Cassert logs.count('PASSED') == 3
Dassert any('PASSED' in log for log in logs)
Attempts:
2 left
💡 Hint
Check each log individually for the substring.
🔧 Debug
advanced
2:00remaining
Debugging flaky tests in parallel CI runs
A test suite runs fine locally but intermittently fails in CI when run with pytest-xdist parallel execution. Which is the most likely cause?
ATests share global state or resources causing race conditions.
Bpytest-xdist does not support parallel execution on CI servers.
CThe test code has syntax errors that only appear in CI.
DThe CI server has insufficient CPU cores to run tests.
Attempts:
2 left
💡 Hint
Parallel tests must avoid shared mutable state.
framework
advanced
2:00remaining
Configuring pytest for parallel execution in CI
Which pytest configuration snippet correctly enables parallel test execution with 4 workers in a CI environment?
A
[pytest]
addopts = -n 4 --dist=loadfile
B
[pytest]
addopts = --workers=4 --dist=loadfile
C
[pytest]
addopts = -n 4 --dist=loadscope
D
[pytest]
addopts = --parallel=4 --dist=loadscope
Attempts:
2 left
💡 Hint
The '-n' option sets the number of workers.
🧠 Conceptual
expert
2:30remaining
Impact of parallel test execution on test isolation in CI
Why is test isolation especially critical when running tests in parallel in a CI pipeline?
ABecause isolated tests require more CPU resources in parallel.
BBecause parallel tests always run slower, isolation improves speed.
CBecause parallel tests may run simultaneously, shared state can cause unpredictable failures.
DBecause CI pipelines do not support parallel execution without isolation.
Attempts:
2 left
💡 Hint
Think about what happens if tests share data at the same time.

Practice

(1/5)
1. What is the main benefit of using parallel execution in pytest within a CI environment?
easy
A. It disables flaky tests to improve stability.
B. It automatically fixes failing tests during execution.
C. It generates detailed test coverage reports.
D. It runs multiple tests at the same time to reduce total test time.

Solution

  1. Step 1: Understand parallel execution purpose

    Parallel execution means running tests simultaneously instead of one by one.
  2. Step 2: Identify benefit in CI context

    Running tests at the same time reduces the total time needed to finish all tests in CI.
  3. Final Answer:

    It runs multiple tests at the same time to reduce total test time. -> Option D
  4. Quick Check:

    Parallel execution = faster test runs [OK]
Hint: Parallel means multiple tests run together, saving time [OK]
Common Mistakes:
  • Confusing parallel execution with automatic bug fixing
  • Thinking it generates reports automatically
  • Assuming it disables tests instead of running them
2. Which command correctly enables parallel test execution using pytest-xdist with 4 workers?
easy
A. pytest -n 4
B. pytest --workers=4
C. pytest --parallel=4
D. pytest -p xdist 4

Solution

  1. Step 1: Recall pytest-xdist syntax

    The pytest-xdist plugin uses the option -n followed by the number of workers.
  2. Step 2: Match correct command

    The correct command to run tests in parallel with 4 workers is pytest -n 4.
  3. Final Answer:

    pytest -n 4 -> Option A
  4. Quick Check:

    Use -n to set worker count [OK]
Hint: Remember: -n sets number of parallel workers [OK]
Common Mistakes:
  • Using --workers instead of -n
  • Adding number without -n option
  • Misplacing plugin name in command
3. Given this pytest command in CI: pytest -n 3 tests/, what is the expected behavior?
medium
A. Tests in the 'tests/' folder run sequentially on one worker.
B. Tests in the 'tests/' folder run in parallel on 3 workers.
C. Only 3 tests will run from the 'tests/' folder.
D. Tests will run with 3 retries on failure.

Solution

  1. Step 1: Analyze the command options

    The -n 3 option tells pytest-xdist to use 3 parallel workers.
  2. Step 2: Understand test execution effect

    All tests in the 'tests/' folder will be distributed and run simultaneously on 3 workers.
  3. Final Answer:

    Tests in the 'tests/' folder run in parallel on 3 workers. -> Option B
  4. Quick Check:

    -n 3 means 3 parallel workers [OK]
Hint: -n 3 means run tests on 3 parallel workers [OK]
Common Mistakes:
  • Thinking only 3 tests run total
  • Assuming tests run sequentially
  • Confusing retries with parallelism
4. You added pytest -n 4 in your CI but tests still run sequentially. What is the most likely cause?
medium
A. The tests folder is empty, so no tests run.
B. You need to add --parallel option instead of -n.
C. pytest-xdist plugin is not installed in the CI environment.
D. You must specify the number of retries for parallel to work.

Solution

  1. Step 1: Check plugin requirement for parallelism

    pytest-xdist plugin must be installed to enable -n parallel execution.
  2. Step 2: Identify cause of sequential runs

    If plugin is missing, pytest ignores -n and runs tests sequentially.
  3. Final Answer:

    pytest-xdist plugin is not installed in the CI environment. -> Option C
  4. Quick Check:

    Missing plugin causes no parallelism [OK]
Hint: Parallel needs pytest-xdist installed to work [OK]
Common Mistakes:
  • Using wrong option like --parallel
  • Assuming empty folder causes sequential runs
  • Confusing retries with parallel execution
5. In a CI pipeline, you want to run tests in parallel but limit each worker to use only one CPU core to avoid overload. Which pytest-xdist option helps achieve this?
hard
A. Use pytest -n auto --dist loadscope to auto assign workers with load balancing.
B. Use pytest -n 4 --max-worker-threads=1 to limit threads per worker.
C. Use pytest -n 4 --boxed to isolate each test in a subprocess.
D. Use pytest -n 4 --max-worker-memory=1G to limit memory per worker.

Solution

  1. Step 1: Understand CPU core limitation in pytest-xdist

    pytest-xdist can auto detect CPU cores and assign workers accordingly using -n auto.
  2. Step 2: Use load balancing to distribute tests efficiently

    The --dist loadscope option balances tests to avoid overloading any worker.
  3. Final Answer:

    Use pytest -n auto --dist loadscope to auto assign workers with load balancing. -> Option A
  4. Quick Check:

    -n auto with loadscope balances CPU load [OK]
Hint: -n auto with --dist loadscope balances CPU load per worker [OK]
Common Mistakes:
  • Using non-existent options like --max-worker-threads
  • Confusing --boxed with CPU core limits
  • Trying to limit memory instead of CPU