Skip to content

Commit

Permalink
Make Django's assertion helpers available in pytest_django.asserts (#709
Browse files Browse the repository at this point in the history
)

Fixes #97.

Co-authored-by: Daniel Hahler <github@thequod.de>
  • Loading branch information
wgordon17 and blueyed committed Jan 13, 2020
1 parent c334d26 commit febbff4
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/helpers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
Django helpers
==============

Assertions
----------

All of Django's :py:class:`~django:django.test.TestCase`
:ref:`django:assertions` are available in ``pytest_django.asserts``, e.g.

::

from pytest_django.asserts import assertTemplateUsed

Markers
-------

Expand Down
34 changes: 34 additions & 0 deletions pytest_django/asserts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Dynamically load all Django assertion cases and expose them for importing.
"""
from functools import wraps
from django.test import (
TestCase, SimpleTestCase,
LiveServerTestCase, TransactionTestCase
)

test_case = TestCase('run')


def _wrapper(name):
func = getattr(test_case, name)

@wraps(func)
def assertion_func(*args, **kwargs):
return func(*args, **kwargs)

return assertion_func


__all__ = []
assertions_names = set()
assertions_names.update(
set(attr for attr in vars(TestCase) if attr.startswith('assert')),
set(attr for attr in vars(SimpleTestCase) if attr.startswith('assert')),
set(attr for attr in vars(LiveServerTestCase) if attr.startswith('assert')),
set(attr for attr in vars(TransactionTestCase) if attr.startswith('assert')),
)

for assert_func in assertions_names:
globals()[assert_func] = _wrapper(assert_func)
__all__.append(assert_func)
56 changes: 56 additions & 0 deletions tests/test_asserts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Tests the dynamic loading of all Django assertion cases.
"""
import inspect

import pytest
import pytest_django

from pytest_django.asserts import __all__ as asserts_all


def _get_actual_assertions_names():
"""
Returns list with names of all assertion helpers in Django.
"""
from django.test import TestCase as DjangoTestCase
from unittest import TestCase as DefaultTestCase

obj = DjangoTestCase('run')

def is_assert(func):
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():
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():
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__

0 comments on commit febbff4

Please sign in to comment.