From 5b3f9c4f4e2954cc3331b76bdbbba3c034c73bd7 Mon Sep 17 00:00:00 2001 From: Jarry Shaw Date: Sun, 1 Nov 2026 14:00:40 -0500 Subject: [PATCH] test: import pcapkit once per class in TCPHTTPDispatchTests (#2562) - TCPHTTPDispatchTests.setUp calls reimport_once_per_class(self) in place of a per-test purge_modules(['pcapkit']); its tests read TCP.__proto__ and parse fixed bytes, so none needs an import of its own. - test-coverage.rst's "Import once per class" bullet records the per-test form: purge in setUp instead of calling the helper, with a comment saying why, as ConstEnumRegisterFallbackTests does, and names test_per_class_import_guard.py as the check on undoing the helper. --- docs/source/contributing/conventions/test-coverage.rst | 8 ++++++++ tests/protocols/transport/test_tcp_http_dispatch_unit.py | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/source/contributing/conventions/test-coverage.rst b/docs/source/contributing/conventions/test-coverage.rst index 038c0301cc..30fc39d19d 100644 --- a/docs/source/contributing/conventions/test-coverage.rst +++ b/docs/source/contributing/conventions/test-coverage.rst @@ -143,6 +143,14 @@ Running the Tests ``purge_modules``, which re-imported the package before every test and dominated the suite's time (:issue:`1065`). Both live in :file:`tests/_support.py`; pass ``restore=True`` when the class must leave :data:`sys.modules` as it found it. + + A class whose tests each need their own import purges in ``setUp`` instead of + calling the helper, and says why in a comment there: ``ConstEnumRegisterFallbackTests`` + in :file:`tests/const/test_const_enum_builtin_parity.py` does, because a lookup in + one test grows the registries' tables the next one reads. Calling the helper and + then purging the whole package, later in ``setUp`` or in ``tearDown``, throws the + class's import away while reading as if it kept it; + :file:`tests/project/test_per_class_import_guard.py` fails on that (:issue:`1538`). * **Check ordering under plain unittest.** :file:`tests/conftest.py` heals cross-module import pollution after every test, so pytest alone cannot see it (:issue:`981`). :file:`util/run_unittest_leg.py` runs one :file:`tests/` subdirectory, then the diff --git a/tests/protocols/transport/test_tcp_http_dispatch_unit.py b/tests/protocols/transport/test_tcp_http_dispatch_unit.py index 46380c1eca..accffa766c 100644 --- a/tests/protocols/transport/test_tcp_http_dispatch_unit.py +++ b/tests/protocols/transport/test_tcp_http_dispatch_unit.py @@ -26,7 +26,7 @@ import unittest import warnings -from tests._support import purge_modules, time_limit +from tests._support import reimport_once_per_class, time_limit RUNTIME_DEPS = ('tbtrim', 'aenum', 'chardet', 'dictdumper') HAS_RUNTIME = all(importlib.util.find_spec(name) is not None for name in RUNTIME_DEPS) @@ -50,7 +50,7 @@ class TCPHTTPDispatchTests(unittest.TestCase): """TCP's port-80/8080 entries point at the proxy, and behave like it.""" def setUp(self) -> None: - purge_modules(['pcapkit']) + reimport_once_per_class(self) def test_tcp_binds_the_http_proxy_on_both_http_ports(self) -> None: """Ports 80 and 8080 resolve to ``application.http``, not ``application.httpv1``.