Imagine you have 10 tests that each take 1 minute to run. If you run them one after another, it takes 10 minutes total. If you run them in parallel, why does the total time reduce?
Think about how multiple people working together can finish tasks faster than one person alone.
Parallel tests run simultaneously on multiple processors or cores. This means the total time is roughly the time of the longest test, not the sum of all tests.
Given the following pytest command and test files, what is the expected total test run time?
pytest -n 4
Each of 8 tests takes 2 seconds to run.
4 workers run tests in parallel, dividing 8 tests evenly.
With 4 workers, 8 tests split into 4 groups of 2 tests each. Each group takes 4 seconds (2 tests × 2 seconds). All groups run simultaneously, so total time is about 4 seconds.
You want to write a test that checks if running tests in parallel is faster than running them sequentially. Which assertion correctly compares the times?
sequential_time = 20 # seconds parallel_time = 6 # seconds # Which assertion is correct?
Parallel tests should finish faster than sequential tests.
The assertion should confirm that parallel_time is less than sequential_time to prove speedup.
You run tests in parallel using pytest-xdist but see no reduction in total time. What is the most likely cause?
Think about what happens if tests block each other.
If tests share a resource like a database or file and wait for it, they run sequentially despite parallel setup, causing no speedup.
Which pytest-xdist configuration option best controls the number of parallel workers to match your CPU cores?
Look for the option that automatically detects CPU cores.
The option -n auto tells pytest-xdist to use as many workers as CPU cores automatically.