Skip to content

Commit

Permalink
fix: request recording in unittest setUp method
Browse files Browse the repository at this point in the history
  • Loading branch information
zermelo-wisen committed Jun 7, 2024
1 parent 1d5a072 commit f90ca45
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
11 changes: 11 additions & 0 deletions _appmap/test/data/django/test/test_unittest_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

from unittest import TestCase

from django.test import Client

class DisabledRequestsRecordingTest(TestCase):
def setUp(self) -> None:
Client().get("/")

def test_request_in_setup(self):
pass
6 changes: 3 additions & 3 deletions _appmap/test/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def test_enabled(self, pytester):
# To really check middleware reset, the tests must run in order,
# so disable randomly.
result = pytester.runpytest("-svv", "-p", "no:randomly")
result.assert_outcomes(passed=4, failed=0, errors=0)
result.assert_outcomes(passed=5, failed=0, errors=0)
# Look for the http_server_request event in test_app's appmap. If
# middleware reset is broken, it won't be there.
appmap_file = pytester.path / "tmp" / "appmap" / "pytest" / "test_request.appmap.json"
Expand All @@ -213,7 +213,7 @@ def test_enabled(self, pytester):
def test_disabled(self, pytester, monkeypatch):
monkeypatch.setenv("_APPMAP", "false")
result = pytester.runpytest("-svv", "-p", "no:randomly", "-k", "test_request")
result.assert_outcomes(passed=1, failed=0, errors=0)
result.assert_outcomes(passed=2, failed=0, errors=0)
assert not (pytester.path / "tmp").exists()

def test_disabled_for_process(self, pytester, monkeypatch):
Expand All @@ -223,7 +223,7 @@ def test_disabled_for_process(self, pytester, monkeypatch):

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

assert (pytester.path / "tmp" / "appmap" / "process").exists()
assert not (pytester.path / "tmp" / "appmap" / "requests").exists()
Expand Down
12 changes: 12 additions & 0 deletions _appmap/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from contextlib import contextmanager

from _appmap import noappmap, testing_framework, wrapt
from _appmap.env import Env
from _appmap.utils import get_function_location

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

else:
# We need to disable request recording in TestCase._callSetUp too
# in order to prevent creation of a request recording besides test
# recording when requests are made inside setUp method.
# This edge case can be observed in this test in django project:
# $ APPMAP=TRUE ./runtests.py auth_tests.test_views.ChangelistTests.test_user_change_email
#  (ChangelistTests.setUp makes a request)
@wrapt.patch_function_wrapper("unittest.case", "TestCase._callSetUp")
def callSetUp(wrapped, test_case, args, kwargs): # pylint: disable=unused-argument
with Env.current.disabled("requests"):
wrapped(*args, **kwargs)

# As of 3.8, unittest.case.TestCase now calls the test's method indirectly, through
# TestCase._callTestMethod. Hook that to manage a recording session.
@wrapt.patch_function_wrapper("unittest.case", "TestCase._callTestMethod")
Expand Down

0 comments on commit f90ca45

Please sign in to comment.