diff options
author | Brett Cannon <brett@python.org> | 2012-04-21 22:53:14 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2012-04-21 22:53:14 (GMT) |
commit | b582c923ba4b0833e840360994743b68e0dfe219 (patch) | |
tree | 459095eb90f234794d70379d5a0c3653e1acdbb4 /Lib | |
parent | a64faf0771bceee789dd345202919147f595bfd3 (diff) | |
parent | 9cf5c9d85e3b904a76acac3cbcdc0d45c4e486b5 (diff) | |
download | cpython-b582c923ba4b0833e840360994743b68e0dfe219.zip cpython-b582c923ba4b0833e840360994743b68e0dfe219.tar.gz cpython-b582c923ba4b0833e840360994743b68e0dfe219.tar.bz2 |
merge
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/unittest/mock.py | 20 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testhelpers.py | 15 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testmock.py | 10 |
3 files changed, 38 insertions, 7 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 8cedcb4..a94acd6 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -78,11 +78,14 @@ def _getsignature(func, skipfirst, instance=False): return try: - regargs, varargs, varkwargs, defaults = inspect.getargspec(func) + argspec = inspect.getfullargspec(func) except TypeError: # C function / method, possibly inherited object().__init__ return + regargs, varargs, varkw, defaults, kwonly, kwonlydef, ann = argspec + + # instance methods and classmethods need to lose the self argument if getattr(func, '__self__', None) is not None: regargs = regargs[1:] @@ -90,8 +93,9 @@ def _getsignature(func, skipfirst, instance=False): # this condition and the above one are never both True - why? regargs = regargs[1:] - signature = inspect.formatargspec(regargs, varargs, varkwargs, defaults, - formatvalue=lambda value: "") + signature = inspect.formatargspec( + regargs, varargs, varkw, defaults, + kwonly, kwonlydef, ann, formatvalue=lambda value: "") return signature[1:-1], func @@ -891,7 +895,10 @@ class CallableMixin(Base): raise effect if not _callable(effect): - return next(effect) + result = next(effect) + if _is_exception(result): + raise result + return result ret_val = effect(*args, **kwargs) if ret_val is DEFAULT: @@ -931,8 +938,9 @@ class Mock(CallableMixin, NonCallableMock): arguments as the mock, and unless it returns `DEFAULT`, the return value of this function is used as the return value. - Alternatively `side_effect` can be an exception class or instance. In - this case the exception will be raised when the mock is called. + If `side_effect` is an iterable then each call to the mock will return + the next value from the iterable. If any of the members of the iterable + are exceptions they will be raised instead of returned. If `side_effect` is an iterable then each call to the mock will return the next value from the iterable. diff --git a/Lib/unittest/test/testmock/testhelpers.py b/Lib/unittest/test/testmock/testhelpers.py index 4c43f87..7a7145e 100644 --- a/Lib/unittest/test/testmock/testhelpers.py +++ b/Lib/unittest/test/testmock/testhelpers.py @@ -367,7 +367,7 @@ class SpecSignatureTest(unittest.TestCase): def test_create_autospec_unbound_methods(self): - # see issue 128 + # see mock issue 128 # this is expected to fail until the issue is fixed return class Foo(object): @@ -391,6 +391,19 @@ class SpecSignatureTest(unittest.TestCase): self.assertEqual(m.a, '3') + def test_create_autospec_keyword_only_arguments(self): + def foo(a, *, b=None): + pass + + m = create_autospec(foo) + m(1) + m.assert_called_with(1) + self.assertRaises(TypeError, m, 1, 2) + + m(2, b=3) + m.assert_called_with(2, b=3) + + def test_function_as_instance_attribute(self): obj = SomeClass() def f(a): diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index ae9822e..64fd1a1 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -868,6 +868,16 @@ class MockTest(unittest.TestCase): self.assertRaises(StopIteration, mock) + def test_side_effect_iterator_exceptions(self): + for Klass in Mock, MagicMock: + iterable = (ValueError, 3, KeyError, 6) + m = Klass(side_effect=iterable) + self.assertRaises(ValueError, m) + self.assertEqual(m(), 3) + self.assertRaises(KeyError, m) + self.assertEqual(m(), 6) + + def test_side_effect_setting_iterator(self): mock = Mock() mock.side_effect = iter([1, 2, 3]) |