|
| 1 | +""" |
| 2 | +Tests the dynamic loading of all Django assertion cases. |
| 3 | +""" |
| 4 | +import inspect |
| 5 | + |
| 6 | +import pytest |
| 7 | +import pytest_django |
| 8 | + |
| 9 | +from pytest_django.asserts import __all__ as asserts_all |
| 10 | + |
| 11 | + |
| 12 | +def _get_actual_assertions_names(): |
| 13 | + """ |
| 14 | + Returns list with names of all assertion helpers in Django. |
| 15 | + """ |
| 16 | + from django.test import TestCase as DjangoTestCase |
| 17 | + from unittest import TestCase as DefaultTestCase |
| 18 | + |
| 19 | + obj = DjangoTestCase('run') |
| 20 | + |
| 21 | + def is_assert(func): |
| 22 | + return func.startswith('assert') and '_' not in func |
| 23 | + |
| 24 | + base_methods = [name for name, member in |
| 25 | + inspect.getmembers(DefaultTestCase) |
| 26 | + if is_assert(name)] |
| 27 | + |
| 28 | + return [name for name, member in inspect.getmembers(obj) |
| 29 | + if is_assert(name) and name not in base_methods] |
| 30 | + |
| 31 | + |
| 32 | +def test_django_asserts_available(): |
| 33 | + django_assertions = _get_actual_assertions_names() |
| 34 | + expected_assertions = asserts_all |
| 35 | + assert set(django_assertions) == set(expected_assertions) |
| 36 | + |
| 37 | + for name in expected_assertions: |
| 38 | + assert hasattr(pytest_django.asserts, name) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.django_db |
| 42 | +def test_sanity(): |
| 43 | + from django.http import HttpResponse |
| 44 | + from pytest_django.asserts import assertContains, assertNumQueries |
| 45 | + |
| 46 | + response = HttpResponse('My response') |
| 47 | + |
| 48 | + assertContains(response, 'My response') |
| 49 | + with pytest.raises(AssertionError): |
| 50 | + assertContains(response, 'Not my response') |
| 51 | + |
| 52 | + assertNumQueries(0, lambda: 1 + 1) |
| 53 | + with assertNumQueries(0): |
| 54 | + pass |
| 55 | + |
| 56 | + assert assertContains.__doc__ |
0 commit comments