summaryrefslogtreecommitdiff
path: root/Lib/unittest
diff options
context:
space:
mode:
authorMartin Panter <vadmium+py@gmail.com>2016-07-11 12:54:44 +0000
committerMartin Panter <vadmium+py@gmail.com>2016-07-11 12:54:44 +0000
commitd7b522282ed6d8326fdb8e22f4e6a28d2060fb68 (patch)
tree44f544927e6600116d1d5d4a9125f0890eb64ddd /Lib/unittest
parentd93a89ad6a774ac637ce1676f82cb0c07ff1cd1d (diff)
parente9b081cab28d648b14bace6b419157622d020a90 (diff)
downloadcpython-d7b522282ed6d8326fdb8e22f4e6a28d2060fb68.tar.gz
Issue #23804: Merge spelling and NEWS fixes from 3.5
Diffstat (limited to 'Lib/unittest')
-rw-r--r--Lib/unittest/mock.py27
-rw-r--r--Lib/unittest/test/testmock/support.py2
-rw-r--r--Lib/unittest/test/testmock/testmagicmethods.py1
-rw-r--r--Lib/unittest/test/testmock/testmock.py39
-rw-r--r--Lib/unittest/test/testmock/testpatch.py4
5 files changed, 66 insertions, 7 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 7f30b287ae..4f91c44c6a 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -523,7 +523,7 @@ class NonCallableMock(Base):
side_effect = property(__get_side_effect, __set_side_effect)
- def reset_mock(self, visited=None):
+ def reset_mock(self, visited=None,*, return_value=False, side_effect=False):
"Restore the mock object to its initial state."
if visited is None:
visited = []
@@ -538,6 +538,11 @@ class NonCallableMock(Base):
self.call_args_list = _CallList()
self.method_calls = _CallList()
+ if return_value:
+ self._mock_return_value = DEFAULT
+ if side_effect:
+ self._mock_side_effect = None
+
for child in self._mock_children.values():
if isinstance(child, _SpecState):
continue
@@ -772,6 +777,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 +843,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..b07a7cc318 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()
@@ -1256,6 +1277,24 @@ class MockTest(unittest.TestCase):
self.assertEqual(m.method_calls[0], c)
self.assertEqual(m.method_calls[1], i)
+ def test_reset_return_sideeffect(self):
+ m = Mock(return_value=10, side_effect=[2,3])
+ m.reset_mock(return_value=True, side_effect=True)
+ self.assertIsInstance(m.return_value, Mock)
+ self.assertEqual(m.side_effect, None)
+
+ def test_reset_return(self):
+ m = Mock(return_value=10, side_effect=[2,3])
+ m.reset_mock(return_value=True)
+ self.assertIsInstance(m.return_value, Mock)
+ self.assertNotEqual(m.side_effect, None)
+
+ def test_reset_sideeffect(self):
+ m = Mock(return_value=10, side_effect=[2,3])
+ m.reset_mock(side_effect=True)
+ self.assertEqual(m.return_value, 10)
+ self.assertEqual(m.side_effect, None)
+
def test_mock_add_spec(self):
class _One(object):
one = 1
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
)