diff --git a/falcon/testing/test_case.py b/falcon/testing/test_case.py index 632b662b7..1026adc86 100644 --- a/falcon/testing/test_case.py +++ b/falcon/testing/test_case.py @@ -18,18 +18,25 @@ utilities for simulating and validating HTTP requests. """ -try: - import testtools as unittest -except ImportError: # pragma: nocover - import unittest +import os +import warnings import falcon -import falcon.request - -# TODO hoist for backwards compat. Remove in falcon 4. from falcon.testing.client import Result # NOQA from falcon.testing.client import TestClient +USE_TESTTOOLS = os.getenv('USE_TESTTOOLS', 'false').lower() == 'true' + +try: + if USE_TESTTOOLS: + import testtools as unittest + else: + import unittest +except ImportError: # pragma: nocover + import unittest + + USE_TESTTOOLS = False + class TestCase(unittest.TestCase, TestClient): """Extends :mod:`unittest` to support WSGI/ASGI functional testing. @@ -47,6 +54,12 @@ class TestCase(unittest.TestCase, TestClient): :class:`unittest.TestCase` or :class:`testtools.TestCase`. """ + if USE_TESTTOOLS: + warnings.warn( + 'The use of "testtools.TestCase" is deprecated and will be removed ' + 'in Falcon 5.0. Please migrate to "pytest" or "unittest" ' + ) + # NOTE(vytas): Here we have to restore __test__ to allow collecting tests! __test__ = True diff --git a/tests/test_testing.py b/tests/test_testing.py index 89a8c49e8..fe1aa6319 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -1,3 +1,6 @@ +import os +import warnings + import pytest import falcon @@ -224,3 +227,45 @@ def capture_method(req, resp): assert result.status_code == 200 expected = '' if method == 'HEAD' else method assert result.text == expected + + +def test_testclient_initialization(): + class SampleResource: + def on_get(self, req, resp): + resp.media = {'message': 'Hello'} + + class SampleTestCase(testing.TestCase): + def setUp(self): + super().setUp() + self.app = falcon.App() + self.app.add_route('/hello', SampleResource()) + + test_case = SampleTestCase() + test_case.setUp() + client = testing.TestClient(test_case.app) + + response = client.simulate_get('/hello') + assert response.status_code == 200 + assert response.json == {'message': 'Hello'} + + +def test_deprecation_warning_with_testtools(): + os.environ['USE_TESTTOOLS'] = 'true' + + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + + class SampleTestCase(testing.TestCase): + def setUp(self): + super().setUp() + self.app = falcon.App() + + test_case = SampleTestCase() + test_case.setUp() + + assert any( + 'The use of "testtools.TestCase" is deprecated' in str(warning.message) + for warning in w + ), 'Expected deprecation warning not found' + + os.environ.pop('USE_TESTTOOLS', None)