Skip to content

Commit 2627a60

Browse files
committed
tests: reorder to unpin pytest
Fix: getsentry#3035 Rearrange test items so that forked tests come before normal tests within their respective modules. Swap the last forked test with the last normal test if necessary. Workaround to unpin pytest. See: pytest-dev/pytest#9621, pytest-dev/pytest-forked#67, and specifically: pytest-dev/pytest-forked#67 (comment)
1 parent 5080c76 commit 2627a60

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

Diff for: requirements-devenv.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
-r requirements-linting.txt
22
-r requirements-testing.txt
33
mockupdb # required by `pymongo` tests that are enabled by `pymongo` from linter requirements
4-
pytest<7.0.0 # https://github.com/pytest-dev/pytest/issues/9621; see tox.ini
54
pytest-asyncio

Diff for: tests/conftest.py

+39
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import socket
44
import warnings
5+
from collections import OrderedDict
56
from threading import Thread
67
from contextlib import contextmanager
78
from http.server import BaseHTTPRequestHandler, HTTPServer
@@ -645,3 +646,41 @@ def __eq__(self, other):
645646

646647
def __ne__(self, other):
647648
return not self.__eq__(other)
649+
650+
651+
def pytest_collection_modifyitems(config, items):
652+
"""
653+
Rearrange test items so that forked tests come before normal tests within their respective modules.
654+
Swap the last forked test with the last normal test if necessary.
655+
656+
Workaround to unpin pytest. See:
657+
https://github.com/pytest-dev/pytest/issues/9621,
658+
https://github.com/pytest-dev/pytest-forked/issues/67, and specifically:
659+
https://github.com/pytest-dev/pytest-forked/issues/67#issuecomment-1964718720
660+
"""
661+
module_states = OrderedDict()
662+
663+
for idx in range(len(items)):
664+
item = items[idx]
665+
current_module = item.module.__name__
666+
667+
if current_module not in module_states:
668+
module_states[current_module] = {"forked": [], "normal": []}
669+
670+
if "forked" in item.keywords:
671+
module_states[current_module]["forked"].append(idx)
672+
else:
673+
module_states[current_module]["normal"].append(idx)
674+
675+
# Swap the last forked test with the last normal test if necessary
676+
for states in module_states.values():
677+
if states["forked"] and states["normal"]:
678+
last_forked_idx = states["forked"][-1]
679+
last_normal_idx = states["normal"][-1]
680+
681+
if last_forked_idx > last_normal_idx:
682+
# Swap the items
683+
items[last_forked_idx], items[last_normal_idx] = (
684+
items[last_normal_idx],
685+
items[last_forked_idx],
686+
)

Diff for: tox.ini

-10
Original file line numberDiff line numberDiff line change
@@ -290,19 +290,10 @@ deps =
290290
# === Common ===
291291
py3.8-common: hypothesis
292292
{py3.6,py3.7,py3.8,py3.9,py3.10,py3.11,py3.12,py3.13}-common: pytest-asyncio
293-
# See https://github.com/pytest-dev/pytest/issues/9621
294-
# and https://github.com/pytest-dev/pytest-forked/issues/67
295-
# for justification of the upper bound on pytest
296-
{py3.6,py3.7,py3.8,py3.9,py3.10,py3.11,py3.12}-common: pytest<7.0.0
297-
py3.13-common: pytest
298293

299294
# === Gevent ===
300295
{py3.6,py3.7,py3.8,py3.9,py3.10,py3.11}-gevent: gevent>=22.10.0, <22.11.0
301296
{py3.12}-gevent: gevent
302-
# See https://github.com/pytest-dev/pytest/issues/9621
303-
# and https://github.com/pytest-dev/pytest-forked/issues/67
304-
# for justification of the upper bound on pytest
305-
{py3.6,py3.7,py3.8,py3.9,py3.10,py3.11,py3.12}-gevent: pytest<7.0.0
306297

307298
# === Integrations ===
308299

@@ -372,7 +363,6 @@ deps =
372363
celery-latest: Celery
373364

374365
celery: newrelic
375-
celery: pytest<7
376366
{py3.7}-celery: importlib-metadata<5.0
377367

378368
# Chalice

0 commit comments

Comments
 (0)