ยปCore Development>Code coverage>Lib/unittest/test/testmock/testcallable.py

Python code coverage for Lib/unittest/test/testmock/testcallable.py

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