How many times will 'Setup resource' be printed during the entire test run?
medium
A. Once
B. Zero times
C. Twice
D. Four times
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 C
Quick Check:
Session scope runs once per worker = 2 times [OK]
Hint: Session scope runs once per worker in parallel tests [OK]
Common Mistakes:
Assuming session scope runs only once globally
Ignoring parallel worker count
Confusing function scope with session scope
4. You have a session-scoped fixture used in parallel tests with 3 workers. You notice the fixture setup runs 3 times, but you want it to run only once globally. What is the likely cause and fix?
medium
A. Cause: fixture is not used; Fix: add fixture to tests.
B. Cause: fixture scope is function; Fix: change to session scope.
C. Cause: parallel tests disabled; Fix: enable parallel execution.
D. Cause: session scope runs per worker; Fix: use a database or external service to share state.
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 D
Quick Check:
Session scope per worker needs external sharing [OK]
Hint: Session scope runs per worker; share state externally to fix [OK]
Common Mistakes:
Thinking session scope runs once globally in parallel
Changing scope to function instead of sharing state
Ignoring parallel execution effects
5. You want to run expensive setup code only once for all tests across 4 parallel pytest workers. Which approach correctly ensures this behavior?
hard
A. Use scope='session' fixture and implement external resource locking (e.g., file lock or database).
B. Use scope='function' fixture and cache results in a global variable.
C. Use scope='session' fixture and rely on pytest-xdist to share it automatically.
D. Use scope='module' fixture and run tests sequentially.
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 A
Quick Check:
External locking + session scope = single global setup [OK]
Hint: Combine session scope with external locking for global setup [OK]