Skip to content

Commit

Permalink
fix: generate AppMap data from django tests
Browse files Browse the repository at this point in the history
Django tests, i.e. those that inherit from django.test.TestCase, can be
run with pytest, using the pytest-django plugin. This changes insure
that such tests generate test recordings (and don't generate request
recordings).
  • Loading branch information
apotterri committed Aug 12, 2024
1 parent 69b83e2 commit c878c7d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
13 changes: 13 additions & 0 deletions _appmap/test/data/django/djangoapp/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from pathlib import Path


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# If the SECRET_KEY isn't defined we get the misleading error message
# CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.
SECRET_KEY = "3*+d^_kjnr2gz)4q2m(&&^%$p4fj5dk3%lz4pl3g4m-%6!gf&)"
Expand All @@ -10,3 +16,10 @@

# Turn off deprecation warning
USE_TZ = True

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
8 changes: 8 additions & 0 deletions _appmap/test/data/django/test/test_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.test import TestCase
from django.test import Client


class TestRequest(TestCase):
def test_request_test(self):
resp = self.client.get("/test")
assert resp.status_code == 200
14 changes: 14 additions & 0 deletions _appmap/test/test_test_frameworks.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ def test_enabled_no_test_cases(self, testdir, monkeypatch):
verify_expected_appmap(testdir, f"-numpy{numpy_version.major}-no-test-cases")
verify_expected_metadata(testdir)

@pytest.mark.example_dir("django")
def test_pytest_django(testdir):
result = testdir.runpytest("-svv", "-k", "test_request_test")
result.assert_outcomes(passed=1)
# django.test.TestCase is a subclass of unittest.TestCase, so recorder type is unittest
assert (
testdir.path
/ "tmp"
/ "appmap"
/ "unittest"
/ "test_test_request_TestRequest_test_request_test.appmap.json"
).exists()
assert not (testdir.path / "tmp" / "appmap" / "requests").exists()


@pytest.mark.example_dir("trial")
class TestPytestRunnerTrial(_TestTestRunner):
Expand Down
9 changes: 8 additions & 1 deletion appmap/pytest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from importlib.metadata import version

import pytest
try:
from pytest_django.django_compat import is_django_unittest
except ImportError:

def is_django_unittest(item):
return False


from _appmap import noappmap, testing_framework, wrapt
from _appmap.env import Env
Expand Down Expand Up @@ -51,7 +58,7 @@ def pytest_runtest_call(item):
# running the test case. (This nesting of function calls is
# verified by the expected appmap in the test for a unittest
# TestCase run by pytest.)
if hasattr(item, "_testcase"):
if hasattr(item, "_testcase") and not is_django_unittest(item):
setattr(
item._testcase, # pylint: disable=protected-access
"_appmap_pytest_recording",
Expand Down

0 comments on commit c878c7d

Please sign in to comment.