Commit 5eb90119 authored by FichteFoll's avatar FichteFoll

Monkeypatch unittest on function call, not on load

Doing changes like these on module load is very intransparent and the
explicit call of the function telegraphs this way better.

Inspired by requests:
https://github.com/kennethreitz/requests/blob/9bbab338fdbb562b923ba2d8a80f0bfba697fa41/requests/__init__.py#L53-L54
parent 190f6fd3
...@@ -24,12 +24,15 @@ if sys.version_info >= (3,): ...@@ -24,12 +24,15 @@ if sys.version_info >= (3,):
from urllib.request import urlopen from urllib.request import urlopen
from urllib.error import HTTPError from urllib.error import HTTPError
from urllib.parse import urljoin from urllib.parse import urljoin
generator_method_type = 'method' generator_method_type = 'method'
str_cls = str str_cls = str
else: else:
from . import unittest_compat # NOQA (monkeypatches the unittest module) from . import unittest_compat
from urlparse import urljoin from urlparse import urljoin
from urllib2 import HTTPError, urlopen from urllib2 import HTTPError, urlopen
unittest_compat.inject_into_unittest()
generator_method_type = 'instancemethod' generator_method_type = 'instancemethod'
str_cls = unicode # NOQA (obviously undefined in Py3) str_cls = unicode # NOQA (obviously undefined in Py3)
......
...@@ -3,7 +3,8 @@ import re ...@@ -3,7 +3,8 @@ import re
import unittest import unittest
if sys.version_info < (2, 7): def inject_into_unittest():
if sys.version_info < (2, 7):
def assertIn(self, member, container, msg=None): def assertIn(self, member, container, msg=None):
"""Just like self.assertTrue(a in b), but with a nicer default message.""" """Just like self.assertTrue(a in b), but with a nicer default message."""
if member not in container: if member not in container:
...@@ -62,6 +63,6 @@ if sys.version_info < (2, 7): ...@@ -62,6 +63,6 @@ if sys.version_info < (2, 7):
self.fail(msg) self.fail(msg)
unittest.TestCase.assertIsInstance = assertIsInstance unittest.TestCase.assertIsInstance = assertIsInstance
else: else:
unittest.TestCase.assertRegex = unittest.TestCase.assertRegexpMatches unittest.TestCase.assertRegex = unittest.TestCase.assertRegexpMatches
unittest.TestCase.assertNotRegex = unittest.TestCase.assertNotRegexpMatches unittest.TestCase.assertNotRegex = unittest.TestCase.assertNotRegexpMatches
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment