From 8952f71a5a0c762040f7a25f07e5652f4bb1cec4 Mon Sep 17 00:00:00 2001 From: "eric.goulart" Date: Mon, 11 Nov 2024 15:37:10 -0300 Subject: [PATCH] updating changes, still missing test covarage --- ...kingchange.rst => 2156.newandimproved.rst} | 0 falcon/testing/test_case.py | 42 ++++++++++++------- 2 files changed, 27 insertions(+), 15 deletions(-) rename docs/_newsfragments/{2156.breakingchange.rst => 2156.newandimproved.rst} (100%) diff --git a/docs/_newsfragments/2156.breakingchange.rst b/docs/_newsfragments/2156.newandimproved.rst similarity index 100% rename from docs/_newsfragments/2156.breakingchange.rst rename to docs/_newsfragments/2156.newandimproved.rst diff --git a/falcon/testing/test_case.py b/falcon/testing/test_case.py index 856961406..b995c87e7 100644 --- a/falcon/testing/test_case.py +++ b/falcon/testing/test_case.py @@ -18,34 +18,34 @@ utilities for simulating and validating HTTP requests. """ +from importlib import import_module import os -import unittest import warnings +try: + import testtools as unittest +except ImportError: # pragma: nocover + import unittest + import falcon import falcon.request from falcon.testing.client import Result # NOQA from falcon.testing.client import TestClient -base_case = os.environ.get('FALCON_BASE_TEST_CASE') +# TODO hoist for backwards compat. Remove in falcon 5. -if base_case and 'testtools.TestCase' in base_case: - try: - import testtools +base_case_path = os.environ.get('FALCON_BASE_TEST_CASE') - BaseTestCase = testtools.TestCase - except ImportError: - BaseTestCase = unittest.TestCase -elif base_case is None: +if base_case_path: try: - import testtools - + module_path, class_name = base_case_path.rsplit('.', 1) + module = import_module(module_path) + BaseTestCase = getattr(module, class_name) + except (ImportError, AttributeError): warnings.warn( - 'Support for testtools is deprecated and will be removed in Falcon 5.0.', - DeprecationWarning, + f"Could not import '{base_case_path}', defaulting to testtools.TestCase.", + ImportWarning, ) - BaseTestCase = testtools.TestCase - except ImportError: BaseTestCase = unittest.TestCase else: BaseTestCase = unittest.TestCase @@ -91,6 +91,7 @@ def test_get_message(self): self.assertEqual(result.json, doc) """ + # NOTE(vytas): Here we have to restore __test__ to allow collecting tests! __test__ = True app: falcon.App @@ -116,6 +117,17 @@ def setUp(self): def setUp(self) -> None: super(TestCase, self).setUp() + if ( + BaseTestCase == unittest.TestCase + and os.environ.get('FALCON_BASE_TEST_CASE') is None + and 'testtools' in str(unittest) + ): + warnings.warn( + 'Support for testtools is deprecated' + 'and will be removed in Falcon 5.0.', + DeprecationWarning, + ) + app = falcon.App() # NOTE(kgriffs): Don't use super() to avoid triggering