Skip to content

Commit

Permalink
still failing some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EricGoulart committed Nov 3, 2024
1 parent 1220b21 commit d9aa5ac
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
27 changes: 20 additions & 7 deletions falcon/testing/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down
45 changes: 45 additions & 0 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
import warnings

import pytest

import falcon
Expand Down Expand Up @@ -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)

0 comments on commit d9aa5ac

Please sign in to comment.