michael@0: # Copyright (C) 2007-2012 Michael Foord & the mock team michael@0: # E-mail: fuzzyman AT voidspace DOT org DOT uk michael@0: # http://www.voidspace.org.uk/python/mock/ michael@0: michael@0: from tests.support import ( michael@0: callable, unittest2, inPy3k, is_instance, next michael@0: ) michael@0: michael@0: import copy michael@0: import pickle michael@0: import sys michael@0: michael@0: import mock michael@0: from mock import ( michael@0: call, DEFAULT, patch, sentinel, michael@0: MagicMock, Mock, NonCallableMock, michael@0: NonCallableMagicMock, _CallList, michael@0: create_autospec michael@0: ) michael@0: michael@0: michael@0: try: michael@0: unicode michael@0: except NameError: michael@0: unicode = str michael@0: michael@0: michael@0: class Iter(object): michael@0: def __init__(self): michael@0: self.thing = iter(['this', 'is', 'an', 'iter']) michael@0: michael@0: def __iter__(self): michael@0: return self michael@0: michael@0: def next(self): michael@0: return next(self.thing) michael@0: michael@0: __next__ = next michael@0: michael@0: michael@0: class Subclass(MagicMock): michael@0: pass michael@0: michael@0: michael@0: class Thing(object): michael@0: attribute = 6 michael@0: foo = 'bar' michael@0: michael@0: michael@0: michael@0: class MockTest(unittest2.TestCase): michael@0: michael@0: def test_all(self): michael@0: # if __all__ is badly defined then import * will raise an error michael@0: # We have to exec it because you can't import * inside a method michael@0: # in Python 3 michael@0: exec("from mock import *") michael@0: michael@0: michael@0: def test_constructor(self): michael@0: mock = Mock() michael@0: michael@0: self.assertFalse(mock.called, "called not initialised correctly") michael@0: self.assertEqual(mock.call_count, 0, michael@0: "call_count not initialised correctly") michael@0: self.assertTrue(is_instance(mock.return_value, Mock), michael@0: "return_value not initialised correctly") michael@0: michael@0: self.assertEqual(mock.call_args, None, michael@0: "call_args not initialised correctly") michael@0: self.assertEqual(mock.call_args_list, [], michael@0: "call_args_list not initialised correctly") michael@0: self.assertEqual(mock.method_calls, [], michael@0: "method_calls not initialised correctly") michael@0: michael@0: # Can't use hasattr for this test as it always returns True on a mock michael@0: self.assertFalse('_items' in mock.__dict__, michael@0: "default mock should not have '_items' attribute") michael@0: michael@0: self.assertIsNone(mock._mock_parent, michael@0: "parent not initialised correctly") michael@0: self.assertIsNone(mock._mock_methods, michael@0: "methods not initialised correctly") michael@0: self.assertEqual(mock._mock_children, {}, michael@0: "children not initialised incorrectly") michael@0: michael@0: michael@0: def test_unicode_not_broken(self): michael@0: # This used to raise an exception with Python 2.5 and Mock 0.4 michael@0: unicode(Mock()) michael@0: michael@0: michael@0: def test_return_value_in_constructor(self): michael@0: mock = Mock(return_value=None) michael@0: self.assertIsNone(mock.return_value, michael@0: "return value in constructor not honoured") michael@0: michael@0: michael@0: def test_repr(self): michael@0: mock = Mock(name='foo') michael@0: self.assertIn('foo', repr(mock)) michael@0: self.assertIn("'%s'" % id(mock), repr(mock)) michael@0: michael@0: mocks = [(Mock(), 'mock'), (Mock(name='bar'), 'bar')] michael@0: for mock, name in mocks: michael@0: self.assertIn('%s.bar' % name, repr(mock.bar)) michael@0: self.assertIn('%s.foo()' % name, repr(mock.foo())) michael@0: self.assertIn('%s.foo().bing' % name, repr(mock.foo().bing)) michael@0: self.assertIn('%s()' % name, repr(mock())) michael@0: self.assertIn('%s()()' % name, repr(mock()())) michael@0: self.assertIn('%s()().foo.bar.baz().bing' % name, michael@0: repr(mock()().foo.bar.baz().bing)) michael@0: michael@0: michael@0: def test_repr_with_spec(self): michael@0: class X(object): michael@0: pass michael@0: michael@0: mock = Mock(spec=X) michael@0: self.assertIn(" spec='X' ", repr(mock)) michael@0: michael@0: mock = Mock(spec=X()) michael@0: self.assertIn(" spec='X' ", repr(mock)) michael@0: michael@0: mock = Mock(spec_set=X) michael@0: self.assertIn(" spec_set='X' ", repr(mock)) michael@0: michael@0: mock = Mock(spec_set=X()) michael@0: self.assertIn(" spec_set='X' ", repr(mock)) michael@0: michael@0: mock = Mock(spec=X, name='foo') michael@0: self.assertIn(" spec='X' ", repr(mock)) michael@0: self.assertIn(" name='foo' ", repr(mock)) michael@0: michael@0: mock = Mock(name='foo') michael@0: self.assertNotIn("spec", repr(mock)) michael@0: michael@0: mock = Mock() michael@0: self.assertNotIn("spec", repr(mock)) michael@0: michael@0: mock = Mock(spec=['foo']) michael@0: self.assertNotIn("spec", repr(mock)) michael@0: michael@0: michael@0: def test_side_effect(self): michael@0: mock = Mock() michael@0: michael@0: def effect(*args, **kwargs): michael@0: raise SystemError('kablooie') michael@0: michael@0: mock.side_effect = effect michael@0: self.assertRaises(SystemError, mock, 1, 2, fish=3) michael@0: mock.assert_called_with(1, 2, fish=3) michael@0: michael@0: results = [1, 2, 3] michael@0: def effect(): michael@0: return results.pop() michael@0: mock.side_effect = effect michael@0: michael@0: self.assertEqual([mock(), mock(), mock()], [3, 2, 1], michael@0: "side effect not used correctly") michael@0: michael@0: mock = Mock(side_effect=sentinel.SideEffect) michael@0: self.assertEqual(mock.side_effect, sentinel.SideEffect, michael@0: "side effect in constructor not used") michael@0: michael@0: def side_effect(): michael@0: return DEFAULT michael@0: mock = Mock(side_effect=side_effect, return_value=sentinel.RETURN) michael@0: self.assertEqual(mock(), sentinel.RETURN) michael@0: michael@0: michael@0: @unittest2.skipUnless('java' in sys.platform, michael@0: 'This test only applies to Jython') michael@0: def test_java_exception_side_effect(self): michael@0: import java michael@0: mock = Mock(side_effect=java.lang.RuntimeException("Boom!")) michael@0: michael@0: # can't use assertRaises with java exceptions michael@0: try: michael@0: mock(1, 2, fish=3) michael@0: except java.lang.RuntimeException: michael@0: pass michael@0: else: michael@0: self.fail('java exception not raised') michael@0: mock.assert_called_with(1,2, fish=3) michael@0: michael@0: michael@0: def test_reset_mock(self): michael@0: parent = Mock() michael@0: spec = ["something"] michael@0: mock = Mock(name="child", parent=parent, spec=spec) michael@0: mock(sentinel.Something, something=sentinel.SomethingElse) michael@0: something = mock.something michael@0: mock.something() michael@0: mock.side_effect = sentinel.SideEffect michael@0: return_value = mock.return_value michael@0: return_value() michael@0: michael@0: mock.reset_mock() michael@0: michael@0: self.assertEqual(mock._mock_name, "child", michael@0: "name incorrectly reset") michael@0: self.assertEqual(mock._mock_parent, parent, michael@0: "parent incorrectly reset") michael@0: self.assertEqual(mock._mock_methods, spec, michael@0: "methods incorrectly reset") michael@0: michael@0: self.assertFalse(mock.called, "called not reset") michael@0: self.assertEqual(mock.call_count, 0, "call_count not reset") michael@0: self.assertEqual(mock.call_args, None, "call_args not reset") michael@0: self.assertEqual(mock.call_args_list, [], "call_args_list not reset") michael@0: self.assertEqual(mock.method_calls, [], michael@0: "method_calls not initialised correctly: %r != %r" % michael@0: (mock.method_calls, [])) michael@0: self.assertEqual(mock.mock_calls, []) michael@0: michael@0: self.assertEqual(mock.side_effect, sentinel.SideEffect, michael@0: "side_effect incorrectly reset") michael@0: self.assertEqual(mock.return_value, return_value, michael@0: "return_value incorrectly reset") michael@0: self.assertFalse(return_value.called, "return value mock not reset") michael@0: self.assertEqual(mock._mock_children, {'something': something}, michael@0: "children reset incorrectly") michael@0: self.assertEqual(mock.something, something, michael@0: "children incorrectly cleared") michael@0: self.assertFalse(mock.something.called, "child not reset") michael@0: michael@0: michael@0: def test_reset_mock_recursion(self): michael@0: mock = Mock() michael@0: mock.return_value = mock michael@0: michael@0: # used to cause recursion michael@0: mock.reset_mock() michael@0: michael@0: michael@0: def test_call(self): michael@0: mock = Mock() michael@0: self.assertTrue(is_instance(mock.return_value, Mock), michael@0: "Default return_value should be a Mock") michael@0: michael@0: result = mock() michael@0: self.assertEqual(mock(), result, michael@0: "different result from consecutive calls") michael@0: mock.reset_mock() michael@0: michael@0: ret_val = mock(sentinel.Arg) michael@0: self.assertTrue(mock.called, "called not set") michael@0: self.assertEqual(mock.call_count, 1, "call_count incoreect") michael@0: self.assertEqual(mock.call_args, ((sentinel.Arg,), {}), michael@0: "call_args not set") michael@0: self.assertEqual(mock.call_args_list, [((sentinel.Arg,), {})], michael@0: "call_args_list not initialised correctly") michael@0: michael@0: mock.return_value = sentinel.ReturnValue michael@0: ret_val = mock(sentinel.Arg, key=sentinel.KeyArg) michael@0: self.assertEqual(ret_val, sentinel.ReturnValue, michael@0: "incorrect return value") michael@0: michael@0: self.assertEqual(mock.call_count, 2, "call_count incorrect") michael@0: self.assertEqual(mock.call_args, michael@0: ((sentinel.Arg,), {'key': sentinel.KeyArg}), michael@0: "call_args not set") michael@0: self.assertEqual(mock.call_args_list, [ michael@0: ((sentinel.Arg,), {}), michael@0: ((sentinel.Arg,), {'key': sentinel.KeyArg}) michael@0: ], michael@0: "call_args_list not set") michael@0: michael@0: michael@0: def test_call_args_comparison(self): michael@0: mock = Mock() michael@0: mock() michael@0: mock(sentinel.Arg) michael@0: mock(kw=sentinel.Kwarg) michael@0: mock(sentinel.Arg, kw=sentinel.Kwarg) michael@0: self.assertEqual(mock.call_args_list, [ michael@0: (), michael@0: ((sentinel.Arg,),), michael@0: ({"kw": sentinel.Kwarg},), michael@0: ((sentinel.Arg,), {"kw": sentinel.Kwarg}) michael@0: ]) michael@0: self.assertEqual(mock.call_args, michael@0: ((sentinel.Arg,), {"kw": sentinel.Kwarg})) michael@0: michael@0: michael@0: def test_assert_called_with(self): michael@0: mock = Mock() michael@0: mock() michael@0: michael@0: # Will raise an exception if it fails michael@0: mock.assert_called_with() michael@0: self.assertRaises(AssertionError, mock.assert_called_with, 1) michael@0: michael@0: mock.reset_mock() michael@0: self.assertRaises(AssertionError, mock.assert_called_with) michael@0: michael@0: mock(1, 2, 3, a='fish', b='nothing') michael@0: mock.assert_called_with(1, 2, 3, a='fish', b='nothing') michael@0: michael@0: michael@0: def test_assert_called_once_with(self): michael@0: mock = Mock() michael@0: mock() michael@0: michael@0: # Will raise an exception if it fails michael@0: mock.assert_called_once_with() michael@0: michael@0: mock() michael@0: self.assertRaises(AssertionError, mock.assert_called_once_with) michael@0: michael@0: mock.reset_mock() michael@0: self.assertRaises(AssertionError, mock.assert_called_once_with) michael@0: michael@0: mock('foo', 'bar', baz=2) michael@0: mock.assert_called_once_with('foo', 'bar', baz=2) michael@0: michael@0: mock.reset_mock() michael@0: mock('foo', 'bar', baz=2) michael@0: self.assertRaises( michael@0: AssertionError, michael@0: lambda: mock.assert_called_once_with('bob', 'bar', baz=2) michael@0: ) michael@0: michael@0: michael@0: def test_attribute_access_returns_mocks(self): michael@0: mock = Mock() michael@0: something = mock.something michael@0: self.assertTrue(is_instance(something, Mock), "attribute isn't a mock") michael@0: self.assertEqual(mock.something, something, michael@0: "different attributes returned for same name") michael@0: michael@0: # Usage example michael@0: mock = Mock() michael@0: mock.something.return_value = 3 michael@0: michael@0: self.assertEqual(mock.something(), 3, "method returned wrong value") michael@0: self.assertTrue(mock.something.called, michael@0: "method didn't record being called") michael@0: michael@0: michael@0: def test_attributes_have_name_and_parent_set(self): michael@0: mock = Mock() michael@0: something = mock.something michael@0: michael@0: self.assertEqual(something._mock_name, "something", michael@0: "attribute name not set correctly") michael@0: self.assertEqual(something._mock_parent, mock, michael@0: "attribute parent not set correctly") michael@0: michael@0: michael@0: def test_method_calls_recorded(self): michael@0: mock = Mock() michael@0: mock.something(3, fish=None) michael@0: mock.something_else.something(6, cake=sentinel.Cake) michael@0: michael@0: self.assertEqual(mock.something_else.method_calls, michael@0: [("something", (6,), {'cake': sentinel.Cake})], michael@0: "method calls not recorded correctly") michael@0: self.assertEqual(mock.method_calls, [ michael@0: ("something", (3,), {'fish': None}), michael@0: ("something_else.something", (6,), {'cake': sentinel.Cake}) michael@0: ], michael@0: "method calls not recorded correctly") michael@0: michael@0: michael@0: def test_method_calls_compare_easily(self): michael@0: mock = Mock() michael@0: mock.something() michael@0: self.assertEqual(mock.method_calls, [('something',)]) michael@0: self.assertEqual(mock.method_calls, [('something', (), {})]) michael@0: michael@0: mock = Mock() michael@0: mock.something('different') michael@0: self.assertEqual(mock.method_calls, [('something', ('different',))]) michael@0: self.assertEqual(mock.method_calls, michael@0: [('something', ('different',), {})]) michael@0: michael@0: mock = Mock() michael@0: mock.something(x=1) michael@0: self.assertEqual(mock.method_calls, [('something', {'x': 1})]) michael@0: self.assertEqual(mock.method_calls, [('something', (), {'x': 1})]) michael@0: michael@0: mock = Mock() michael@0: mock.something('different', some='more') michael@0: self.assertEqual(mock.method_calls, [ michael@0: ('something', ('different',), {'some': 'more'}) michael@0: ]) michael@0: michael@0: michael@0: def test_only_allowed_methods_exist(self): michael@0: for spec in ['something'], ('something',): michael@0: for arg in 'spec', 'spec_set': michael@0: mock = Mock(**{arg: spec}) michael@0: michael@0: # this should be allowed michael@0: mock.something michael@0: self.assertRaisesRegexp( michael@0: AttributeError, michael@0: "Mock object has no attribute 'something_else'", michael@0: getattr, mock, 'something_else' michael@0: ) michael@0: michael@0: michael@0: def test_from_spec(self): michael@0: class Something(object): michael@0: x = 3 michael@0: __something__ = None michael@0: def y(self): michael@0: pass michael@0: michael@0: def test_attributes(mock): michael@0: # should work michael@0: mock.x michael@0: mock.y michael@0: mock.__something__ michael@0: self.assertRaisesRegexp( michael@0: AttributeError, michael@0: "Mock object has no attribute 'z'", michael@0: getattr, mock, 'z' michael@0: ) michael@0: self.assertRaisesRegexp( michael@0: AttributeError, michael@0: "Mock object has no attribute '__foobar__'", michael@0: getattr, mock, '__foobar__' michael@0: ) michael@0: michael@0: test_attributes(Mock(spec=Something)) michael@0: test_attributes(Mock(spec=Something())) michael@0: michael@0: michael@0: def test_wraps_calls(self): michael@0: real = Mock() michael@0: michael@0: mock = Mock(wraps=real) michael@0: self.assertEqual(mock(), real()) michael@0: michael@0: real.reset_mock() michael@0: michael@0: mock(1, 2, fish=3) michael@0: real.assert_called_with(1, 2, fish=3) michael@0: michael@0: michael@0: def test_wraps_call_with_nondefault_return_value(self): michael@0: real = Mock() michael@0: michael@0: mock = Mock(wraps=real) michael@0: mock.return_value = 3 michael@0: michael@0: self.assertEqual(mock(), 3) michael@0: self.assertFalse(real.called) michael@0: michael@0: michael@0: def test_wraps_attributes(self): michael@0: class Real(object): michael@0: attribute = Mock() michael@0: michael@0: real = Real() michael@0: michael@0: mock = Mock(wraps=real) michael@0: self.assertEqual(mock.attribute(), real.attribute()) michael@0: self.assertRaises(AttributeError, lambda: mock.fish) michael@0: michael@0: self.assertNotEqual(mock.attribute, real.attribute) michael@0: result = mock.attribute.frog(1, 2, fish=3) michael@0: Real.attribute.frog.assert_called_with(1, 2, fish=3) michael@0: self.assertEqual(result, Real.attribute.frog()) michael@0: michael@0: michael@0: def test_exceptional_side_effect(self): michael@0: mock = Mock(side_effect=AttributeError) michael@0: self.assertRaises(AttributeError, mock) michael@0: michael@0: mock = Mock(side_effect=AttributeError('foo')) michael@0: self.assertRaises(AttributeError, mock) michael@0: michael@0: michael@0: def test_baseexceptional_side_effect(self): michael@0: mock = Mock(side_effect=KeyboardInterrupt) michael@0: self.assertRaises(KeyboardInterrupt, mock) michael@0: michael@0: mock = Mock(side_effect=KeyboardInterrupt('foo')) michael@0: self.assertRaises(KeyboardInterrupt, mock) michael@0: michael@0: michael@0: def test_assert_called_with_message(self): michael@0: mock = Mock() michael@0: self.assertRaisesRegexp(AssertionError, 'Not called', michael@0: mock.assert_called_with) michael@0: michael@0: michael@0: def test__name__(self): michael@0: mock = Mock() michael@0: self.assertRaises(AttributeError, lambda: mock.__name__) michael@0: michael@0: mock.__name__ = 'foo' michael@0: self.assertEqual(mock.__name__, 'foo') michael@0: michael@0: michael@0: def test_spec_list_subclass(self): michael@0: class Sub(list): michael@0: pass michael@0: mock = Mock(spec=Sub(['foo'])) michael@0: michael@0: mock.append(3) michael@0: mock.append.assert_called_with(3) michael@0: self.assertRaises(AttributeError, getattr, mock, 'foo') michael@0: michael@0: michael@0: def test_spec_class(self): michael@0: class X(object): michael@0: pass michael@0: michael@0: mock = Mock(spec=X) michael@0: self.assertTrue(isinstance(mock, X)) michael@0: michael@0: mock = Mock(spec=X()) michael@0: self.assertTrue(isinstance(mock, X)) michael@0: michael@0: self.assertIs(mock.__class__, X) michael@0: self.assertEqual(Mock().__class__.__name__, 'Mock') michael@0: michael@0: mock = Mock(spec_set=X) michael@0: self.assertTrue(isinstance(mock, X)) michael@0: michael@0: mock = Mock(spec_set=X()) michael@0: self.assertTrue(isinstance(mock, X)) michael@0: michael@0: michael@0: def test_setting_attribute_with_spec_set(self): michael@0: class X(object): michael@0: y = 3 michael@0: michael@0: mock = Mock(spec=X) michael@0: mock.x = 'foo' michael@0: michael@0: mock = Mock(spec_set=X) michael@0: def set_attr(): michael@0: mock.x = 'foo' michael@0: michael@0: mock.y = 'foo' michael@0: self.assertRaises(AttributeError, set_attr) michael@0: michael@0: michael@0: def test_copy(self): michael@0: current = sys.getrecursionlimit() michael@0: self.addCleanup(sys.setrecursionlimit, current) michael@0: michael@0: # can't use sys.maxint as this doesn't exist in Python 3 michael@0: sys.setrecursionlimit(int(10e8)) michael@0: # this segfaults without the fix in place michael@0: copy.copy(Mock()) michael@0: michael@0: michael@0: @unittest2.skipIf(inPy3k, "no old style classes in Python 3") michael@0: def test_spec_old_style_classes(self): michael@0: class Foo: michael@0: bar = 7 michael@0: michael@0: mock = Mock(spec=Foo) michael@0: mock.bar = 6 michael@0: self.assertRaises(AttributeError, lambda: mock.foo) michael@0: michael@0: mock = Mock(spec=Foo()) michael@0: mock.bar = 6 michael@0: self.assertRaises(AttributeError, lambda: mock.foo) michael@0: michael@0: michael@0: @unittest2.skipIf(inPy3k, "no old style classes in Python 3") michael@0: def test_spec_set_old_style_classes(self): michael@0: class Foo: michael@0: bar = 7 michael@0: michael@0: mock = Mock(spec_set=Foo) michael@0: mock.bar = 6 michael@0: self.assertRaises(AttributeError, lambda: mock.foo) michael@0: michael@0: def _set(): michael@0: mock.foo = 3 michael@0: self.assertRaises(AttributeError, _set) michael@0: michael@0: mock = Mock(spec_set=Foo()) michael@0: mock.bar = 6 michael@0: self.assertRaises(AttributeError, lambda: mock.foo) michael@0: michael@0: def _set(): michael@0: mock.foo = 3 michael@0: self.assertRaises(AttributeError, _set) michael@0: michael@0: michael@0: def test_subclass_with_properties(self): michael@0: class SubClass(Mock): michael@0: def _get(self): michael@0: return 3 michael@0: def _set(self, value): michael@0: raise NameError('strange error') michael@0: some_attribute = property(_get, _set) michael@0: michael@0: s = SubClass(spec_set=SubClass) michael@0: self.assertEqual(s.some_attribute, 3) michael@0: michael@0: def test(): michael@0: s.some_attribute = 3 michael@0: self.assertRaises(NameError, test) michael@0: michael@0: def test(): michael@0: s.foo = 'bar' michael@0: self.assertRaises(AttributeError, test) michael@0: michael@0: michael@0: def test_setting_call(self): michael@0: mock = Mock() michael@0: def __call__(self, a): michael@0: return self._mock_call(a) michael@0: michael@0: type(mock).__call__ = __call__ michael@0: mock('one') michael@0: mock.assert_called_with('one') michael@0: michael@0: self.assertRaises(TypeError, mock, 'one', 'two') michael@0: michael@0: michael@0: @unittest2.skipUnless(sys.version_info[:2] >= (2, 6), michael@0: "__dir__ not available until Python 2.6 or later") michael@0: def test_dir(self): michael@0: mock = Mock() michael@0: attrs = set(dir(mock)) michael@0: type_attrs = set([m for m in dir(Mock) if not m.startswith('_')]) michael@0: michael@0: # all public attributes from the type are included michael@0: self.assertEqual(set(), type_attrs - attrs) michael@0: michael@0: # creates these attributes michael@0: mock.a, mock.b michael@0: self.assertIn('a', dir(mock)) michael@0: self.assertIn('b', dir(mock)) michael@0: michael@0: # instance attributes michael@0: mock.c = mock.d = None michael@0: self.assertIn('c', dir(mock)) michael@0: self.assertIn('d', dir(mock)) michael@0: michael@0: # magic methods michael@0: mock.__iter__ = lambda s: iter([]) michael@0: self.assertIn('__iter__', dir(mock)) michael@0: michael@0: michael@0: @unittest2.skipUnless(sys.version_info[:2] >= (2, 6), michael@0: "__dir__ not available until Python 2.6 or later") michael@0: def test_dir_from_spec(self): michael@0: mock = Mock(spec=unittest2.TestCase) michael@0: testcase_attrs = set(dir(unittest2.TestCase)) michael@0: attrs = set(dir(mock)) michael@0: michael@0: # all attributes from the spec are included michael@0: self.assertEqual(set(), testcase_attrs - attrs) michael@0: michael@0: # shadow a sys attribute michael@0: mock.version = 3 michael@0: self.assertEqual(dir(mock).count('version'), 1) michael@0: michael@0: michael@0: @unittest2.skipUnless(sys.version_info[:2] >= (2, 6), michael@0: "__dir__ not available until Python 2.6 or later") michael@0: def test_filter_dir(self): michael@0: patcher = patch.object(mock, 'FILTER_DIR', False) michael@0: patcher.start() michael@0: try: michael@0: attrs = set(dir(Mock())) michael@0: type_attrs = set(dir(Mock)) michael@0: michael@0: # ALL attributes from the type are included michael@0: self.assertEqual(set(), type_attrs - attrs) michael@0: finally: michael@0: patcher.stop() michael@0: michael@0: michael@0: def test_configure_mock(self): michael@0: mock = Mock(foo='bar') michael@0: self.assertEqual(mock.foo, 'bar') michael@0: michael@0: mock = MagicMock(foo='bar') michael@0: self.assertEqual(mock.foo, 'bar') michael@0: michael@0: kwargs = {'side_effect': KeyError, 'foo.bar.return_value': 33, michael@0: 'foo': MagicMock()} michael@0: mock = Mock(**kwargs) michael@0: self.assertRaises(KeyError, mock) michael@0: self.assertEqual(mock.foo.bar(), 33) michael@0: self.assertIsInstance(mock.foo, MagicMock) michael@0: michael@0: mock = Mock() michael@0: mock.configure_mock(**kwargs) michael@0: self.assertRaises(KeyError, mock) michael@0: self.assertEqual(mock.foo.bar(), 33) michael@0: self.assertIsInstance(mock.foo, MagicMock) michael@0: michael@0: michael@0: def assertRaisesWithMsg(self, exception, message, func, *args, **kwargs): michael@0: # needed because assertRaisesRegex doesn't work easily with newlines michael@0: try: michael@0: func(*args, **kwargs) michael@0: except: michael@0: instance = sys.exc_info()[1] michael@0: self.assertIsInstance(instance, exception) michael@0: else: michael@0: self.fail('Exception %r not raised' % (exception,)) michael@0: michael@0: msg = str(instance) michael@0: self.assertEqual(msg, message) michael@0: michael@0: michael@0: def test_assert_called_with_failure_message(self): michael@0: mock = NonCallableMock() michael@0: michael@0: expected = "mock(1, '2', 3, bar='foo')" michael@0: message = 'Expected call: %s\nNot called' michael@0: self.assertRaisesWithMsg( michael@0: AssertionError, message % (expected,), michael@0: mock.assert_called_with, 1, '2', 3, bar='foo' michael@0: ) michael@0: michael@0: mock.foo(1, '2', 3, foo='foo') michael@0: michael@0: michael@0: asserters = [ michael@0: mock.foo.assert_called_with, mock.foo.assert_called_once_with michael@0: ] michael@0: for meth in asserters: michael@0: actual = "foo(1, '2', 3, foo='foo')" michael@0: expected = "foo(1, '2', 3, bar='foo')" michael@0: message = 'Expected call: %s\nActual call: %s' michael@0: self.assertRaisesWithMsg( michael@0: AssertionError, message % (expected, actual), michael@0: meth, 1, '2', 3, bar='foo' michael@0: ) michael@0: michael@0: # just kwargs michael@0: for meth in asserters: michael@0: actual = "foo(1, '2', 3, foo='foo')" michael@0: expected = "foo(bar='foo')" michael@0: message = 'Expected call: %s\nActual call: %s' michael@0: self.assertRaisesWithMsg( michael@0: AssertionError, message % (expected, actual), michael@0: meth, bar='foo' michael@0: ) michael@0: michael@0: # just args michael@0: for meth in asserters: michael@0: actual = "foo(1, '2', 3, foo='foo')" michael@0: expected = "foo(1, 2, 3)" michael@0: message = 'Expected call: %s\nActual call: %s' michael@0: self.assertRaisesWithMsg( michael@0: AssertionError, message % (expected, actual), michael@0: meth, 1, 2, 3 michael@0: ) michael@0: michael@0: # empty michael@0: for meth in asserters: michael@0: actual = "foo(1, '2', 3, foo='foo')" michael@0: expected = "foo()" michael@0: message = 'Expected call: %s\nActual call: %s' michael@0: self.assertRaisesWithMsg( michael@0: AssertionError, message % (expected, actual), meth michael@0: ) michael@0: michael@0: michael@0: def test_mock_calls(self): michael@0: mock = MagicMock() michael@0: michael@0: # need to do this because MagicMock.mock_calls used to just return michael@0: # a MagicMock which also returned a MagicMock when __eq__ was called michael@0: self.assertIs(mock.mock_calls == [], True) michael@0: michael@0: mock = MagicMock() michael@0: mock() michael@0: expected = [('', (), {})] michael@0: self.assertEqual(mock.mock_calls, expected) michael@0: michael@0: mock.foo() michael@0: expected.append(call.foo()) michael@0: self.assertEqual(mock.mock_calls, expected) michael@0: # intermediate mock_calls work too michael@0: self.assertEqual(mock.foo.mock_calls, [('', (), {})]) michael@0: michael@0: mock = MagicMock() michael@0: mock().foo(1, 2, 3, a=4, b=5) michael@0: expected = [ michael@0: ('', (), {}), ('().foo', (1, 2, 3), dict(a=4, b=5)) michael@0: ] michael@0: self.assertEqual(mock.mock_calls, expected) michael@0: self.assertEqual(mock.return_value.foo.mock_calls, michael@0: [('', (1, 2, 3), dict(a=4, b=5))]) michael@0: self.assertEqual(mock.return_value.mock_calls, michael@0: [('foo', (1, 2, 3), dict(a=4, b=5))]) michael@0: michael@0: mock = MagicMock() michael@0: mock().foo.bar().baz() michael@0: expected = [ michael@0: ('', (), {}), ('().foo.bar', (), {}), michael@0: ('().foo.bar().baz', (), {}) michael@0: ] michael@0: self.assertEqual(mock.mock_calls, expected) michael@0: self.assertEqual(mock().mock_calls, michael@0: call.foo.bar().baz().call_list()) michael@0: michael@0: for kwargs in dict(), dict(name='bar'): michael@0: mock = MagicMock(**kwargs) michael@0: int(mock.foo) michael@0: expected = [('foo.__int__', (), {})] michael@0: self.assertEqual(mock.mock_calls, expected) michael@0: michael@0: mock = MagicMock(**kwargs) michael@0: mock.a()() michael@0: expected = [('a', (), {}), ('a()', (), {})] michael@0: self.assertEqual(mock.mock_calls, expected) michael@0: self.assertEqual(mock.a().mock_calls, [call()]) michael@0: michael@0: mock = MagicMock(**kwargs) michael@0: mock(1)(2)(3) michael@0: self.assertEqual(mock.mock_calls, call(1)(2)(3).call_list()) michael@0: self.assertEqual(mock().mock_calls, call(2)(3).call_list()) michael@0: self.assertEqual(mock()().mock_calls, call(3).call_list()) michael@0: michael@0: mock = MagicMock(**kwargs) michael@0: mock(1)(2)(3).a.b.c(4) michael@0: self.assertEqual(mock.mock_calls, michael@0: call(1)(2)(3).a.b.c(4).call_list()) michael@0: self.assertEqual(mock().mock_calls, michael@0: call(2)(3).a.b.c(4).call_list()) michael@0: self.assertEqual(mock()().mock_calls, michael@0: call(3).a.b.c(4).call_list()) michael@0: michael@0: mock = MagicMock(**kwargs) michael@0: int(mock().foo.bar().baz()) michael@0: last_call = ('().foo.bar().baz().__int__', (), {}) michael@0: self.assertEqual(mock.mock_calls[-1], last_call) michael@0: self.assertEqual(mock().mock_calls, michael@0: call.foo.bar().baz().__int__().call_list()) michael@0: self.assertEqual(mock().foo.bar().mock_calls, michael@0: call.baz().__int__().call_list()) michael@0: self.assertEqual(mock().foo.bar().baz.mock_calls, michael@0: call().__int__().call_list()) michael@0: michael@0: michael@0: def test_subclassing(self): michael@0: class Subclass(Mock): michael@0: pass michael@0: michael@0: mock = Subclass() michael@0: self.assertIsInstance(mock.foo, Subclass) michael@0: self.assertIsInstance(mock(), Subclass) michael@0: michael@0: class Subclass(Mock): michael@0: def _get_child_mock(self, **kwargs): michael@0: return Mock(**kwargs) michael@0: michael@0: mock = Subclass() michael@0: self.assertNotIsInstance(mock.foo, Subclass) michael@0: self.assertNotIsInstance(mock(), Subclass) michael@0: michael@0: michael@0: def test_arg_lists(self): michael@0: mocks = [ michael@0: Mock(), michael@0: MagicMock(), michael@0: NonCallableMock(), michael@0: NonCallableMagicMock() michael@0: ] michael@0: michael@0: def assert_attrs(mock): michael@0: names = 'call_args_list', 'method_calls', 'mock_calls' michael@0: for name in names: michael@0: attr = getattr(mock, name) michael@0: self.assertIsInstance(attr, _CallList) michael@0: self.assertIsInstance(attr, list) michael@0: self.assertEqual(attr, []) michael@0: michael@0: for mock in mocks: michael@0: assert_attrs(mock) michael@0: michael@0: if callable(mock): michael@0: mock() michael@0: mock(1, 2) michael@0: mock(a=3) michael@0: michael@0: mock.reset_mock() michael@0: assert_attrs(mock) michael@0: michael@0: mock.foo() michael@0: mock.foo.bar(1, a=3) michael@0: mock.foo(1).bar().baz(3) michael@0: michael@0: mock.reset_mock() michael@0: assert_attrs(mock) michael@0: michael@0: michael@0: def test_call_args_two_tuple(self): michael@0: mock = Mock() michael@0: mock(1, a=3) michael@0: mock(2, b=4) michael@0: michael@0: self.assertEqual(len(mock.call_args), 2) michael@0: args, kwargs = mock.call_args michael@0: self.assertEqual(args, (2,)) michael@0: self.assertEqual(kwargs, dict(b=4)) michael@0: michael@0: expected_list = [((1,), dict(a=3)), ((2,), dict(b=4))] michael@0: for expected, call_args in zip(expected_list, mock.call_args_list): michael@0: self.assertEqual(len(call_args), 2) michael@0: self.assertEqual(expected[0], call_args[0]) michael@0: self.assertEqual(expected[1], call_args[1]) michael@0: michael@0: michael@0: def test_side_effect_iterator(self): michael@0: mock = Mock(side_effect=iter([1, 2, 3])) michael@0: self.assertEqual([mock(), mock(), mock()], [1, 2, 3]) michael@0: self.assertRaises(StopIteration, mock) michael@0: michael@0: mock = MagicMock(side_effect=['a', 'b', 'c']) michael@0: self.assertEqual([mock(), mock(), mock()], ['a', 'b', 'c']) michael@0: self.assertRaises(StopIteration, mock) michael@0: michael@0: mock = Mock(side_effect='ghi') michael@0: self.assertEqual([mock(), mock(), mock()], ['g', 'h', 'i']) michael@0: self.assertRaises(StopIteration, mock) michael@0: michael@0: class Foo(object): michael@0: pass michael@0: mock = MagicMock(side_effect=Foo) michael@0: self.assertIsInstance(mock(), Foo) michael@0: michael@0: mock = Mock(side_effect=Iter()) michael@0: self.assertEqual([mock(), mock(), mock(), mock()], michael@0: ['this', 'is', 'an', 'iter']) michael@0: self.assertRaises(StopIteration, mock) michael@0: michael@0: michael@0: def test_side_effect_setting_iterator(self): michael@0: mock = Mock() michael@0: mock.side_effect = iter([1, 2, 3]) michael@0: self.assertEqual([mock(), mock(), mock()], [1, 2, 3]) michael@0: self.assertRaises(StopIteration, mock) michael@0: side_effect = mock.side_effect michael@0: self.assertIsInstance(side_effect, type(iter([]))) michael@0: michael@0: mock.side_effect = ['a', 'b', 'c'] michael@0: self.assertEqual([mock(), mock(), mock()], ['a', 'b', 'c']) michael@0: self.assertRaises(StopIteration, mock) michael@0: side_effect = mock.side_effect michael@0: self.assertIsInstance(side_effect, type(iter([]))) michael@0: michael@0: this_iter = Iter() michael@0: mock.side_effect = this_iter michael@0: self.assertEqual([mock(), mock(), mock(), mock()], michael@0: ['this', 'is', 'an', 'iter']) michael@0: self.assertRaises(StopIteration, mock) michael@0: self.assertIs(mock.side_effect, this_iter) michael@0: michael@0: michael@0: def test_side_effect_iterator_exceptions(self): michael@0: for Klass in Mock, MagicMock: michael@0: iterable = (ValueError, 3, KeyError, 6) michael@0: m = Klass(side_effect=iterable) michael@0: self.assertRaises(ValueError, m) michael@0: self.assertEqual(m(), 3) michael@0: self.assertRaises(KeyError, m) michael@0: self.assertEqual(m(), 6) michael@0: michael@0: michael@0: def test_assert_has_calls_any_order(self): michael@0: mock = Mock() michael@0: mock(1, 2) michael@0: mock(a=3) michael@0: mock(3, 4) michael@0: mock(b=6) michael@0: mock(b=6) michael@0: michael@0: kalls = [ michael@0: call(1, 2), ({'a': 3},), michael@0: ((3, 4),), ((), {'a': 3}), michael@0: ('', (1, 2)), ('', {'a': 3}), michael@0: ('', (1, 2), {}), ('', (), {'a': 3}) michael@0: ] michael@0: for kall in kalls: michael@0: mock.assert_has_calls([kall], any_order=True) michael@0: michael@0: for kall in call(1, '2'), call(b=3), call(), 3, None, 'foo': michael@0: self.assertRaises( michael@0: AssertionError, mock.assert_has_calls, michael@0: [kall], any_order=True michael@0: ) michael@0: michael@0: kall_lists = [ michael@0: [call(1, 2), call(b=6)], michael@0: [call(3, 4), call(1, 2)], michael@0: [call(b=6), call(b=6)], michael@0: ] michael@0: michael@0: for kall_list in kall_lists: michael@0: mock.assert_has_calls(kall_list, any_order=True) michael@0: michael@0: kall_lists = [ michael@0: [call(b=6), call(b=6), call(b=6)], michael@0: [call(1, 2), call(1, 2)], michael@0: [call(3, 4), call(1, 2), call(5, 7)], michael@0: [call(b=6), call(3, 4), call(b=6), call(1, 2), call(b=6)], michael@0: ] michael@0: for kall_list in kall_lists: michael@0: self.assertRaises( michael@0: AssertionError, mock.assert_has_calls, michael@0: kall_list, any_order=True michael@0: ) michael@0: michael@0: def test_assert_has_calls(self): michael@0: kalls1 = [ michael@0: call(1, 2), ({'a': 3},), michael@0: ((3, 4),), call(b=6), michael@0: ('', (1,), {'b': 6}), michael@0: ] michael@0: kalls2 = [call.foo(), call.bar(1)] michael@0: kalls2.extend(call.spam().baz(a=3).call_list()) michael@0: kalls2.extend(call.bam(set(), foo={}).fish([1]).call_list()) michael@0: michael@0: mocks = [] michael@0: for mock in Mock(), MagicMock(): michael@0: mock(1, 2) michael@0: mock(a=3) michael@0: mock(3, 4) michael@0: mock(b=6) michael@0: mock(1, b=6) michael@0: mocks.append((mock, kalls1)) michael@0: michael@0: mock = Mock() michael@0: mock.foo() michael@0: mock.bar(1) michael@0: mock.spam().baz(a=3) michael@0: mock.bam(set(), foo={}).fish([1]) michael@0: mocks.append((mock, kalls2)) michael@0: michael@0: for mock, kalls in mocks: michael@0: for i in range(len(kalls)): michael@0: for step in 1, 2, 3: michael@0: these = kalls[i:i+step] michael@0: mock.assert_has_calls(these) michael@0: michael@0: if len(these) > 1: michael@0: self.assertRaises( michael@0: AssertionError, michael@0: mock.assert_has_calls, michael@0: list(reversed(these)) michael@0: ) michael@0: michael@0: michael@0: def test_assert_any_call(self): michael@0: mock = Mock() michael@0: mock(1, 2) michael@0: mock(a=3) michael@0: mock(1, b=6) michael@0: michael@0: mock.assert_any_call(1, 2) michael@0: mock.assert_any_call(a=3) michael@0: mock.assert_any_call(1, b=6) michael@0: michael@0: self.assertRaises( michael@0: AssertionError, michael@0: mock.assert_any_call michael@0: ) michael@0: self.assertRaises( michael@0: AssertionError, michael@0: mock.assert_any_call, michael@0: 1, 3 michael@0: ) michael@0: self.assertRaises( michael@0: AssertionError, michael@0: mock.assert_any_call, michael@0: a=4 michael@0: ) michael@0: michael@0: michael@0: def test_mock_calls_create_autospec(self): michael@0: def f(a, b): michael@0: pass michael@0: obj = Iter() michael@0: obj.f = f michael@0: michael@0: funcs = [ michael@0: create_autospec(f), michael@0: create_autospec(obj).f michael@0: ] michael@0: for func in funcs: michael@0: func(1, 2) michael@0: func(3, 4) michael@0: michael@0: self.assertEqual( michael@0: func.mock_calls, [call(1, 2), call(3, 4)] michael@0: ) michael@0: michael@0: michael@0: def test_mock_add_spec(self): michael@0: class _One(object): michael@0: one = 1 michael@0: class _Two(object): michael@0: two = 2 michael@0: class Anything(object): michael@0: one = two = three = 'four' michael@0: michael@0: klasses = [ michael@0: Mock, MagicMock, NonCallableMock, NonCallableMagicMock michael@0: ] michael@0: for Klass in list(klasses): michael@0: klasses.append(lambda K=Klass: K(spec=Anything)) michael@0: klasses.append(lambda K=Klass: K(spec_set=Anything)) michael@0: michael@0: for Klass in klasses: michael@0: for kwargs in dict(), dict(spec_set=True): michael@0: mock = Klass() michael@0: #no error michael@0: mock.one, mock.two, mock.three michael@0: michael@0: for One, Two in [(_One, _Two), (['one'], ['two'])]: michael@0: for kwargs in dict(), dict(spec_set=True): michael@0: mock.mock_add_spec(One, **kwargs) michael@0: michael@0: mock.one michael@0: self.assertRaises( michael@0: AttributeError, getattr, mock, 'two' michael@0: ) michael@0: self.assertRaises( michael@0: AttributeError, getattr, mock, 'three' michael@0: ) michael@0: if 'spec_set' in kwargs: michael@0: self.assertRaises( michael@0: AttributeError, setattr, mock, 'three', None michael@0: ) michael@0: michael@0: mock.mock_add_spec(Two, **kwargs) michael@0: self.assertRaises( michael@0: AttributeError, getattr, mock, 'one' michael@0: ) michael@0: mock.two michael@0: self.assertRaises( michael@0: AttributeError, getattr, mock, 'three' michael@0: ) michael@0: if 'spec_set' in kwargs: michael@0: self.assertRaises( michael@0: AttributeError, setattr, mock, 'three', None michael@0: ) michael@0: # note that creating a mock, setting an instance attribute, and michael@0: # *then* setting a spec doesn't work. Not the intended use case michael@0: michael@0: michael@0: def test_mock_add_spec_magic_methods(self): michael@0: for Klass in MagicMock, NonCallableMagicMock: michael@0: mock = Klass() michael@0: int(mock) michael@0: michael@0: mock.mock_add_spec(object) michael@0: self.assertRaises(TypeError, int, mock) michael@0: michael@0: mock = Klass() michael@0: mock['foo'] michael@0: mock.__int__.return_value =4 michael@0: michael@0: mock.mock_add_spec(int) michael@0: self.assertEqual(int(mock), 4) michael@0: self.assertRaises(TypeError, lambda: mock['foo']) michael@0: michael@0: michael@0: def test_adding_child_mock(self): michael@0: for Klass in NonCallableMock, Mock, MagicMock, NonCallableMagicMock: michael@0: mock = Klass() michael@0: michael@0: mock.foo = Mock() michael@0: mock.foo() michael@0: michael@0: self.assertEqual(mock.method_calls, [call.foo()]) michael@0: self.assertEqual(mock.mock_calls, [call.foo()]) michael@0: michael@0: mock = Klass() michael@0: mock.bar = Mock(name='name') michael@0: mock.bar() michael@0: self.assertEqual(mock.method_calls, []) michael@0: self.assertEqual(mock.mock_calls, []) michael@0: michael@0: # mock with an existing _new_parent but no name michael@0: mock = Klass() michael@0: mock.baz = MagicMock()() michael@0: mock.baz() michael@0: self.assertEqual(mock.method_calls, []) michael@0: self.assertEqual(mock.mock_calls, []) michael@0: michael@0: michael@0: def test_adding_return_value_mock(self): michael@0: for Klass in Mock, MagicMock: michael@0: mock = Klass() michael@0: mock.return_value = MagicMock() michael@0: michael@0: mock()() michael@0: self.assertEqual(mock.mock_calls, [call(), call()()]) michael@0: michael@0: michael@0: def test_manager_mock(self): michael@0: class Foo(object): michael@0: one = 'one' michael@0: two = 'two' michael@0: manager = Mock() michael@0: p1 = patch.object(Foo, 'one') michael@0: p2 = patch.object(Foo, 'two') michael@0: michael@0: mock_one = p1.start() michael@0: self.addCleanup(p1.stop) michael@0: mock_two = p2.start() michael@0: self.addCleanup(p2.stop) michael@0: michael@0: manager.attach_mock(mock_one, 'one') michael@0: manager.attach_mock(mock_two, 'two') michael@0: michael@0: Foo.two() michael@0: Foo.one() michael@0: michael@0: self.assertEqual(manager.mock_calls, [call.two(), call.one()]) michael@0: michael@0: michael@0: def test_magic_methods_mock_calls(self): michael@0: for Klass in Mock, MagicMock: michael@0: m = Klass() michael@0: m.__int__ = Mock(return_value=3) michael@0: m.__float__ = MagicMock(return_value=3.0) michael@0: int(m) michael@0: float(m) michael@0: michael@0: self.assertEqual(m.mock_calls, [call.__int__(), call.__float__()]) michael@0: self.assertEqual(m.method_calls, []) michael@0: michael@0: michael@0: def test_attribute_deletion(self): michael@0: # this behaviour isn't *useful*, but at least it's now tested... michael@0: for Klass in Mock, MagicMock, NonCallableMagicMock, NonCallableMock: michael@0: m = Klass() michael@0: original = m.foo michael@0: m.foo = 3 michael@0: del m.foo michael@0: self.assertEqual(m.foo, original) michael@0: michael@0: new = m.foo = Mock() michael@0: del m.foo michael@0: self.assertEqual(m.foo, new) michael@0: michael@0: michael@0: def test_mock_parents(self): michael@0: for Klass in Mock, MagicMock: michael@0: m = Klass() michael@0: original_repr = repr(m) michael@0: m.return_value = m michael@0: self.assertIs(m(), m) michael@0: self.assertEqual(repr(m), original_repr) michael@0: michael@0: m.reset_mock() michael@0: self.assertIs(m(), m) michael@0: self.assertEqual(repr(m), original_repr) michael@0: michael@0: m = Klass() michael@0: m.b = m.a michael@0: self.assertIn("name='mock.a'", repr(m.b)) michael@0: self.assertIn("name='mock.a'", repr(m.a)) michael@0: m.reset_mock() michael@0: self.assertIn("name='mock.a'", repr(m.b)) michael@0: self.assertIn("name='mock.a'", repr(m.a)) michael@0: michael@0: m = Klass() michael@0: original_repr = repr(m) michael@0: m.a = m() michael@0: m.a.return_value = m michael@0: michael@0: self.assertEqual(repr(m), original_repr) michael@0: self.assertEqual(repr(m.a()), original_repr) michael@0: michael@0: michael@0: def test_attach_mock(self): michael@0: classes = Mock, MagicMock, NonCallableMagicMock, NonCallableMock michael@0: for Klass in classes: michael@0: for Klass2 in classes: michael@0: m = Klass() michael@0: michael@0: m2 = Klass2(name='foo') michael@0: m.attach_mock(m2, 'bar') michael@0: michael@0: self.assertIs(m.bar, m2) michael@0: self.assertIn("name='mock.bar'", repr(m2)) michael@0: michael@0: m.bar.baz(1) michael@0: self.assertEqual(m.mock_calls, [call.bar.baz(1)]) michael@0: self.assertEqual(m.method_calls, [call.bar.baz(1)]) michael@0: michael@0: michael@0: def test_attach_mock_return_value(self): michael@0: classes = Mock, MagicMock, NonCallableMagicMock, NonCallableMock michael@0: for Klass in Mock, MagicMock: michael@0: for Klass2 in classes: michael@0: m = Klass() michael@0: michael@0: m2 = Klass2(name='foo') michael@0: m.attach_mock(m2, 'return_value') michael@0: michael@0: self.assertIs(m(), m2) michael@0: self.assertIn("name='mock()'", repr(m2)) michael@0: michael@0: m2.foo() michael@0: self.assertEqual(m.mock_calls, call().foo().call_list()) michael@0: michael@0: michael@0: def test_attribute_deletion(self): michael@0: for mock in Mock(), MagicMock(): michael@0: self.assertTrue(hasattr(mock, 'm')) michael@0: michael@0: del mock.m michael@0: self.assertFalse(hasattr(mock, 'm')) michael@0: michael@0: del mock.f michael@0: self.assertFalse(hasattr(mock, 'f')) michael@0: self.assertRaises(AttributeError, getattr, mock, 'f') michael@0: michael@0: michael@0: def test_class_assignable(self): michael@0: for mock in Mock(), MagicMock(): michael@0: self.assertNotIsInstance(mock, int) michael@0: michael@0: mock.__class__ = int michael@0: self.assertIsInstance(mock, int) michael@0: michael@0: michael@0: @unittest2.expectedFailure michael@0: def test_pickle(self): michael@0: for Klass in (MagicMock, Mock, Subclass, NonCallableMagicMock): michael@0: mock = Klass(name='foo', attribute=3) michael@0: mock.foo(1, 2, 3) michael@0: data = pickle.dumps(mock) michael@0: new = pickle.loads(data) michael@0: michael@0: new.foo.assert_called_once_with(1, 2, 3) michael@0: self.assertFalse(new.called) michael@0: self.assertTrue(is_instance(new, Klass)) michael@0: self.assertIsInstance(new, Thing) michael@0: self.assertIn('name="foo"', repr(new)) michael@0: self.assertEqual(new.attribute, 3) michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: unittest2.main()