1 | n/a | # Copyright (C) 2007-2012 Michael Foord & the mock team |
---|
2 | n/a | # E-mail: fuzzyman AT voidspace DOT org DOT uk |
---|
3 | n/a | # http://www.voidspace.org.uk/python/mock/ |
---|
4 | n/a | |
---|
5 | n/a | import unittest |
---|
6 | n/a | from unittest.test.testmock.support import is_instance, X, SomeClass |
---|
7 | n/a | |
---|
8 | n/a | from unittest.mock import ( |
---|
9 | n/a | Mock, MagicMock, NonCallableMagicMock, |
---|
10 | n/a | NonCallableMock, patch, create_autospec, |
---|
11 | n/a | CallableMixin |
---|
12 | n/a | ) |
---|
13 | n/a | |
---|
14 | n/a | |
---|
15 | n/a | |
---|
16 | n/a | class TestCallable(unittest.TestCase): |
---|
17 | n/a | |
---|
18 | n/a | def assertNotCallable(self, mock): |
---|
19 | n/a | self.assertTrue(is_instance(mock, NonCallableMagicMock)) |
---|
20 | n/a | self.assertFalse(is_instance(mock, CallableMixin)) |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | def test_non_callable(self): |
---|
24 | n/a | for mock in NonCallableMagicMock(), NonCallableMock(): |
---|
25 | n/a | self.assertRaises(TypeError, mock) |
---|
26 | n/a | self.assertFalse(hasattr(mock, '__call__')) |
---|
27 | n/a | self.assertIn(mock.__class__.__name__, repr(mock)) |
---|
28 | n/a | |
---|
29 | n/a | |
---|
30 | n/a | def test_hierarchy(self): |
---|
31 | n/a | self.assertTrue(issubclass(MagicMock, Mock)) |
---|
32 | n/a | self.assertTrue(issubclass(NonCallableMagicMock, NonCallableMock)) |
---|
33 | n/a | |
---|
34 | n/a | |
---|
35 | n/a | def test_attributes(self): |
---|
36 | n/a | one = NonCallableMock() |
---|
37 | n/a | self.assertTrue(issubclass(type(one.one), Mock)) |
---|
38 | n/a | |
---|
39 | n/a | two = NonCallableMagicMock() |
---|
40 | n/a | self.assertTrue(issubclass(type(two.two), MagicMock)) |
---|
41 | n/a | |
---|
42 | n/a | |
---|
43 | n/a | def test_subclasses(self): |
---|
44 | n/a | class MockSub(Mock): |
---|
45 | n/a | pass |
---|
46 | n/a | |
---|
47 | n/a | one = MockSub() |
---|
48 | n/a | self.assertTrue(issubclass(type(one.one), MockSub)) |
---|
49 | n/a | |
---|
50 | n/a | class MagicSub(MagicMock): |
---|
51 | n/a | pass |
---|
52 | n/a | |
---|
53 | n/a | two = MagicSub() |
---|
54 | n/a | self.assertTrue(issubclass(type(two.two), MagicSub)) |
---|
55 | n/a | |
---|
56 | n/a | |
---|
57 | n/a | def test_patch_spec(self): |
---|
58 | n/a | patcher = patch('%s.X' % __name__, spec=True) |
---|
59 | n/a | mock = patcher.start() |
---|
60 | n/a | self.addCleanup(patcher.stop) |
---|
61 | n/a | |
---|
62 | n/a | instance = mock() |
---|
63 | n/a | mock.assert_called_once_with() |
---|
64 | n/a | |
---|
65 | n/a | self.assertNotCallable(instance) |
---|
66 | n/a | self.assertRaises(TypeError, instance) |
---|
67 | n/a | |
---|
68 | n/a | |
---|
69 | n/a | def test_patch_spec_set(self): |
---|
70 | n/a | patcher = patch('%s.X' % __name__, spec_set=True) |
---|
71 | n/a | mock = patcher.start() |
---|
72 | n/a | self.addCleanup(patcher.stop) |
---|
73 | n/a | |
---|
74 | n/a | instance = mock() |
---|
75 | n/a | mock.assert_called_once_with() |
---|
76 | n/a | |
---|
77 | n/a | self.assertNotCallable(instance) |
---|
78 | n/a | self.assertRaises(TypeError, instance) |
---|
79 | n/a | |
---|
80 | n/a | |
---|
81 | n/a | def test_patch_spec_instance(self): |
---|
82 | n/a | patcher = patch('%s.X' % __name__, spec=X()) |
---|
83 | n/a | mock = patcher.start() |
---|
84 | n/a | self.addCleanup(patcher.stop) |
---|
85 | n/a | |
---|
86 | n/a | self.assertNotCallable(mock) |
---|
87 | n/a | self.assertRaises(TypeError, mock) |
---|
88 | n/a | |
---|
89 | n/a | |
---|
90 | n/a | def test_patch_spec_set_instance(self): |
---|
91 | n/a | patcher = patch('%s.X' % __name__, spec_set=X()) |
---|
92 | n/a | mock = patcher.start() |
---|
93 | n/a | self.addCleanup(patcher.stop) |
---|
94 | n/a | |
---|
95 | n/a | self.assertNotCallable(mock) |
---|
96 | n/a | self.assertRaises(TypeError, mock) |
---|
97 | n/a | |
---|
98 | n/a | |
---|
99 | n/a | def test_patch_spec_callable_class(self): |
---|
100 | n/a | class CallableX(X): |
---|
101 | n/a | def __call__(self): |
---|
102 | n/a | pass |
---|
103 | n/a | |
---|
104 | n/a | class Sub(CallableX): |
---|
105 | n/a | pass |
---|
106 | n/a | |
---|
107 | n/a | class Multi(SomeClass, Sub): |
---|
108 | n/a | pass |
---|
109 | n/a | |
---|
110 | n/a | for arg in 'spec', 'spec_set': |
---|
111 | n/a | for Klass in CallableX, Sub, Multi: |
---|
112 | n/a | with patch('%s.X' % __name__, **{arg: Klass}) as mock: |
---|
113 | n/a | instance = mock() |
---|
114 | n/a | mock.assert_called_once_with() |
---|
115 | n/a | |
---|
116 | n/a | self.assertTrue(is_instance(instance, MagicMock)) |
---|
117 | n/a | # inherited spec |
---|
118 | n/a | self.assertRaises(AttributeError, getattr, instance, |
---|
119 | n/a | 'foobarbaz') |
---|
120 | n/a | |
---|
121 | n/a | result = instance() |
---|
122 | n/a | # instance is callable, result has no spec |
---|
123 | n/a | instance.assert_called_once_with() |
---|
124 | n/a | |
---|
125 | n/a | result(3, 2, 1) |
---|
126 | n/a | result.assert_called_once_with(3, 2, 1) |
---|
127 | n/a | result.foo(3, 2, 1) |
---|
128 | n/a | result.foo.assert_called_once_with(3, 2, 1) |
---|
129 | n/a | |
---|
130 | n/a | |
---|
131 | n/a | def test_create_autopsec(self): |
---|
132 | n/a | mock = create_autospec(X) |
---|
133 | n/a | instance = mock() |
---|
134 | n/a | self.assertRaises(TypeError, instance) |
---|
135 | n/a | |
---|
136 | n/a | mock = create_autospec(X()) |
---|
137 | n/a | self.assertRaises(TypeError, mock) |
---|
138 | n/a | |
---|
139 | n/a | |
---|
140 | n/a | def test_create_autospec_instance(self): |
---|
141 | n/a | mock = create_autospec(SomeClass, instance=True) |
---|
142 | n/a | |
---|
143 | n/a | self.assertRaises(TypeError, mock) |
---|
144 | n/a | mock.wibble() |
---|
145 | n/a | mock.wibble.assert_called_once_with() |
---|
146 | n/a | |
---|
147 | n/a | self.assertRaises(TypeError, mock.wibble, 'some', 'args') |
---|
148 | n/a | |
---|
149 | n/a | |
---|
150 | n/a | if __name__ == "__main__": |
---|
151 | n/a | unittest.main() |
---|