diff options
Diffstat (limited to 'Lib/unittest/test/testmock/testmock.py')
-rw-r--r-- | Lib/unittest/test/testmock/testmock.py | 126 |
1 files changed, 73 insertions, 53 deletions
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 9bef51a..5f917dd 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -28,16 +28,13 @@ class Iter(object): class Something(object): - def meth(self, a, b, c, d=None): - pass + def meth(self, a, b, c, d=None): pass @classmethod - def cmeth(cls, a, b, c, d=None): - pass + def cmeth(cls, a, b, c, d=None): pass @staticmethod - def smeth(a, b, c, d=None): - pass + def smeth(a, b, c, d=None): pass class MockTest(unittest.TestCase): @@ -83,6 +80,21 @@ class MockTest(unittest.TestCase): "return value in constructor not honoured") + def test_change_return_value_via_delegate(self): + def f(): pass + mock = create_autospec(f) + mock.mock.return_value = 1 + self.assertEqual(mock(), 1) + + + def test_change_side_effect_via_delegate(self): + def f(): pass + mock = create_autospec(f) + mock.mock.side_effect = TypeError() + with self.assertRaises(TypeError): + mock() + + def test_repr(self): mock = Mock(name='foo') self.assertIn('foo', repr(mock)) @@ -161,8 +173,7 @@ class MockTest(unittest.TestCase): results = [1, 2, 3] def effect(): return results.pop() - def f(): - pass + def f(): pass mock = create_autospec(f) mock.side_effect = [1, 2, 3] @@ -177,8 +188,7 @@ class MockTest(unittest.TestCase): def test_autospec_side_effect_exception(self): # Test for issue 23661 - def f(): - pass + def f(): pass mock = create_autospec(f) mock.side_effect = ValueError('Bazinga!') @@ -340,8 +350,7 @@ class MockTest(unittest.TestCase): def test_assert_called_with_function_spec(self): - def f(a, b, c, d=None): - pass + def f(a, b, c, d=None): pass mock = Mock(spec=f) @@ -409,8 +418,7 @@ class MockTest(unittest.TestCase): def test_assert_called_once_with_function_spec(self): - def f(a, b, c, d=None): - pass + def f(a, b, c, d=None): pass mock = Mock(spec=f) @@ -514,8 +522,7 @@ class MockTest(unittest.TestCase): class Something(object): x = 3 __something__ = None - def y(self): - pass + def y(self): pass def test_attributes(mock): # should work @@ -601,8 +608,7 @@ class MockTest(unittest.TestCase): def test_customize_wrapped_object_with_side_effect_iterable(self): class Real(object): - def method(self): - raise NotImplementedError() + def method(self): pass real = Real() mock = Mock(wraps=real) @@ -615,8 +621,7 @@ class MockTest(unittest.TestCase): def test_customize_wrapped_object_with_side_effect_exception(self): class Real(object): - def method(self): - raise NotImplementedError() + def method(self): pass real = Real() mock = Mock(wraps=real) @@ -627,9 +632,7 @@ class MockTest(unittest.TestCase): def test_customize_wrapped_object_with_side_effect_function(self): class Real(object): - def method(self): - raise NotImplementedError() - + def method(self): pass def side_effect(): return sentinel.VALUE @@ -642,8 +645,7 @@ class MockTest(unittest.TestCase): def test_customize_wrapped_object_with_return_value(self): class Real(object): - def method(self): - raise NotImplementedError() + def method(self): pass real = Real() mock = Mock(wraps=real) @@ -655,8 +657,7 @@ class MockTest(unittest.TestCase): def test_customize_wrapped_object_with_return_value_and_side_effect(self): # side_effect should always take precedence over return_value. class Real(object): - def method(self): - raise NotImplementedError() + def method(self): pass real = Real() mock = Mock(wraps=real) @@ -671,8 +672,7 @@ class MockTest(unittest.TestCase): def test_customize_wrapped_object_with_return_value_and_side_effect2(self): # side_effect can return DEFAULT to default to return_value class Real(object): - def method(self): - raise NotImplementedError() + def method(self): pass real = Real() mock = Mock(wraps=real) @@ -684,8 +684,7 @@ class MockTest(unittest.TestCase): def test_customize_wrapped_object_with_return_value_and_side_effect_default(self): class Real(object): - def method(self): - raise NotImplementedError() + def method(self): pass real = Real() mock = Mock(wraps=real) @@ -764,6 +763,26 @@ class MockTest(unittest.TestCase): self.assertIsInstance(mock, X) + def test_spec_class_no_object_base(self): + class X: + pass + + mock = Mock(spec=X) + self.assertIsInstance(mock, X) + + mock = Mock(spec=X()) + self.assertIsInstance(mock, X) + + self.assertIs(mock.__class__, X) + self.assertEqual(Mock().__class__.__name__, 'Mock') + + mock = Mock(spec_set=X) + self.assertIsInstance(mock, X) + + mock = Mock(spec_set=X()) + self.assertIsInstance(mock, X) + + def test_setting_attribute_with_spec_set(self): class X(object): y = 3 @@ -902,15 +921,9 @@ class MockTest(unittest.TestCase): def assertRaisesWithMsg(self, exception, message, func, *args, **kwargs): # needed because assertRaisesRegex doesn't work easily with newlines - try: + with self.assertRaises(exception) as context: func(*args, **kwargs) - except: - instance = sys.exc_info()[1] - self.assertIsInstance(instance, exception) - else: - self.fail('Exception %r not raised' % (exception,)) - - msg = str(instance) + msg = str(context.exception) self.assertEqual(msg, message) @@ -1099,6 +1112,18 @@ class MockTest(unittest.TestCase): self.assertEqual(repr(m.mock_calls[2]), 'call.foo().bar().baz.bob()') + def test_mock_call_repr_loop(self): + m = Mock() + m.foo = m + repr(m.foo()) + self.assertRegex(repr(m.foo()), r"<Mock name='mock\(\)' id='\d+'>") + + + def test_mock_calls_contains(self): + m = Mock() + self.assertFalse([call()] in m.mock_calls) + + def test_subclassing(self): class Subclass(Mock): pass @@ -1312,8 +1337,7 @@ class MockTest(unittest.TestCase): def test_assert_has_calls_with_function_spec(self): - def f(a, b, c, d=None): - pass + def f(a, b, c, d=None): pass mock = Mock(spec=f) @@ -1371,8 +1395,7 @@ class MockTest(unittest.TestCase): def test_assert_any_call_with_function_spec(self): - def f(a, b, c, d=None): - pass + def f(a, b, c, d=None): pass mock = Mock(spec=f) @@ -1391,8 +1414,7 @@ class MockTest(unittest.TestCase): def test_mock_calls_create_autospec(self): - def f(a, b): - pass + def f(a, b): pass obj = Iter() obj.f = f @@ -1417,12 +1439,10 @@ class MockTest(unittest.TestCase): def test_create_autospec_classmethod_and_staticmethod(self): class TestClass: @classmethod - def class_method(cls): - pass + def class_method(cls): pass @staticmethod - def static_method(): - pass + def static_method(): pass for method in ('class_method', 'static_method'): with self.subTest(method=method): mock_method = mock.create_autospec(getattr(TestClass, method)) @@ -1848,8 +1868,7 @@ class MockTest(unittest.TestCase): def test_parent_propagation_with_create_autospec(self): - def foo(a, b): - pass + def foo(a, b): pass mock = Mock() mock.child = create_autospec(foo) @@ -1878,11 +1897,12 @@ class MockTest(unittest.TestCase): with patch.dict('sys.modules'): del sys.modules['unittest.mock'] - def trace(frame, event, arg): + # This trace will stop coverage being measured ;-) + def trace(frame, event, arg): # pragma: no cover return trace + self.addCleanup(sys.settrace, sys.gettrace()) sys.settrace(trace) - self.addCleanup(sys.settrace, None) from unittest.mock import ( Mock, MagicMock, NonCallableMock, NonCallableMagicMock |