-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathtest_asserts.py
134 lines (96 loc) · 3.96 KB
/
test_asserts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
Tests the dynamic loading of all Django assertion cases.
"""
from __future__ import annotations
import inspect
import django.test
import pytest
from .helpers import DjangoPytester
import pytest_django
from pytest_django.asserts import __all__ as asserts_all
def _get_actual_assertions_names() -> list[str]:
"""
Returns list with names of all assertion helpers in Django.
"""
from unittest import TestCase as DefaultTestCase
from django import VERSION
from django.test import TestCase as DjangoTestCase
if VERSION >= (5, 0):
from django.contrib.messages.test import MessagesTestMixin
class MessagesTestCase(MessagesTestMixin, DjangoTestCase):
pass
obj = MessagesTestCase("run")
else:
obj = DjangoTestCase("run")
def is_assert(func) -> bool:
return func.startswith("assert") and "_" not in func
base_methods = [
name for name, member in inspect.getmembers(DefaultTestCase) if is_assert(name)
]
return [
name
for name, member in inspect.getmembers(obj)
if is_assert(name) and name not in base_methods
]
def test_django_asserts_available() -> None:
django_assertions = _get_actual_assertions_names()
expected_assertions = asserts_all
assert set(django_assertions) == set(expected_assertions)
for name in expected_assertions:
assert hasattr(pytest_django.asserts, name)
@pytest.mark.django_db
def test_sanity() -> None:
from django.http import HttpResponse
from pytest_django.asserts import assertContains, assertNumQueries
response = HttpResponse("My response")
assertContains(response, "My response")
with pytest.raises(AssertionError):
assertContains(response, "Not my response")
assertNumQueries(0, lambda: 1 + 1)
with assertNumQueries(0):
pass
assert assertContains.__doc__
def test_fixture_assert(django_testcase: django.test.TestCase) -> None:
django_testcase.assertEqual("a", "a") # noqa: PT009
with pytest.raises(AssertionError):
django_testcase.assertXMLEqual("a" * 10_000, "a")
class TestInternalDjangoAssert:
def test_fixture_assert(self, django_testcase: django.test.TestCase) -> None:
assert django_testcase != self
django_testcase.assertEqual("a", "a") # noqa: PT009
assert not hasattr(self, "assertEqual")
with pytest.raises(AssertionError):
django_testcase.assertXMLEqual("a" * 10_000, "a")
@pytest.mark.django_project(create_manage_py=True)
def test_django_test_case_assert(django_pytester: DjangoPytester) -> None:
django_pytester.create_test_module(
"""
import pytest
import django.test
class TestDjangoAssert(django.test.TestCase):
def test_fixture_assert(self, django_testcase: django.test.TestCase) -> None:
assert False, "Cannot use the fixture"
def test_normal_assert(self) -> None:
self.assertEqual("a", "a")
with pytest.raises(AssertionError):
self.assertXMLEqual("a" * 10_000, "a")
"""
)
result = django_pytester.runpytest_subprocess()
result.assert_outcomes(failed=1, passed=1)
assert "missing 1 required positional argument: 'django_testcase'" in result.stdout.str()
@pytest.mark.django_project(create_manage_py=True)
def test_unittest_assert(django_pytester: DjangoPytester) -> None:
django_pytester.create_test_module(
"""
import unittest
class TestUnittestAssert(unittest.TestCase):
def test_fixture_assert(self, django_testcase: unittest.TestCase) -> None:
assert False, "Cannot use the fixture"
def test_normal_assert(self) -> None:
self.assertEqual("a", "a")
"""
)
result = django_pytester.runpytest_subprocess()
result.assert_outcomes(failed=1, passed=1)
assert "missing 1 required positional argument: 'django_testcase'" in result.stdout.str()