Skip to content

Commit 6956d6d

Browse files
Leverage unittest2 to make assertions about logs
Python 3.4 introduces an assertLogs for unittest.TestCase. This assertion has been backported to unittest2. Enable this assertion by installing unittest2 and monkey-patching TestCase for Python < 3.4.
1 parent 810be79 commit 6956d6d

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ do to make sure it is compatible with your Django version and environment.
6969
you run any other Django application test suite. Just type ``python manage.py
7070
test djangosaml2``.
7171

72+
Python 2 users need to ``pip install djangosaml2[test]`` in order to run the
73+
tests.
74+
7275
Then you have to add the ``djangosaml2.backends.Saml2Backend``
7376
authentication backend to the list of authentications backends.
7477
By default only the ModelBackend included in Django is configured.

setup.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@
1313
# limitations under the License.
1414

1515

16-
import os
1716
import codecs
17+
import os
18+
import sys
1819
from setuptools import setup, find_packages
1920

2021

2122
def read(*rnames):
2223
return codecs.open(os.path.join(os.path.dirname(__file__), *rnames), encoding='utf-8').read()
2324

2425

26+
extra = {'test': []}
27+
if sys.version_info < (3, 4):
28+
# Necessary to use assertLogs in tests
29+
extra['test'].append('unittest2')
30+
31+
2532
setup(
2633
name='djangosaml2',
2734
version='0.16.10',
@@ -65,4 +72,5 @@ def read(*rnames):
6572
'pysaml2==4.4.0',
6673
'defusedxml>=0.4.1'
6774
],
75+
extras_require=extra,
6876
)

tests/testprofiles/tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
import sys
18+
1719
from django.contrib.auth import get_user_model
1820
from django.contrib.auth.models import User as DjangoUserModel
1921
from django.test import TestCase, override_settings
@@ -22,6 +24,17 @@
2224

2325
User = get_user_model()
2426

27+
if sys.version_info < (3, 4):
28+
# Monkey-patch TestCase to add the assertLogs method introduced in
29+
# Python 3.4
30+
from unittest2.case import _AssertLogsContext
31+
32+
class LoggerTestCase(TestCase):
33+
def assertLogs(self, logger=None, level=None):
34+
return _AssertLogsContext(self, logger, level)
35+
36+
TestCase = LoggerTestCase
37+
2538

2639
class Saml2BackendTests(TestCase):
2740
def test_update_user(self):

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ deps =
1616
django110: Django>=1.10,<1.11
1717
django111: Django>=1.11,<2.0
1818
djangomaster: https://github.com/django/django/archive/master.tar.gz
19+
.[test]
1920

2021
ignore_outcome =
2122
djangomaster: True

0 commit comments

Comments
 (0)