Bird
Raised Fist0
PyTesttesting~10 mins

Fixture scope with parallel tests in PyTest - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a fixture with session scope.

PyTest
import pytest

@pytest.fixture(scope=[1])
def setup_db():
    print("Setup DB")
    yield
    print("Teardown DB")
Drag options to blanks, or click blank then click option'
Afunction
Bsession
Cmodule
Dclass
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'function' scope when you want to share setup across tests.
Confusing 'module' and 'session' scopes.
2fill in blank
medium

Complete the code to run tests in parallel using pytest-xdist.

PyTest
# Run tests with pytest-xdist
pytest -n [1]
Drag options to blanks, or click blank then click option'
Aauto
B4
Call
Dparallel
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid values like 'all' or 'parallel' for '-n'.
Not installing pytest-xdist before using '-n'.
3fill in blank
hard

Fix the error in the fixture to avoid sharing state in parallel tests.

PyTest
import pytest

@pytest.fixture(scope="session")
def resource():
    data = []
    yield data
    data.clear()

# Tests modify resource by appending values
# Problem: parallel tests share the same list causing conflicts
# Fix by changing [1]
Drag options to blanks, or click blank then click option'
Ascope to 'module'
Bdata to a global variable
Cyield to return
Dscope to 'function'
Attempts:
3 left
💡 Hint
Common Mistakes
Keeping session scope and causing test interference.
Using global variables which are shared across tests.
4fill in blank
hard

Fill both blanks to create a fixture that runs once per module and uses autouse.

PyTest
@pytest.fixture(scope=[1], autouse=[2])
def setup_env():
    print("Setup environment")
    yield
    print("Teardown environment")
Drag options to blanks, or click blank then click option'
Amodule
Bfunction
CTrue
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Setting autouse to False and expecting automatic use.
Using function scope when module scope is needed.
5fill in blank
hard

Fill all three blanks to create a parametrized fixture with session scope and proper teardown.

PyTest
@pytest.fixture(scope=[1], params=["chrome", "firefox"])
def browser(request):
    driver = start_driver([2])
    yield driver
    [3](driver)
Drag options to blanks, or click blank then click option'
Asession
Brequest.param
Cstop_driver
Dfunction
Attempts:
3 left
💡 Hint
Common Mistakes
Using function scope instead of session for sharing.
Not using request.param to get parameter value.
Forgetting to stop the driver in teardown.

Practice

(1/5)
1. What does the scope='session' parameter in a pytest fixture control?
easy
A. The fixture runs once per entire test session.
B. The fixture runs once per test function.
C. The fixture runs once per test class.
D. The fixture runs once per test module.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    The fixture runs once per entire test session. -> Option A
  4. Quick Check:

    scope='session' = runs once per session [OK]
Hint: Session scope means one setup for all tests in session [OK]
Common Mistakes:
  • Confusing session scope with function scope
  • Thinking session scope runs per test module
  • Assuming session scope runs per test class
2. Which of the following is the correct syntax to define a pytest fixture with session scope?
easy
A. @pytest.fixture(scope='function')
B. @pytest.fixture(scope='session')
C. @pytest.fixture(session=True)
D. @pytest.fixture(scope=session)

Solution

  1. Step 1: Recall pytest fixture syntax

    Pytest fixtures use the decorator @pytest.fixture() with optional parameters like scope as a string.
  2. Step 2: Identify correct scope parameter usage

    The scope parameter must be a string, so scope='session' is correct. Options C and D are invalid syntax.
  3. Final Answer:

    @pytest.fixture(scope='session') -> Option B
  4. Quick Check:

    Correct syntax uses scope='session' string [OK]
Hint: Use quotes around scope value: scope='session' [OK]
Common Mistakes:
  • Omitting quotes around 'session'
  • Using invalid keyword arguments
  • Confusing scope with boolean flags
3. Consider this pytest fixture and test code run with 2 parallel workers:
@pytest.fixture(scope='session')
def resource():
    print('Setup resource')
    yield
    print('Teardown resource')

def test_a(resource):
    pass

def test_b(resource):
    pass

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

  1. 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.
  2. Step 2: Calculate total setup calls

    With 2 workers, the fixture setup runs once per worker, so 'Setup resource' prints twice.
  3. Final Answer:

    Twice -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Cause: session scope runs per worker; Fix: use a database or external service to share state. -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Use scope='session' fixture and implement external resource locking (e.g., file lock or database). -> Option A
  4. Quick Check:

    External locking + session scope = single global setup [OK]
Hint: Combine session scope with external locking for global setup [OK]
Common Mistakes:
  • Assuming pytest-xdist shares session fixtures automatically
  • Using function scope and expecting single setup
  • Running tests sequentially defeats parallel purpose