import pytest
import time
import threading
execution_log = []
@pytest.fixture(scope='module')
def resource():
return "shared_resource"
@pytest.mark.parametrize('i', range(4))
def test_parallel_execution(i, resource):
start = time.time()
thread_name = threading.current_thread().name
execution_log.append((i, thread_name, start))
time.sleep(0.5) # Simulate work
end = time.time()
execution_log.append((i, thread_name, end))
assert resource == "shared_resource"
def test_all_tests_ran():
# Check that all 4 tests ran
test_ids = set(entry[0] for entry in execution_log)
assert test_ids == {0,1,2,3}
# To run this test suite with parallel workers, use:
# pytest -n 2 --dist=loadscope
# or
# pytest -n 4
# The assertions verify all tests ran and used the shared fixture correctly.
The test suite defines 4 parameterized tests to simulate independent tests.
Each test logs its start and end time with the thread name to show parallel execution.
The shared fixture with module scope ensures no conflicts.
The final test verifies all tests ran by checking the log.
Running with pytest -n 2 or pytest -n 4 uses multiple workers to distribute tests.
The --dist=loadscope option groups tests by scope for distribution.
This setup follows best practices by avoiding shared state conflicts and verifying parallel execution.