0
0
PyTesttesting~20 mins

Running with -n auto in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.