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

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

#countcontent
1n/aimport unittest
2n/a
3n/afrom unittest.mock import (
4n/a call, _Call, create_autospec, MagicMock,
5n/a Mock, ANY, _CallList, patch, PropertyMock
6n/a)
7n/a
8n/afrom datetime import datetime
9n/a
10n/aclass SomeClass(object):
11n/a def one(self, a, b):
12n/a pass
13n/a def two(self):
14n/a pass
15n/a def three(self, a=None):
16n/a pass
17n/a
18n/a
19n/a
20n/aclass AnyTest(unittest.TestCase):
21n/a
22n/a def test_any(self):
23n/a self.assertEqual(ANY, object())
24n/a
25n/a mock = Mock()
26n/a mock(ANY)
27n/a mock.assert_called_with(ANY)
28n/a
29n/a mock = Mock()
30n/a mock(foo=ANY)
31n/a mock.assert_called_with(foo=ANY)
32n/a
33n/a def test_repr(self):
34n/a self.assertEqual(repr(ANY), '<ANY>')
35n/a self.assertEqual(str(ANY), '<ANY>')
36n/a
37n/a
38n/a def test_any_and_datetime(self):
39n/a mock = Mock()
40n/a mock(datetime.now(), foo=datetime.now())
41n/a
42n/a mock.assert_called_with(ANY, foo=ANY)
43n/a
44n/a
45n/a def test_any_mock_calls_comparison_order(self):
46n/a mock = Mock()
47n/a d = datetime.now()
48n/a class Foo(object):
49n/a def __eq__(self, other):
50n/a return False
51n/a def __ne__(self, other):
52n/a return True
53n/a
54n/a for d in datetime.now(), Foo():
55n/a mock.reset_mock()
56n/a
57n/a mock(d, foo=d, bar=d)
58n/a mock.method(d, zinga=d, alpha=d)
59n/a mock().method(a1=d, z99=d)
60n/a
61n/a expected = [
62n/a call(ANY, foo=ANY, bar=ANY),
63n/a call.method(ANY, zinga=ANY, alpha=ANY),
64n/a call(), call().method(a1=ANY, z99=ANY)
65n/a ]
66n/a self.assertEqual(expected, mock.mock_calls)
67n/a self.assertEqual(mock.mock_calls, expected)
68n/a
69n/a
70n/a
71n/aclass CallTest(unittest.TestCase):
72n/a
73n/a def test_call_with_call(self):
74n/a kall = _Call()
75n/a self.assertEqual(kall, _Call())
76n/a self.assertEqual(kall, _Call(('',)))
77n/a self.assertEqual(kall, _Call(((),)))
78n/a self.assertEqual(kall, _Call(({},)))
79n/a self.assertEqual(kall, _Call(('', ())))
80n/a self.assertEqual(kall, _Call(('', {})))
81n/a self.assertEqual(kall, _Call(('', (), {})))
82n/a self.assertEqual(kall, _Call(('foo',)))
83n/a self.assertEqual(kall, _Call(('bar', ())))
84n/a self.assertEqual(kall, _Call(('baz', {})))
85n/a self.assertEqual(kall, _Call(('spam', (), {})))
86n/a
87n/a kall = _Call(((1, 2, 3),))
88n/a self.assertEqual(kall, _Call(((1, 2, 3),)))
89n/a self.assertEqual(kall, _Call(('', (1, 2, 3))))
90n/a self.assertEqual(kall, _Call(((1, 2, 3), {})))
91n/a self.assertEqual(kall, _Call(('', (1, 2, 3), {})))
92n/a
93n/a kall = _Call(((1, 2, 4),))
94n/a self.assertNotEqual(kall, _Call(('', (1, 2, 3))))
95n/a self.assertNotEqual(kall, _Call(('', (1, 2, 3), {})))
96n/a
97n/a kall = _Call(('foo', (1, 2, 4),))
98n/a self.assertNotEqual(kall, _Call(('', (1, 2, 4))))
99n/a self.assertNotEqual(kall, _Call(('', (1, 2, 4), {})))
100n/a self.assertNotEqual(kall, _Call(('bar', (1, 2, 4))))
101n/a self.assertNotEqual(kall, _Call(('bar', (1, 2, 4), {})))
102n/a
103n/a kall = _Call(({'a': 3},))
104n/a self.assertEqual(kall, _Call(('', (), {'a': 3})))
105n/a self.assertEqual(kall, _Call(('', {'a': 3})))
106n/a self.assertEqual(kall, _Call(((), {'a': 3})))
107n/a self.assertEqual(kall, _Call(({'a': 3},)))
108n/a
109n/a
110n/a def test_empty__Call(self):
111n/a args = _Call()
112n/a
113n/a self.assertEqual(args, ())
114n/a self.assertEqual(args, ('foo',))
115n/a self.assertEqual(args, ((),))
116n/a self.assertEqual(args, ('foo', ()))
117n/a self.assertEqual(args, ('foo',(), {}))
118n/a self.assertEqual(args, ('foo', {}))
119n/a self.assertEqual(args, ({},))
120n/a
121n/a
122n/a def test_named_empty_call(self):
123n/a args = _Call(('foo', (), {}))
124n/a
125n/a self.assertEqual(args, ('foo',))
126n/a self.assertEqual(args, ('foo', ()))
127n/a self.assertEqual(args, ('foo',(), {}))
128n/a self.assertEqual(args, ('foo', {}))
129n/a
130n/a self.assertNotEqual(args, ((),))
131n/a self.assertNotEqual(args, ())
132n/a self.assertNotEqual(args, ({},))
133n/a self.assertNotEqual(args, ('bar',))
134n/a self.assertNotEqual(args, ('bar', ()))
135n/a self.assertNotEqual(args, ('bar', {}))
136n/a
137n/a
138n/a def test_call_with_args(self):
139n/a args = _Call(((1, 2, 3), {}))
140n/a
141n/a self.assertEqual(args, ((1, 2, 3),))
142n/a self.assertEqual(args, ('foo', (1, 2, 3)))
143n/a self.assertEqual(args, ('foo', (1, 2, 3), {}))
144n/a self.assertEqual(args, ((1, 2, 3), {}))
145n/a
146n/a
147n/a def test_named_call_with_args(self):
148n/a args = _Call(('foo', (1, 2, 3), {}))
149n/a
150n/a self.assertEqual(args, ('foo', (1, 2, 3)))
151n/a self.assertEqual(args, ('foo', (1, 2, 3), {}))
152n/a
153n/a self.assertNotEqual(args, ((1, 2, 3),))
154n/a self.assertNotEqual(args, ((1, 2, 3), {}))
155n/a
156n/a
157n/a def test_call_with_kwargs(self):
158n/a args = _Call(((), dict(a=3, b=4)))
159n/a
160n/a self.assertEqual(args, (dict(a=3, b=4),))
161n/a self.assertEqual(args, ('foo', dict(a=3, b=4)))
162n/a self.assertEqual(args, ('foo', (), dict(a=3, b=4)))
163n/a self.assertEqual(args, ((), dict(a=3, b=4)))
164n/a
165n/a
166n/a def test_named_call_with_kwargs(self):
167n/a args = _Call(('foo', (), dict(a=3, b=4)))
168n/a
169n/a self.assertEqual(args, ('foo', dict(a=3, b=4)))
170n/a self.assertEqual(args, ('foo', (), dict(a=3, b=4)))
171n/a
172n/a self.assertNotEqual(args, (dict(a=3, b=4),))
173n/a self.assertNotEqual(args, ((), dict(a=3, b=4)))
174n/a
175n/a
176n/a def test_call_with_args_call_empty_name(self):
177n/a args = _Call(((1, 2, 3), {}))
178n/a self.assertEqual(args, call(1, 2, 3))
179n/a self.assertEqual(call(1, 2, 3), args)
180n/a self.assertIn(call(1, 2, 3), [args])
181n/a
182n/a
183n/a def test_call_ne(self):
184n/a self.assertNotEqual(_Call(((1, 2, 3),)), call(1, 2))
185n/a self.assertFalse(_Call(((1, 2, 3),)) != call(1, 2, 3))
186n/a self.assertTrue(_Call(((1, 2), {})) != call(1, 2, 3))
187n/a
188n/a
189n/a def test_call_non_tuples(self):
190n/a kall = _Call(((1, 2, 3),))
191n/a for value in 1, None, self, int:
192n/a self.assertNotEqual(kall, value)
193n/a self.assertFalse(kall == value)
194n/a
195n/a
196n/a def test_repr(self):
197n/a self.assertEqual(repr(_Call()), 'call()')
198n/a self.assertEqual(repr(_Call(('foo',))), 'call.foo()')
199n/a
200n/a self.assertEqual(repr(_Call(((1, 2, 3), {'a': 'b'}))),
201n/a "call(1, 2, 3, a='b')")
202n/a self.assertEqual(repr(_Call(('bar', (1, 2, 3), {'a': 'b'}))),
203n/a "call.bar(1, 2, 3, a='b')")
204n/a
205n/a self.assertEqual(repr(call), 'call')
206n/a self.assertEqual(str(call), 'call')
207n/a
208n/a self.assertEqual(repr(call()), 'call()')
209n/a self.assertEqual(repr(call(1)), 'call(1)')
210n/a self.assertEqual(repr(call(zz='thing')), "call(zz='thing')")
211n/a
212n/a self.assertEqual(repr(call().foo), 'call().foo')
213n/a self.assertEqual(repr(call(1).foo.bar(a=3).bing),
214n/a 'call().foo.bar().bing')
215n/a self.assertEqual(
216n/a repr(call().foo(1, 2, a=3)),
217n/a "call().foo(1, 2, a=3)"
218n/a )
219n/a self.assertEqual(repr(call()()), "call()()")
220n/a self.assertEqual(repr(call(1)(2)), "call()(2)")
221n/a self.assertEqual(
222n/a repr(call()().bar().baz.beep(1)),
223n/a "call()().bar().baz.beep(1)"
224n/a )
225n/a
226n/a
227n/a def test_call(self):
228n/a self.assertEqual(call(), ('', (), {}))
229n/a self.assertEqual(call('foo', 'bar', one=3, two=4),
230n/a ('', ('foo', 'bar'), {'one': 3, 'two': 4}))
231n/a
232n/a mock = Mock()
233n/a mock(1, 2, 3)
234n/a mock(a=3, b=6)
235n/a self.assertEqual(mock.call_args_list,
236n/a [call(1, 2, 3), call(a=3, b=6)])
237n/a
238n/a def test_attribute_call(self):
239n/a self.assertEqual(call.foo(1), ('foo', (1,), {}))
240n/a self.assertEqual(call.bar.baz(fish='eggs'),
241n/a ('bar.baz', (), {'fish': 'eggs'}))
242n/a
243n/a mock = Mock()
244n/a mock.foo(1, 2 ,3)
245n/a mock.bar.baz(a=3, b=6)
246n/a self.assertEqual(mock.method_calls,
247n/a [call.foo(1, 2, 3), call.bar.baz(a=3, b=6)])
248n/a
249n/a
250n/a def test_extended_call(self):
251n/a result = call(1).foo(2).bar(3, a=4)
252n/a self.assertEqual(result, ('().foo().bar', (3,), dict(a=4)))
253n/a
254n/a mock = MagicMock()
255n/a mock(1, 2, a=3, b=4)
256n/a self.assertEqual(mock.call_args, call(1, 2, a=3, b=4))
257n/a self.assertNotEqual(mock.call_args, call(1, 2, 3))
258n/a
259n/a self.assertEqual(mock.call_args_list, [call(1, 2, a=3, b=4)])
260n/a self.assertEqual(mock.mock_calls, [call(1, 2, a=3, b=4)])
261n/a
262n/a mock = MagicMock()
263n/a mock.foo(1).bar()().baz.beep(a=6)
264n/a
265n/a last_call = call.foo(1).bar()().baz.beep(a=6)
266n/a self.assertEqual(mock.mock_calls[-1], last_call)
267n/a self.assertEqual(mock.mock_calls, last_call.call_list())
268n/a
269n/a
270n/a def test_call_list(self):
271n/a mock = MagicMock()
272n/a mock(1)
273n/a self.assertEqual(call(1).call_list(), mock.mock_calls)
274n/a
275n/a mock = MagicMock()
276n/a mock(1).method(2)
277n/a self.assertEqual(call(1).method(2).call_list(),
278n/a mock.mock_calls)
279n/a
280n/a mock = MagicMock()
281n/a mock(1).method(2)(3)
282n/a self.assertEqual(call(1).method(2)(3).call_list(),
283n/a mock.mock_calls)
284n/a
285n/a mock = MagicMock()
286n/a int(mock(1).method(2)(3).foo.bar.baz(4)(5))
287n/a kall = call(1).method(2)(3).foo.bar.baz(4)(5).__int__()
288n/a self.assertEqual(kall.call_list(), mock.mock_calls)
289n/a
290n/a
291n/a def test_call_any(self):
292n/a self.assertEqual(call, ANY)
293n/a
294n/a m = MagicMock()
295n/a int(m)
296n/a self.assertEqual(m.mock_calls, [ANY])
297n/a self.assertEqual([ANY], m.mock_calls)
298n/a
299n/a
300n/a def test_two_args_call(self):
301n/a args = _Call(((1, 2), {'a': 3}), two=True)
302n/a self.assertEqual(len(args), 2)
303n/a self.assertEqual(args[0], (1, 2))
304n/a self.assertEqual(args[1], {'a': 3})
305n/a
306n/a other_args = _Call(((1, 2), {'a': 3}))
307n/a self.assertEqual(args, other_args)
308n/a
309n/a def test_call_with_name(self):
310n/a self.assertEqual(
311n/a 'foo',
312n/a _Call((), 'foo')[0],
313n/a )
314n/a self.assertEqual(
315n/a '',
316n/a _Call((('bar', 'barz'), ), )[0]
317n/a )
318n/a self.assertEqual(
319n/a '',
320n/a _Call((('bar', 'barz'), {'hello': 'world'}), )[0]
321n/a )
322n/a
323n/a
324n/aclass SpecSignatureTest(unittest.TestCase):
325n/a
326n/a def _check_someclass_mock(self, mock):
327n/a self.assertRaises(AttributeError, getattr, mock, 'foo')
328n/a mock.one(1, 2)
329n/a mock.one.assert_called_with(1, 2)
330n/a self.assertRaises(AssertionError,
331n/a mock.one.assert_called_with, 3, 4)
332n/a self.assertRaises(TypeError, mock.one, 1)
333n/a
334n/a mock.two()
335n/a mock.two.assert_called_with()
336n/a self.assertRaises(AssertionError,
337n/a mock.two.assert_called_with, 3)
338n/a self.assertRaises(TypeError, mock.two, 1)
339n/a
340n/a mock.three()
341n/a mock.three.assert_called_with()
342n/a self.assertRaises(AssertionError,
343n/a mock.three.assert_called_with, 3)
344n/a self.assertRaises(TypeError, mock.three, 3, 2)
345n/a
346n/a mock.three(1)
347n/a mock.three.assert_called_with(1)
348n/a
349n/a mock.three(a=1)
350n/a mock.three.assert_called_with(a=1)
351n/a
352n/a
353n/a def test_basic(self):
354n/a mock = create_autospec(SomeClass)
355n/a self._check_someclass_mock(mock)
356n/a mock = create_autospec(SomeClass())
357n/a self._check_someclass_mock(mock)
358n/a
359n/a
360n/a def test_create_autospec_return_value(self):
361n/a def f():
362n/a pass
363n/a mock = create_autospec(f, return_value='foo')
364n/a self.assertEqual(mock(), 'foo')
365n/a
366n/a class Foo(object):
367n/a pass
368n/a
369n/a mock = create_autospec(Foo, return_value='foo')
370n/a self.assertEqual(mock(), 'foo')
371n/a
372n/a
373n/a def test_autospec_reset_mock(self):
374n/a m = create_autospec(int)
375n/a int(m)
376n/a m.reset_mock()
377n/a self.assertEqual(m.__int__.call_count, 0)
378n/a
379n/a
380n/a def test_mocking_unbound_methods(self):
381n/a class Foo(object):
382n/a def foo(self, foo):
383n/a pass
384n/a p = patch.object(Foo, 'foo')
385n/a mock_foo = p.start()
386n/a Foo().foo(1)
387n/a
388n/a mock_foo.assert_called_with(1)
389n/a
390n/a
391n/a def test_create_autospec_unbound_methods(self):
392n/a # see mock issue 128
393n/a # this is expected to fail until the issue is fixed
394n/a return
395n/a class Foo(object):
396n/a def foo(self):
397n/a pass
398n/a
399n/a klass = create_autospec(Foo)
400n/a instance = klass()
401n/a self.assertRaises(TypeError, instance.foo, 1)
402n/a
403n/a # Note: no type checking on the "self" parameter
404n/a klass.foo(1)
405n/a klass.foo.assert_called_with(1)
406n/a self.assertRaises(TypeError, klass.foo)
407n/a
408n/a
409n/a def test_create_autospec_keyword_arguments(self):
410n/a class Foo(object):
411n/a a = 3
412n/a m = create_autospec(Foo, a='3')
413n/a self.assertEqual(m.a, '3')
414n/a
415n/a
416n/a def test_create_autospec_keyword_only_arguments(self):
417n/a def foo(a, *, b=None):
418n/a pass
419n/a
420n/a m = create_autospec(foo)
421n/a m(1)
422n/a m.assert_called_with(1)
423n/a self.assertRaises(TypeError, m, 1, 2)
424n/a
425n/a m(2, b=3)
426n/a m.assert_called_with(2, b=3)
427n/a
428n/a
429n/a def test_function_as_instance_attribute(self):
430n/a obj = SomeClass()
431n/a def f(a):
432n/a pass
433n/a obj.f = f
434n/a
435n/a mock = create_autospec(obj)
436n/a mock.f('bing')
437n/a mock.f.assert_called_with('bing')
438n/a
439n/a
440n/a def test_spec_as_list(self):
441n/a # because spec as a list of strings in the mock constructor means
442n/a # something very different we treat a list instance as the type.
443n/a mock = create_autospec([])
444n/a mock.append('foo')
445n/a mock.append.assert_called_with('foo')
446n/a
447n/a self.assertRaises(AttributeError, getattr, mock, 'foo')
448n/a
449n/a class Foo(object):
450n/a foo = []
451n/a
452n/a mock = create_autospec(Foo)
453n/a mock.foo.append(3)
454n/a mock.foo.append.assert_called_with(3)
455n/a self.assertRaises(AttributeError, getattr, mock.foo, 'foo')
456n/a
457n/a
458n/a def test_attributes(self):
459n/a class Sub(SomeClass):
460n/a attr = SomeClass()
461n/a
462n/a sub_mock = create_autospec(Sub)
463n/a
464n/a for mock in (sub_mock, sub_mock.attr):
465n/a self._check_someclass_mock(mock)
466n/a
467n/a
468n/a def test_builtin_functions_types(self):
469n/a # we could replace builtin functions / methods with a function
470n/a # with *args / **kwargs signature. Using the builtin method type
471n/a # as a spec seems to work fairly well though.
472n/a class BuiltinSubclass(list):
473n/a def bar(self, arg):
474n/a pass
475n/a sorted = sorted
476n/a attr = {}
477n/a
478n/a mock = create_autospec(BuiltinSubclass)
479n/a mock.append(3)
480n/a mock.append.assert_called_with(3)
481n/a self.assertRaises(AttributeError, getattr, mock.append, 'foo')
482n/a
483n/a mock.bar('foo')
484n/a mock.bar.assert_called_with('foo')
485n/a self.assertRaises(TypeError, mock.bar, 'foo', 'bar')
486n/a self.assertRaises(AttributeError, getattr, mock.bar, 'foo')
487n/a
488n/a mock.sorted([1, 2])
489n/a mock.sorted.assert_called_with([1, 2])
490n/a self.assertRaises(AttributeError, getattr, mock.sorted, 'foo')
491n/a
492n/a mock.attr.pop(3)
493n/a mock.attr.pop.assert_called_with(3)
494n/a self.assertRaises(AttributeError, getattr, mock.attr, 'foo')
495n/a
496n/a
497n/a def test_method_calls(self):
498n/a class Sub(SomeClass):
499n/a attr = SomeClass()
500n/a
501n/a mock = create_autospec(Sub)
502n/a mock.one(1, 2)
503n/a mock.two()
504n/a mock.three(3)
505n/a
506n/a expected = [call.one(1, 2), call.two(), call.three(3)]
507n/a self.assertEqual(mock.method_calls, expected)
508n/a
509n/a mock.attr.one(1, 2)
510n/a mock.attr.two()
511n/a mock.attr.three(3)
512n/a
513n/a expected.extend(
514n/a [call.attr.one(1, 2), call.attr.two(), call.attr.three(3)]
515n/a )
516n/a self.assertEqual(mock.method_calls, expected)
517n/a
518n/a
519n/a def test_magic_methods(self):
520n/a class BuiltinSubclass(list):
521n/a attr = {}
522n/a
523n/a mock = create_autospec(BuiltinSubclass)
524n/a self.assertEqual(list(mock), [])
525n/a self.assertRaises(TypeError, int, mock)
526n/a self.assertRaises(TypeError, int, mock.attr)
527n/a self.assertEqual(list(mock), [])
528n/a
529n/a self.assertIsInstance(mock['foo'], MagicMock)
530n/a self.assertIsInstance(mock.attr['foo'], MagicMock)
531n/a
532n/a
533n/a def test_spec_set(self):
534n/a class Sub(SomeClass):
535n/a attr = SomeClass()
536n/a
537n/a for spec in (Sub, Sub()):
538n/a mock = create_autospec(spec, spec_set=True)
539n/a self._check_someclass_mock(mock)
540n/a
541n/a self.assertRaises(AttributeError, setattr, mock, 'foo', 'bar')
542n/a self.assertRaises(AttributeError, setattr, mock.attr, 'foo', 'bar')
543n/a
544n/a
545n/a def test_descriptors(self):
546n/a class Foo(object):
547n/a @classmethod
548n/a def f(cls, a, b):
549n/a pass
550n/a @staticmethod
551n/a def g(a, b):
552n/a pass
553n/a
554n/a class Bar(Foo):
555n/a pass
556n/a
557n/a class Baz(SomeClass, Bar):
558n/a pass
559n/a
560n/a for spec in (Foo, Foo(), Bar, Bar(), Baz, Baz()):
561n/a mock = create_autospec(spec)
562n/a mock.f(1, 2)
563n/a mock.f.assert_called_once_with(1, 2)
564n/a
565n/a mock.g(3, 4)
566n/a mock.g.assert_called_once_with(3, 4)
567n/a
568n/a
569n/a def test_recursive(self):
570n/a class A(object):
571n/a def a(self):
572n/a pass
573n/a foo = 'foo bar baz'
574n/a bar = foo
575n/a
576n/a A.B = A
577n/a mock = create_autospec(A)
578n/a
579n/a mock()
580n/a self.assertFalse(mock.B.called)
581n/a
582n/a mock.a()
583n/a mock.B.a()
584n/a self.assertEqual(mock.method_calls, [call.a(), call.B.a()])
585n/a
586n/a self.assertIs(A.foo, A.bar)
587n/a self.assertIsNot(mock.foo, mock.bar)
588n/a mock.foo.lower()
589n/a self.assertRaises(AssertionError, mock.bar.lower.assert_called_with)
590n/a
591n/a
592n/a def test_spec_inheritance_for_classes(self):
593n/a class Foo(object):
594n/a def a(self, x):
595n/a pass
596n/a class Bar(object):
597n/a def f(self, y):
598n/a pass
599n/a
600n/a class_mock = create_autospec(Foo)
601n/a
602n/a self.assertIsNot(class_mock, class_mock())
603n/a
604n/a for this_mock in class_mock, class_mock():
605n/a this_mock.a(x=5)
606n/a this_mock.a.assert_called_with(x=5)
607n/a this_mock.a.assert_called_with(5)
608n/a self.assertRaises(TypeError, this_mock.a, 'foo', 'bar')
609n/a self.assertRaises(AttributeError, getattr, this_mock, 'b')
610n/a
611n/a instance_mock = create_autospec(Foo())
612n/a instance_mock.a(5)
613n/a instance_mock.a.assert_called_with(5)
614n/a instance_mock.a.assert_called_with(x=5)
615n/a self.assertRaises(TypeError, instance_mock.a, 'foo', 'bar')
616n/a self.assertRaises(AttributeError, getattr, instance_mock, 'b')
617n/a
618n/a # The return value isn't isn't callable
619n/a self.assertRaises(TypeError, instance_mock)
620n/a
621n/a instance_mock.Bar.f(6)
622n/a instance_mock.Bar.f.assert_called_with(6)
623n/a instance_mock.Bar.f.assert_called_with(y=6)
624n/a self.assertRaises(AttributeError, getattr, instance_mock.Bar, 'g')
625n/a
626n/a instance_mock.Bar().f(6)
627n/a instance_mock.Bar().f.assert_called_with(6)
628n/a instance_mock.Bar().f.assert_called_with(y=6)
629n/a self.assertRaises(AttributeError, getattr, instance_mock.Bar(), 'g')
630n/a
631n/a
632n/a def test_inherit(self):
633n/a class Foo(object):
634n/a a = 3
635n/a
636n/a Foo.Foo = Foo
637n/a
638n/a # class
639n/a mock = create_autospec(Foo)
640n/a instance = mock()
641n/a self.assertRaises(AttributeError, getattr, instance, 'b')
642n/a
643n/a attr_instance = mock.Foo()
644n/a self.assertRaises(AttributeError, getattr, attr_instance, 'b')
645n/a
646n/a # instance
647n/a mock = create_autospec(Foo())
648n/a self.assertRaises(AttributeError, getattr, mock, 'b')
649n/a self.assertRaises(TypeError, mock)
650n/a
651n/a # attribute instance
652n/a call_result = mock.Foo()
653n/a self.assertRaises(AttributeError, getattr, call_result, 'b')
654n/a
655n/a
656n/a def test_builtins(self):
657n/a # used to fail with infinite recursion
658n/a create_autospec(1)
659n/a
660n/a create_autospec(int)
661n/a create_autospec('foo')
662n/a create_autospec(str)
663n/a create_autospec({})
664n/a create_autospec(dict)
665n/a create_autospec([])
666n/a create_autospec(list)
667n/a create_autospec(set())
668n/a create_autospec(set)
669n/a create_autospec(1.0)
670n/a create_autospec(float)
671n/a create_autospec(1j)
672n/a create_autospec(complex)
673n/a create_autospec(False)
674n/a create_autospec(True)
675n/a
676n/a
677n/a def test_function(self):
678n/a def f(a, b):
679n/a pass
680n/a
681n/a mock = create_autospec(f)
682n/a self.assertRaises(TypeError, mock)
683n/a mock(1, 2)
684n/a mock.assert_called_with(1, 2)
685n/a mock.assert_called_with(1, b=2)
686n/a mock.assert_called_with(a=1, b=2)
687n/a
688n/a f.f = f
689n/a mock = create_autospec(f)
690n/a self.assertRaises(TypeError, mock.f)
691n/a mock.f(3, 4)
692n/a mock.f.assert_called_with(3, 4)
693n/a mock.f.assert_called_with(a=3, b=4)
694n/a
695n/a
696n/a def test_skip_attributeerrors(self):
697n/a class Raiser(object):
698n/a def __get__(self, obj, type=None):
699n/a if obj is None:
700n/a raise AttributeError('Can only be accessed via an instance')
701n/a
702n/a class RaiserClass(object):
703n/a raiser = Raiser()
704n/a
705n/a @staticmethod
706n/a def existing(a, b):
707n/a return a + b
708n/a
709n/a s = create_autospec(RaiserClass)
710n/a self.assertRaises(TypeError, lambda x: s.existing(1, 2, 3))
711n/a s.existing(1, 2)
712n/a self.assertRaises(AttributeError, lambda: s.nonexisting)
713n/a
714n/a # check we can fetch the raiser attribute and it has no spec
715n/a obj = s.raiser
716n/a obj.foo, obj.bar
717n/a
718n/a
719n/a def test_signature_class(self):
720n/a class Foo(object):
721n/a def __init__(self, a, b=3):
722n/a pass
723n/a
724n/a mock = create_autospec(Foo)
725n/a
726n/a self.assertRaises(TypeError, mock)
727n/a mock(1)
728n/a mock.assert_called_once_with(1)
729n/a mock.assert_called_once_with(a=1)
730n/a self.assertRaises(AssertionError, mock.assert_called_once_with, 2)
731n/a
732n/a mock(4, 5)
733n/a mock.assert_called_with(4, 5)
734n/a mock.assert_called_with(a=4, b=5)
735n/a self.assertRaises(AssertionError, mock.assert_called_with, a=5, b=4)
736n/a
737n/a
738n/a def test_class_with_no_init(self):
739n/a # this used to raise an exception
740n/a # due to trying to get a signature from object.__init__
741n/a class Foo(object):
742n/a pass
743n/a create_autospec(Foo)
744n/a
745n/a
746n/a def test_signature_callable(self):
747n/a class Callable(object):
748n/a def __init__(self, x, y):
749n/a pass
750n/a def __call__(self, a):
751n/a pass
752n/a
753n/a mock = create_autospec(Callable)
754n/a mock(1, 2)
755n/a mock.assert_called_once_with(1, 2)
756n/a mock.assert_called_once_with(x=1, y=2)
757n/a self.assertRaises(TypeError, mock, 'a')
758n/a
759n/a instance = mock(1, 2)
760n/a self.assertRaises(TypeError, instance)
761n/a instance(a='a')
762n/a instance.assert_called_once_with('a')
763n/a instance.assert_called_once_with(a='a')
764n/a instance('a')
765n/a instance.assert_called_with('a')
766n/a instance.assert_called_with(a='a')
767n/a
768n/a mock = create_autospec(Callable(1, 2))
769n/a mock(a='a')
770n/a mock.assert_called_once_with(a='a')
771n/a self.assertRaises(TypeError, mock)
772n/a mock('a')
773n/a mock.assert_called_with('a')
774n/a
775n/a
776n/a def test_signature_noncallable(self):
777n/a class NonCallable(object):
778n/a def __init__(self):
779n/a pass
780n/a
781n/a mock = create_autospec(NonCallable)
782n/a instance = mock()
783n/a mock.assert_called_once_with()
784n/a self.assertRaises(TypeError, mock, 'a')
785n/a self.assertRaises(TypeError, instance)
786n/a self.assertRaises(TypeError, instance, 'a')
787n/a
788n/a mock = create_autospec(NonCallable())
789n/a self.assertRaises(TypeError, mock)
790n/a self.assertRaises(TypeError, mock, 'a')
791n/a
792n/a
793n/a def test_create_autospec_none(self):
794n/a class Foo(object):
795n/a bar = None
796n/a
797n/a mock = create_autospec(Foo)
798n/a none = mock.bar
799n/a self.assertNotIsInstance(none, type(None))
800n/a
801n/a none.foo()
802n/a none.foo.assert_called_once_with()
803n/a
804n/a
805n/a def test_autospec_functions_with_self_in_odd_place(self):
806n/a class Foo(object):
807n/a def f(a, self):
808n/a pass
809n/a
810n/a a = create_autospec(Foo)
811n/a a.f(10)
812n/a a.f.assert_called_with(10)
813n/a a.f.assert_called_with(self=10)
814n/a a.f(self=10)
815n/a a.f.assert_called_with(10)
816n/a a.f.assert_called_with(self=10)
817n/a
818n/a
819n/a def test_autospec_data_descriptor(self):
820n/a class Descriptor(object):
821n/a def __init__(self, value):
822n/a self.value = value
823n/a
824n/a def __get__(self, obj, cls=None):
825n/a if obj is None:
826n/a return self
827n/a return self.value
828n/a
829n/a def __set__(self, obj, value):
830n/a pass
831n/a
832n/a class MyProperty(property):
833n/a pass
834n/a
835n/a class Foo(object):
836n/a __slots__ = ['slot']
837n/a
838n/a @property
839n/a def prop(self):
840n/a return 3
841n/a
842n/a @MyProperty
843n/a def subprop(self):
844n/a return 4
845n/a
846n/a desc = Descriptor(42)
847n/a
848n/a foo = create_autospec(Foo)
849n/a
850n/a def check_data_descriptor(mock_attr):
851n/a # Data descriptors don't have a spec.
852n/a self.assertIsInstance(mock_attr, MagicMock)
853n/a mock_attr(1, 2, 3)
854n/a mock_attr.abc(4, 5, 6)
855n/a mock_attr.assert_called_once_with(1, 2, 3)
856n/a mock_attr.abc.assert_called_once_with(4, 5, 6)
857n/a
858n/a # property
859n/a check_data_descriptor(foo.prop)
860n/a # property subclass
861n/a check_data_descriptor(foo.subprop)
862n/a # class __slot__
863n/a check_data_descriptor(foo.slot)
864n/a # plain data descriptor
865n/a check_data_descriptor(foo.desc)
866n/a
867n/a
868n/aclass TestCallList(unittest.TestCase):
869n/a
870n/a def test_args_list_contains_call_list(self):
871n/a mock = Mock()
872n/a self.assertIsInstance(mock.call_args_list, _CallList)
873n/a
874n/a mock(1, 2)
875n/a mock(a=3)
876n/a mock(3, 4)
877n/a mock(b=6)
878n/a
879n/a for kall in call(1, 2), call(a=3), call(3, 4), call(b=6):
880n/a self.assertIn(kall, mock.call_args_list)
881n/a
882n/a calls = [call(a=3), call(3, 4)]
883n/a self.assertIn(calls, mock.call_args_list)
884n/a calls = [call(1, 2), call(a=3)]
885n/a self.assertIn(calls, mock.call_args_list)
886n/a calls = [call(3, 4), call(b=6)]
887n/a self.assertIn(calls, mock.call_args_list)
888n/a calls = [call(3, 4)]
889n/a self.assertIn(calls, mock.call_args_list)
890n/a
891n/a self.assertNotIn(call('fish'), mock.call_args_list)
892n/a self.assertNotIn([call('fish')], mock.call_args_list)
893n/a
894n/a
895n/a def test_call_list_str(self):
896n/a mock = Mock()
897n/a mock(1, 2)
898n/a mock.foo(a=3)
899n/a mock.foo.bar().baz('fish', cat='dog')
900n/a
901n/a expected = (
902n/a "[call(1, 2),\n"
903n/a " call.foo(a=3),\n"
904n/a " call.foo.bar(),\n"
905n/a " call.foo.bar().baz('fish', cat='dog')]"
906n/a )
907n/a self.assertEqual(str(mock.mock_calls), expected)
908n/a
909n/a
910n/a def test_propertymock(self):
911n/a p = patch('%s.SomeClass.one' % __name__, new_callable=PropertyMock)
912n/a mock = p.start()
913n/a try:
914n/a SomeClass.one
915n/a mock.assert_called_once_with()
916n/a
917n/a s = SomeClass()
918n/a s.one
919n/a mock.assert_called_with()
920n/a self.assertEqual(mock.mock_calls, [call(), call()])
921n/a
922n/a s.one = 3
923n/a self.assertEqual(mock.mock_calls, [call(), call(), call(3)])
924n/a finally:
925n/a p.stop()
926n/a
927n/a
928n/a def test_propertymock_returnvalue(self):
929n/a m = MagicMock()
930n/a p = PropertyMock()
931n/a type(m).foo = p
932n/a
933n/a returned = m.foo
934n/a p.assert_called_once_with()
935n/a self.assertIsInstance(returned, MagicMock)
936n/a self.assertNotIsInstance(returned, PropertyMock)
937n/a
938n/a
939n/aif __name__ == '__main__':
940n/a unittest.main()