1 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 |
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 |
2 # For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt |
3 |
3 |
4 """Implementations of unittest features from the future.""" |
4 """Implementations of unittest features from the future.""" |
5 |
5 |
6 # Use unittest2 if it's available, otherwise unittest. This gives us |
6 import unittest |
7 # back-ported features for 2.6. |
|
8 try: |
|
9 import unittest2 as unittest |
|
10 except ImportError: |
|
11 import unittest |
|
12 |
7 |
13 |
8 |
14 def unittest_has(method): |
9 def unittest_has(method): |
15 """Does `unittest.TestCase` have `method` defined?""" |
10 """Does `unittest.TestCase` have `method` defined?""" |
16 return hasattr(unittest.TestCase, method) |
11 return hasattr(unittest.TestCase, method) |
21 |
16 |
22 Designed to be compatible with 3.1 unittest. Methods are only defined if |
17 Designed to be compatible with 3.1 unittest. Methods are only defined if |
23 `unittest` doesn't have them. |
18 `unittest` doesn't have them. |
24 |
19 |
25 """ |
20 """ |
26 # pylint: disable=missing-docstring |
21 # pylint: disable=arguments-differ, deprecated-method |
27 |
22 |
28 # Many Pythons have this method defined. But PyPy3 has a bug with it |
23 if not unittest_has('assertCountEqual'): |
29 # somehow (https://bitbucket.org/pypy/pypy/issues/2092), so always use our |
24 def assertCountEqual(self, *args, **kwargs): |
30 # own implementation that works everywhere, at least for the ways we're |
25 return self.assertItemsEqual(*args, **kwargs) |
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 |
26 |
36 if not unittest_has('assertRaisesRegex'): |
27 if not unittest_has('assertRaisesRegex'): |
37 def assertRaisesRegex(self, *args, **kwargs): |
28 def assertRaisesRegex(self, *args, **kwargs): |
38 return self.assertRaisesRegexp(*args, **kwargs) |
29 return self.assertRaisesRegexp(*args, **kwargs) |
39 |
30 |