Skip to content

Commit ab2770e

Browse files
authored
Merge pull request #338 from getappmap/fix/request-recording-in-unittest-setup
fix: request recording in unittest setUp method
2 parents 965f987 + 1ee69cb commit ab2770e

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
from unittest import TestCase
3+
4+
from django.test import Client
5+
6+
class DisabledRequestsRecordingTest(TestCase):
7+
def setUp(self) -> None:
8+
Client().get("/")
9+
10+
def test_request_in_setup(self):
11+
pass

_appmap/test/test_django.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,21 @@ def test_enabled(self, pytester):
201201
# To really check middleware reset, the tests must run in order,
202202
# so disable randomly.
203203
result = pytester.runpytest("-svv", "-p", "no:randomly")
204-
result.assert_outcomes(passed=4, failed=0, errors=0)
204+
result.assert_outcomes(passed=5, failed=0, errors=0)
205205
# Look for the http_server_request event in test_app's appmap. If
206206
# middleware reset is broken, it won't be there.
207207
appmap_file = pytester.path / "tmp" / "appmap" / "pytest" / "test_request.appmap.json"
208-
assert not os.path.exists(pytester.path / "tmp" / "appmap" / "requests")
208+
assert not os.path.exists(
209+
pytester.path / "tmp" / "appmap" / "requests"
210+
), "django tests generated request recordings"
209211

210212
events = json.loads(appmap_file.read_text())["events"]
211213
assert "http_server_request" in events[0]
212214

213215
def test_disabled(self, pytester, monkeypatch):
214216
monkeypatch.setenv("_APPMAP", "false")
215217
result = pytester.runpytest("-svv", "-p", "no:randomly", "-k", "test_request")
216-
result.assert_outcomes(passed=1, failed=0, errors=0)
218+
result.assert_outcomes(passed=2, failed=0, errors=0)
217219
assert not (pytester.path / "tmp").exists()
218220

219221
def test_disabled_for_process(self, pytester, monkeypatch):
@@ -223,7 +225,7 @@ def test_disabled_for_process(self, pytester, monkeypatch):
223225

224226
# There are two tests for remote recording. They should both fail,
225227
# because process recording should disable remote recording.
226-
result.assert_outcomes(passed=2, failed=2, errors=0)
228+
result.assert_outcomes(passed=3, failed=2, errors=0)
227229

228230
assert (pytester.path / "tmp" / "appmap" / "process").exists()
229231
assert not (pytester.path / "tmp" / "appmap" / "requests").exists()

_appmap/unittest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from contextlib import contextmanager
44

55
from _appmap import noappmap, testing_framework, wrapt
6+
from _appmap.env import Env
67
from _appmap.utils import get_function_location
78

89
_session = testing_framework.session("unittest", "tests")
@@ -52,6 +53,17 @@ def _args(test_case, *_, isTest=False, **__):
5253
yield
5354

5455
else:
56+
# We need to disable request recording in TestCase._callSetUp too
57+
# in order to prevent creation of a request recording besides test
58+
# recording when requests are made inside setUp method.
59+
# This edge case can be observed in this test in django project:
60+
# $ APPMAP=TRUE ./runtests.py auth_tests.test_views.ChangelistTests.test_user_change_email
61+
#  (ChangelistTests.setUp makes a request)
62+
@wrapt.patch_function_wrapper("unittest.case", "TestCase._callSetUp")
63+
def callSetUp(wrapped, test_case, args, kwargs): # pylint: disable=unused-argument
64+
with Env.current.disabled("requests"):
65+
wrapped(*args, **kwargs)
66+
5567
# As of 3.8, unittest.case.TestCase now calls the test's method indirectly, through
5668
# TestCase._callTestMethod. Hook that to manage a recording session.
5769
@wrapt.patch_function_wrapper("unittest.case", "TestCase._callTestMethod")

0 commit comments

Comments
 (0)