diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-05-20 23:29:07 +0300 |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-05-20 23:29:07 +0300 |
commit | 237600bbd51e8fb094d685138a922d25d3897787 (patch) | |
tree | 5136a9abb1bc2402a1f8cc537f361bda25e6a312 /Lib/unittest | |
parent | 0e94dd481b89b04359f8d61ec131ee9c9558675e (diff) | |
parent | 228f1231e888c907c639be42ee20ce86ec812a0b (diff) | |
download | cpython-237600bbd51e8fb094d685138a922d25d3897787.tar.gz |
Issue #27063: Some unittest loader tests were silently skipped.
Diffstat (limited to 'Lib/unittest')
-rw-r--r-- | Lib/unittest/mock.py | 20 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/support.py | 2 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testmagicmethods.py | 1 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testmock.py | 21 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testpatch.py | 4 |
5 files changed, 42 insertions, 6 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 86a5a3dfeb..123c156a96 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -772,6 +772,24 @@ class NonCallableMock(Base): (self._mock_name or 'mock', self.call_count)) raise AssertionError(msg) + def assert_called(_mock_self): + """assert that the mock was called at least once + """ + self = _mock_self + if self.call_count == 0: + msg = ("Expected '%s' to have been called." % + self._mock_name or 'mock') + raise AssertionError(msg) + + def assert_called_once(_mock_self): + """assert that the mock was called only once. + """ + self = _mock_self + if not self.call_count == 1: + msg = ("Expected '%s' to have been called once. Called %s times." % + (self._mock_name or 'mock', self.call_count)) + raise AssertionError(msg) + def assert_called_with(_mock_self, *args, **kwargs): """assert that the mock was called with the specified arguments. @@ -820,7 +838,7 @@ class NonCallableMock(Base): if expected not in all_calls: raise AssertionError( 'Calls not found.\nExpected: %r\n' - 'Actual: %r' % (calls, self.mock_calls) + 'Actual: %r' % (_CallList(calls), self.mock_calls) ) from cause return diff --git a/Lib/unittest/test/testmock/support.py b/Lib/unittest/test/testmock/support.py index f4738793b3..205431adca 100644 --- a/Lib/unittest/test/testmock/support.py +++ b/Lib/unittest/test/testmock/support.py @@ -1,5 +1,3 @@ -import sys - def is_instance(obj, klass): """Version of is_instance that doesn't access __class__""" return issubclass(type(obj), klass) diff --git a/Lib/unittest/test/testmock/testmagicmethods.py b/Lib/unittest/test/testmock/testmagicmethods.py index bb9b956bb2..24569b532d 100644 --- a/Lib/unittest/test/testmock/testmagicmethods.py +++ b/Lib/unittest/test/testmock/testmagicmethods.py @@ -1,5 +1,4 @@ import unittest -import inspect import sys from unittest.mock import Mock, MagicMock, _magics diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 5f82b82966..9910ab92d1 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1239,6 +1239,27 @@ class MockTest(unittest.TestCase): with self.assertRaises(AssertionError): m.hello.assert_not_called() + def test_assert_called(self): + m = Mock() + with self.assertRaises(AssertionError): + m.hello.assert_called() + m.hello() + m.hello.assert_called() + + m.hello() + m.hello.assert_called() + + def test_assert_called_once(self): + m = Mock() + with self.assertRaises(AssertionError): + m.hello.assert_called_once() + m.hello() + m.hello.assert_called_once() + + m.hello() + with self.assertRaises(AssertionError): + m.hello.assert_called_once() + #Issue21256 printout of keyword args should be in deterministic order def test_sorted_call_signature(self): m = Mock() diff --git a/Lib/unittest/test/testmock/testpatch.py b/Lib/unittest/test/testmock/testpatch.py index dfce3696d6..2e0c08f35f 100644 --- a/Lib/unittest/test/testmock/testpatch.py +++ b/Lib/unittest/test/testmock/testpatch.py @@ -10,9 +10,9 @@ from unittest.test.testmock import support from unittest.test.testmock.support import SomeClass, is_instance from unittest.mock import ( - NonCallableMock, CallableMixin, patch, sentinel, + NonCallableMock, CallableMixin, sentinel, MagicMock, Mock, NonCallableMagicMock, patch, _patch, - DEFAULT, call, _get_target, _patch + DEFAULT, call, _get_target ) |