Fixtures help prepare things your tests need. Using fixture scope with parallel tests controls how often setup runs, saving time and avoiding conflicts.
Fixture scope with parallel tests in PyTest
Start learning this pattern below
Jump into concepts and practice - no test required
@pytest.fixture(scope='scope_name') def fixture_name(): # setup code yield resource # teardown code
Scope can be 'function' (default), 'class', 'module', or 'session'.
For parallel tests, 'session' scope means fixture runs once per worker process for all tests in that worker.
@pytest.fixture(scope='function') def setup_func(): print('Setup for each test') yield print('Teardown for each test')
@pytest.fixture(scope='module') def setup_module(): print('Setup once per module') yield print('Teardown once per module')
@pytest.fixture(scope='session') def setup_session(): print('Setup once per test session') yield print('Teardown once per session')
This test uses a session-scoped fixture. The setup runs once before all tests in a worker, even if tests run in parallel. Each test uses the same resource.
import pytest @pytest.fixture(scope='session') def resource(): print('Setup resource') yield 'resource data' print('Teardown resource') @pytest.mark.parametrize('i', range(3)) def test_example(resource, i): print(f'Test {i} uses {resource}') assert resource == 'resource data'
When running tests in parallel (e.g., with pytest-xdist), session-scoped fixtures run once per worker process, not globally.
Use the 'scope' carefully to avoid conflicts or repeated expensive setups.
For truly global fixtures in parallel runs, consider external setup or plugins that support shared fixtures.
Fixture scope controls how often setup and teardown run.
Session scope runs once per test session, saving time for expensive setups.
In parallel tests, session scope runs once per worker, not globally.
Practice
scope='session' parameter in a pytest fixture control?Solution
Step 1: Understand fixture scopes in pytest
Pytest fixtures can have different scopes like function, class, module, and session, which control how often the fixture setup runs.Step 2: Identify what session scope means
Session scope means the fixture runs only once for the entire test session, regardless of how many tests use it.Final Answer:
The fixture runs once per entire test session. -> Option AQuick Check:
scope='session' = runs once per session [OK]
- Confusing session scope with function scope
- Thinking session scope runs per test module
- Assuming session scope runs per test class
Solution
Step 1: Recall pytest fixture syntax
Pytest fixtures use the decorator@pytest.fixture()with optional parameters likescopeas a string.Step 2: Identify correct scope parameter usage
The scope parameter must be a string, soscope='session'is correct. Options C and D are invalid syntax.Final Answer:
@pytest.fixture(scope='session') -> Option BQuick Check:
Correct syntax uses scope='session' string [OK]
- Omitting quotes around 'session'
- Using invalid keyword arguments
- Confusing scope with boolean flags
@pytest.fixture(scope='session')
def resource():
print('Setup resource')
yield
print('Teardown resource')
def test_a(resource):
pass
def test_b(resource):
passHow many times will 'Setup resource' be printed during the entire test run?
Solution
Step 1: Understand session scope with parallel workers
When running tests in parallel with pytest-xdist, each worker runs its own session, so session-scoped fixtures run once per worker.Step 2: Calculate total setup calls
With 2 workers, the fixture setup runs once per worker, so 'Setup resource' prints twice.Final Answer:
Twice -> Option CQuick Check:
Session scope runs once per worker = 2 times [OK]
- Assuming session scope runs only once globally
- Ignoring parallel worker count
- Confusing function scope with session scope
Solution
Step 1: Identify why session scope runs multiple times
In parallel testing, session scope runs once per worker, so with 3 workers, setup runs 3 times.Step 2: Understand how to share fixture state globally
To run setup only once globally, you must share state outside pytest workers, e.g., via a database or external service.Final Answer:
Cause: session scope runs per worker; Fix: use a database or external service to share state. -> Option DQuick Check:
Session scope per worker needs external sharing [OK]
- Thinking session scope runs once globally in parallel
- Changing scope to function instead of sharing state
- Ignoring parallel execution effects
Solution
Step 1: Understand session scope behavior with parallel workers
Session scope runs once per worker, so with 4 workers, setup runs 4 times unless shared externally.Step 2: Identify how to run setup only once globally
Implementing external resource locking (like a file lock or database flag) ensures only one worker runs the expensive setup.Final Answer:
Use scope='session' fixture and implement external resource locking (e.g., file lock or database). -> Option AQuick Check:
External locking + session scope = single global setup [OK]
- Assuming pytest-xdist shares session fixtures automatically
- Using function scope and expecting single setup
- Running tests sequentially defeats parallel purpose
