@@ -190,6 +190,10 @@ def pytest_runtestloop(self, session: pytest.Session):
190190 Based on the default implementation in pytest, but also adds support
191191 for running the tests in an endless loop.
192192 """
193+ if not self .forever :
194+ # let the default pytest_runtestloop run if we don't need any
195+ # customization for --forever.
196+ return None
193197
194198 if (
195199 session .testsfailed
@@ -205,12 +209,10 @@ def pytest_runtestloop(self, session: pytest.Session):
205209 number_of_items = len (session .items )
206210 iter_number = 0
207211 idx = 0
208- next_idx = idx + 1
209- if self .forever :
210- next_idx = next_idx % number_of_items
212+ next_idx = (idx + 1 ) % number_of_items
211213
212214 while idx < number_of_items :
213- if idx == 0 and self . forever :
215+ if idx == 0 :
214216 print ("\n \n " , end = "" )
215217 print ("==========================================================" )
216218 print ("You ran the test suite with 'forever' mode enabled." )
@@ -228,9 +230,7 @@ def pytest_runtestloop(self, session: pytest.Session):
228230 raise session .Interrupted (session .shouldstop )
229231
230232 idx = next_idx
231- next_idx = idx + 1
232- if self .forever :
233- next_idx = next_idx % number_of_items
233+ next_idx = (idx + 1 ) % number_of_items
234234
235235 return True
236236
@@ -407,6 +407,14 @@ def thread_comp(num_parallel_threads):
407407
408408
409409def pytest_configure (config ):
410+ if (
411+ config .option .forever
412+ and (n := getattr (config .option , "numprocesses" , None )) is not None
413+ ):
414+ raise pytest .UsageError (
415+ f"--forever from pytest-run-parallel is incompatible with `-n { n } ` from pytest-xdist."
416+ )
417+
410418 config .addinivalue_line (
411419 "markers" ,
412420 "parallel_threads(n): run the given test function in parallel "
@@ -491,7 +499,8 @@ def pytest_addoption(parser):
491499 default = False ,
492500 help = "Run the test loop forever (starting from the top when all the tests have been run), "
493501 "until one crashes or the user explicitly stops the process with Ctrl-C. This is especially "
494- "helpful for hitting thread safety bugs that only occur rarely." ,
502+ "helpful for hitting thread safety bugs that only occur rarely. --forever is not compatible "
503+ "with -n from pytest-xdist." ,
495504 )
496505 parser .addini (
497506 "thread_unsafe_fixtures" ,
0 commit comments