|
1 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 |
|
2 # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt |
|
3 |
|
4 """Implementations of unittest features from the future.""" |
|
5 |
|
6 # Use unittest2 if it's available, otherwise unittest. This gives us |
|
7 # back-ported features for 2.6. |
|
8 try: |
|
9 import unittest2 as unittest |
|
10 except ImportError: |
|
11 import unittest |
|
12 |
|
13 |
|
14 def unittest_has(method): |
|
15 """Does `unittest.TestCase` have `method` defined?""" |
|
16 return hasattr(unittest.TestCase, method) |
|
17 |
|
18 |
|
19 class TestCase(unittest.TestCase): |
|
20 """Just like unittest.TestCase, but with assert methods added. |
|
21 |
|
22 Designed to be compatible with 3.1 unittest. Methods are only defined if |
|
23 `unittest` doesn't have them. |
|
24 |
|
25 """ |
|
26 # pylint: disable=missing-docstring |
|
27 |
|
28 # Many Pythons have this method defined. But PyPy3 has a bug with it |
|
29 # somehow (https://bitbucket.org/pypy/pypy/issues/2092), so always use our |
|
30 # own implementation that works everywhere, at least for the ways we're |
|
31 # calling it. |
|
32 def assertCountEqual(self, s1, s2): |
|
33 """Assert these have the same elements, regardless of order.""" |
|
34 self.assertEqual(sorted(s1), sorted(s2)) |
|
35 |
|
36 if not unittest_has('assertRaisesRegex'): |
|
37 def assertRaisesRegex(self, *args, **kwargs): |
|
38 return self.assertRaisesRegexp(*args, **kwargs) |
|
39 |
|
40 if not unittest_has('assertRegex'): |
|
41 def assertRegex(self, *args, **kwargs): |
|
42 return self.assertRegexpMatches(*args, **kwargs) |