Bird
Raised Fist0
PyTesttesting~20 mins

Running with -n auto 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 Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when running pytest with '-n auto' on this test file?

Given this simple pytest test file, what will be the output when running pytest -n auto?

PyTest
import pytest
import time

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 in parallel but fail due to shared state issues.
BAll tests run sequentially taking about 3 seconds total, all pass.
CTests run in parallel, total time about 1 second, all pass.
DTests run sequentially but fail due to missing dependencies.
Attempts:
2 left
💡 Hint

Think about how -n auto uses all CPU cores to run tests in parallel.

assertion
intermediate
2:00remaining
Which assertion correctly verifies pytest-xdist runs tests in parallel?

You want to write a test that checks if pytest-xdist is running tests in parallel using -n auto. Which assertion is correct?

PyTest
import pytest
import time
import threading

results = []

def test_parallel():
    def worker():
        time.sleep(1)
        results.append(threading.get_ident())

    threads = []
    for _ in range(3):
        t = threading.Thread(target=worker)
        t.start()
        threads.append(t)
    for t in threads:
        t.join()

    # Which assertion below correctly checks parallel execution?
Aassert results == []
Bassert len(results) == 1
Cassert all(r == results[0] for r in results)
Dassert len(set(results)) == 3
Attempts:
2 left
💡 Hint

Parallel execution means different threads run the tasks.

🔧 Debug
advanced
2:00remaining
Why does pytest with '-n auto' fail on tests using a global variable?

Consider this test file:

counter = 0

def test_increment():
    global counter
    counter += 1
    assert counter == 1

When running pytest -n auto, tests fail intermittently. Why?

ABecause tests run in separate processes, global variables are not shared, causing inconsistent state.
BBecause <code>-n auto</code> disables global variables.
CBecause pytest-xdist requires tests to be synchronous.
DBecause the global variable is reset by pytest before each test.
Attempts:
2 left
💡 Hint

Think about how pytest-xdist runs tests in parallel processes.

framework
advanced
2:00remaining
Which pytest plugin enables the '-n auto' option for parallel test execution?

You want to run tests in parallel using pytest -n auto. Which plugin must be installed?

Apytest-xdist
Bpytest-parallel
Cpytest-concurrent
Dpytest-multithread
Attempts:
2 left
💡 Hint

Look for the official pytest plugin that supports -n option.

🧠 Conceptual
expert
3:00remaining
What is a key limitation when using 'pytest -n auto' with tests that modify a database?

When running tests with pytest -n auto that modify a shared database, what is a common limitation?

ATests automatically rollback changes after each test.
BTests may interfere with each other causing flaky results due to concurrent writes.
CTests cannot access the database at all when run in parallel.
DTests run slower because database connections are serialized.
Attempts:
2 left
💡 Hint

Think about shared resources and concurrency.

Practice

(1/5)
1. What does running pytest -n auto do?
easy
A. Runs tests but disables all plugins
B. Runs tests one by one in the order they appear
C. Runs tests in parallel using all CPU cores automatically
D. Runs tests only on a single CPU core

Solution

  1. Step 1: Understand the -n auto option

    The -n auto option tells pytest to run tests in parallel using all available CPU cores automatically.
  2. Step 2: Compare with other options

    Running tests one by one or on a single core does not use parallelism. Disabling plugins is unrelated.
  3. Final Answer:

    Runs tests in parallel using all CPU cores automatically -> Option C
  4. Quick Check:

    -n auto means parallel on all cores [OK]
Hint: Remember: -n auto means use all CPU cores [OK]
Common Mistakes:
  • Thinking -n auto runs tests sequentially
  • Confusing -n auto with disabling plugins
  • Assuming it runs on a single core only
2. Which command correctly runs pytest with parallel execution using all CPU cores?
easy
A. pytest -n all
B. pytest -n auto
C. pytest --parallel
D. pytest -p auto

Solution

  1. Step 1: Identify the correct syntax for parallel execution

    The correct pytest command to run tests in parallel on all CPU cores is pytest -n auto.
  2. Step 2: Check other options for correctness

    -n all is invalid, --parallel is not a pytest option, and -p auto relates to plugins, not parallelism.
  3. Final Answer:

    pytest -n auto -> Option B
  4. Quick Check:

    Correct flag for parallel is -n auto [OK]
Hint: Use -n auto exactly for parallel runs [OK]
Common Mistakes:
  • Using -n all instead of -n auto
  • Confusing plugin flags with parallel flags
  • Assuming --parallel is valid
3. Given this pytest command: pytest -n auto on a machine with 4 CPU cores, what is the expected behavior?
medium
A. Tests run in parallel on 4 cores
B. Tests run sequentially on one core
C. Tests run in parallel but limited to 2 cores
D. Tests fail to run without extra flags

Solution

  1. Step 1: Understand -n auto behavior

    The -n auto option uses all available CPU cores for parallel test execution.
  2. Step 2: Apply to 4-core machine

    On a machine with 4 CPU cores, pytest will run tests in parallel using all 4 cores.
  3. Final Answer:

    Tests run in parallel on 4 cores -> Option A
  4. Quick Check:

    -n auto uses all cores = 4 cores [OK]
Hint: All cores used equals number of CPU cores [OK]
Common Mistakes:
  • Assuming -n auto limits cores to 2
  • Thinking tests run sequentially despite -n auto
  • Believing extra flags are needed for parallelism
4. You run pytest -n auto but get an error saying the option is unknown. What is the most likely cause?
medium
A. Your Python version is too new
B. You typed -n auto incorrectly
C. You need to run pytest as administrator
D. pytest-xdist plugin is not installed

Solution

  1. Step 1: Identify why -n auto might be unknown

    The -n option is provided by the pytest-xdist plugin. If it's missing, pytest won't recognize -n auto.
  2. Step 2: Check other options

    Typing errors would cause different errors, Python version usually doesn't block this, and admin rights are not required.
  3. Final Answer:

    pytest-xdist plugin is not installed -> Option D
  4. Quick Check:

    Missing plugin causes unknown -n error [OK]
Hint: Install pytest-xdist to enable -n option [OK]
Common Mistakes:
  • Ignoring plugin requirement
  • Assuming Python version blocks -n
  • Thinking admin rights are needed
5. You want to speed up your test suite with pytest -n auto, but some tests fail due to shared resource conflicts. What is the best approach to fix this?
hard
A. Mark conflicting tests with @pytest.mark.serial and run others in parallel
B. Remove -n auto and run all tests sequentially
C. Increase CPU cores to avoid conflicts
D. Disable pytest-xdist plugin

Solution

  1. Step 1: Understand parallel test conflicts

    Tests sharing resources can fail when run in parallel. Marking them to run serially avoids conflicts.
  2. Step 2: Use pytest markers to control parallelism

    Using @pytest.mark.serial or similar markers tells pytest-xdist to run those tests one by one, while others run in parallel.
  3. Step 3: Evaluate other options

    Running all sequentially loses speed benefits, increasing CPU cores doesn't fix resource conflicts, and disabling plugin removes parallelism.
  4. Final Answer:

    Mark conflicting tests with @pytest.mark.serial and run others in parallel -> Option A
  5. Quick Check:

    Use markers to isolate conflicting tests [OK]
Hint: Mark tests to run serially to fix conflicts [OK]
Common Mistakes:
  • Running all tests sequentially losing speed
  • Thinking more CPU cores fix resource conflicts
  • Disabling plugin instead of isolating tests