55import pytest
66
77
8- def test_request_fixture_available (pytester ):
8+ def test_request_fixture_available (pytester , run_with_timeout ):
99 """
1010 Test that the request fixture is available on each test execution.
1111
@@ -22,18 +22,17 @@ def test_request_fixture_available(pytester):
2222 def test_request_fixture_available(request):
2323 global count
2424
25+ count += 1
26+ if count >= 5:
27+ stop_load_testing(request, "Request fixture verified")
28+
2529 # The request fixture should be available
2630 assert request is not None
2731 assert hasattr(request, 'node')
2832 assert hasattr(request, 'session')
29-
30- if count < 5:
31- count += 1
32- else:
33- stop_load_testing(request, "Request fixture verified")
3433 """ )
3534
36- result = pytester . runpytest ( "--load-test" , "-n" , "2" , "-v" )
35+ result = run_with_timeout ( pytester , "--load-test" , "-n" , "2" , "-v" , timeout = 5 )
3736
3837 # Should stop gracefully
3938 result .stdout .fnmatch_lines (
@@ -44,7 +43,7 @@ def test_request_fixture_available(request):
4443 assert result .ret == pytest .ExitCode .INTERRUPTED
4544
4645
47- def test_all_fixture_scopes (pytester ):
46+ def test_all_fixture_scopes (pytester , run_with_timeout ):
4847 """Test that fixtures with different scopes provide correct data."""
4948 pytester .makepyfile ("""
5049 import pytest
@@ -55,9 +54,11 @@ def test_all_fixture_scopes(pytester):
5554
5655 @pytest.fixture(scope="session")
5756 def fixture_counts(tmp_path_factory):
58- counts_file = tmp_path_factory.mktemp("data") / "fixture_counts.json"
57+ root_tmp_dir = tmp_path_factory.getbasetemp().parent
58+ counts_file = root_tmp_dir / "fixture_counts.json"
5959 lock_file = counts_file.with_suffix('.lock')
6060
61+ # Initialize file with a lock
6162 with FileLock(str(lock_file)):
6263 if not counts_file.exists():
6364 counts_file.write_text(json.dumps({
@@ -69,17 +70,19 @@ def fixture_counts(tmp_path_factory):
6970 class Counter:
7071 def __init__(self, file_path, lock_path):
7172 self.file = file_path
72- self.lock = lock_path
73+ self.lock_path = lock_path
7374
7475 def increment(self, key):
75- with FileLock(str(self.lock)):
76+ # Create new FileLock for each operation (xdist-safe)
77+ with FileLock(str(self.lock_path)):
7678 data = json.loads(self.file.read_text())
7779 data[key] += 1
7880 self.file.write_text(json.dumps(data))
7981 return data[key]
8082
8183 def read(self):
82- with FileLock(str(self.lock)):
84+ # Create new FileLock for each operation (xdist-safe)
85+ with FileLock(str(self.lock_path)):
8386 return json.loads(self.file.read_text())
8487
8588 counter = Counter(counts_file, lock_file)
@@ -90,20 +93,23 @@ def read(self):
9093
9194 @pytest.fixture(scope="session")
9295 def execution_counts(tmp_path_factory):
93- counts_file = tmp_path_factory.mktemp("data") / "execution_counts.json"
96+ root_tmp_dir = tmp_path_factory.getbasetemp().parent
97+ counts_file = root_tmp_dir / "execution_counts.json"
9498 lock_file = counts_file.with_suffix('.lock')
9599
100+ # Initialize file with a lock
96101 with FileLock(str(lock_file)):
97102 if not counts_file.exists():
98103 counts_file.write_text(json.dumps({"test1": 0, "test2": 0}))
99104
100105 class Counter:
101106 def __init__(self, file_path, lock_path):
102107 self.file = file_path
103- self.lock = lock_path
108+ self.lock_path = lock_path
104109
105110 def increment(self, key):
106- with FileLock(str(self.lock)):
111+ # Create new FileLock for each operation (xdist-safe)
112+ with FileLock(str(self.lock_path)):
107113 data = json.loads(self.file.read_text())
108114 data[key] += 1
109115 self.file.write_text(json.dumps(data))
@@ -175,7 +181,7 @@ def test_function_fixture_reinitialization(request, function_fixture, execution_
175181 stop_load_testing(request, f"All fixture scopes verified (test1: {execution_count_test1}, test2: {execution_count_test2})")
176182 """ )
177183
178- result = pytester . runpytest ( "--load-test" , "-n" , "1" , "-v" )
184+ result = run_with_timeout ( pytester , "--load-test" , "-n" , "1" , "-v" )
179185
180186 # Should stop gracefully
181187 result .stdout .fnmatch_lines (
0 commit comments