0
0
PyTesttesting~15 mins

Fixture scope with parallel tests in PyTest - Build an Automation Script

Choose your learning style9 modes available
Verify fixture scope behavior with parallel pytest tests
Preconditions (3)
Step 1: Run the tests in parallel using pytest-xdist with 2 workers
Step 2: Observe how many times the fixture setup and teardown run
Step 3: Verify that the fixture setup runs once per worker due to module scope
✅ Expected Result: Fixture setup and teardown run exactly twice in total (once per worker), and both tests pass
Automation Requirements - pytest
Assertions Needed:
Verify both test functions pass
Verify fixture setup and teardown run expected number of times (twice total)
Best Practices:
Use pytest fixtures with proper scope
Use pytest-xdist for parallel test execution
Use print statements or logging to track fixture setup/teardown
Avoid sharing state between workers
Automated Solution
PyTest
import pytest

setup_counter = 0
teardown_counter = 0

@pytest.fixture(scope='module')
def resource():
    global setup_counter
    setup_counter += 1
    print(f'Setup called {setup_counter} time(s)')
    yield
    global teardown_counter
    teardown_counter += 1
    print(f'Teardown called {teardown_counter} time(s)')


def test_one(resource):
    assert True


def test_two(resource):
    assert True

if __name__ == '__main__':
    import subprocess
    # Run pytest with 2 parallel workers
    result = subprocess.run(['pytest', '-n', '2', '--capture=no'], capture_output=True, text=True)
    print(result.stdout)
    # Check output for setup/teardown counts
    setup_runs = result.stdout.count('Setup called')
    teardown_runs = result.stdout.count('Teardown called')
    assert setup_runs == 2, f'Expected 2 setup calls, got {setup_runs}'
    assert teardown_runs == 2, f'Expected 2 teardown calls, got {teardown_runs}'
    assert 'failed' not in result.stdout.lower(), 'Some tests failed'

This code defines a pytest fixture resource with module scope. It increments counters and prints messages when setup and teardown run.

Two test functions use this fixture. When running with pytest-xdist using 2 workers (-n 2), the fixture setup and teardown run once per worker, so twice total.

The __main__ block runs pytest programmatically with parallel workers and captures output. It counts how many times the setup and teardown messages appear to verify the fixture scope behavior.

Assertions check that both tests pass and the fixture setup/teardown run exactly twice, matching the expected behavior for module scope with parallel workers.

This approach helps beginners see how fixture scope affects parallel test runs and how to verify it.

Common Mistakes - 4 Pitfalls
{'mistake': 'Using function scope fixture expecting it to run once per module', 'why_bad': 'Function scope fixtures run before each test function, so setup runs multiple times, not once per module', 'correct_approach': "Set fixture scope to 'module' to run setup once per module per worker"}
{'mistake': 'Not using pytest-xdist and expecting parallel execution', 'why_bad': 'Without pytest-xdist, tests run sequentially, so parallel fixture behavior cannot be observed', 'correct_approach': "Install and use pytest-xdist with '-n' option to run tests in parallel"}
Sharing mutable global state between workers
{'mistake': "Using print statements without '--capture=no' option", 'why_bad': 'Pytest captures output by default, so print statements may not appear in console, confusing beginners', 'correct_approach': "Run pytest with '--capture=no' to see print output during test runs"}
Bonus Challenge

Now add data-driven testing with 3 different inputs using the same fixture and verify fixture setup runs only twice total

Show Hint