-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Django's assertion helpers available in pytest_django.asserts (#709
) Fixes #97. Co-authored-by: Daniel Hahler <[email protected]>
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ |