| 1 |
import inspect |
| 2 |
import time |
| 3 |
import types |
| 4 |
import unittest |
| 5 |
|
| 6 |
from unittest.mock import ( |
| 7 |
call, _Call, create_autospec, MagicMock, |
| 8 |
Mock, ANY, _CallList, patch, PropertyMock, _callable |
| 9 |
) |
| 10 |
|
| 11 |
from dataclasses import dataclass, field, InitVar |
| 12 |
from datetime import datetime |
| 13 |
from functools import partial |
| 14 |
from typing import ClassVar |
| 15 |
|
| 16 |
class SomeClass(object): |
| 17 |
def one(self, a, b): pass |
| 18 |
def two(self): pass |
| 19 |
def three(self, a=None): pass |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class AnyTest(unittest.TestCase): |
| 24 |
|
| 25 |
def test_any(self): |
| 26 |
self.assertEqual(ANY, object()) |
| 27 |
|
| 28 |
mock = Mock() |
| 29 |
mock(ANY) |
| 30 |
mock.assert_called_with(ANY) |
| 31 |
|
| 32 |
mock = Mock() |
| 33 |
mock(foo=ANY) |
| 34 |
mock.assert_called_with(foo=ANY) |
| 35 |
|
| 36 |
def test_repr(self): |
| 37 |
self.assertEqual(repr(ANY), '<ANY>') |
| 38 |
self.assertEqual(str(ANY), '<ANY>') |
| 39 |
|
| 40 |
|
| 41 |
def test_any_and_datetime(self): |
| 42 |
mock = Mock() |
| 43 |
mock(datetime.now(), foo=datetime.now()) |
| 44 |
|
| 45 |
mock.assert_called_with(ANY, foo=ANY) |
| 46 |
|
| 47 |
|
| 48 |
def test_any_mock_calls_comparison_order(self): |
| 49 |
mock = Mock() |
| 50 |
class Foo(object): |
| 51 |
def __eq__(self, other): pass |
| 52 |
def __ne__(self, other): pass |
| 53 |
|
| 54 |
for d in datetime.now(), Foo(): |
| 55 |
mock.reset_mock() |
| 56 |
|
| 57 |
mock(d, foo=d, bar=d) |
| 58 |
mock.method(d, zinga=d, alpha=d) |
| 59 |
mock().method(a1=d, z99=d) |
| 60 |
|
| 61 |
expected = [ |
| 62 |
call(ANY, foo=ANY, bar=ANY), |
| 63 |
call.method(ANY, zinga=ANY, alpha=ANY), |
| 64 |
call(), call().method(a1=ANY, z99=ANY) |
| 65 |
] |
| 66 |
self.assertEqual(expected, mock.mock_calls) |
| 67 |
self.assertEqual(mock.mock_calls, expected) |
| 68 |
|
| 69 |
def test_any_no_spec(self): |
| 70 |
# This is a regression test for bpo-37555 |
| 71 |
class Foo: |
| 72 |
def __eq__(self, other): pass |
| 73 |
|
| 74 |
mock = Mock() |
| 75 |
mock(Foo(), 1) |
| 76 |
mock.assert_has_calls([call(ANY, 1)]) |
| 77 |
mock.assert_called_with(ANY, 1) |
| 78 |
mock.assert_any_call(ANY, 1) |
| 79 |
|
| 80 |
def test_any_and_spec_set(self): |
| 81 |
# This is a regression test for bpo-37555 |
| 82 |
class Foo: |
| 83 |
def __eq__(self, other): pass |
| 84 |
|
| 85 |
mock = Mock(spec=Foo) |
| 86 |
|
| 87 |
mock(Foo(), 1) |
| 88 |
mock.assert_has_calls([call(ANY, 1)]) |
| 89 |
mock.assert_called_with(ANY, 1) |
| 90 |
mock.assert_any_call(ANY, 1) |
| 91 |
|
| 92 |
class CallTest(unittest.TestCase): |
| 93 |
|
| 94 |
def test_call_with_call(self): |
| 95 |
kall = _Call() |
| 96 |
self.assertEqual(kall, _Call()) |
| 97 |
self.assertEqual(kall, _Call(('',))) |
| 98 |
self.assertEqual(kall, _Call(((),))) |
| 99 |
self.assertEqual(kall, _Call(({},))) |
| 100 |
self.assertEqual(kall, _Call(('', ()))) |
| 101 |
self.assertEqual(kall, _Call(('', {}))) |
| 102 |
self.assertEqual(kall, _Call(('', (), {}))) |
| 103 |
self.assertEqual(kall, _Call(('foo',))) |
| 104 |
self.assertEqual(kall, _Call(('bar', ()))) |
| 105 |
self.assertEqual(kall, _Call(('baz', {}))) |
| 106 |
self.assertEqual(kall, _Call(('spam', (), {}))) |
| 107 |
|
| 108 |
kall = _Call(((1, 2, 3),)) |
| 109 |
self.assertEqual(kall, _Call(((1, 2, 3),))) |
| 110 |
self.assertEqual(kall, _Call(('', (1, 2, 3)))) |
| 111 |
self.assertEqual(kall, _Call(((1, 2, 3), {}))) |
| 112 |
self.assertEqual(kall, _Call(('', (1, 2, 3), {}))) |
| 113 |
|
| 114 |
kall = _Call(((1, 2, 4),)) |
| 115 |
self.assertNotEqual(kall, _Call(('', (1, 2, 3)))) |
| 116 |
self.assertNotEqual(kall, _Call(('', (1, 2, 3), {}))) |
| 117 |
|
| 118 |
kall = _Call(('foo', (1, 2, 4),)) |
| 119 |
self.assertNotEqual(kall, _Call(('', (1, 2, 4)))) |
| 120 |
self.assertNotEqual(kall, _Call(('', (1, 2, 4), {}))) |
| 121 |
self.assertNotEqual(kall, _Call(('bar', (1, 2, 4)))) |
| 122 |
self.assertNotEqual(kall, _Call(('bar', (1, 2, 4), {}))) |
| 123 |
|
| 124 |
kall = _Call(({'a': 3},)) |
| 125 |
self.assertEqual(kall, _Call(('', (), {'a': 3}))) |
| 126 |
self.assertEqual(kall, _Call(('', {'a': 3}))) |
| 127 |
self.assertEqual(kall, _Call(((), {'a': 3}))) |
| 128 |
self.assertEqual(kall, _Call(({'a': 3},))) |
| 129 |
|
| 130 |
|
| 131 |
def test_empty__Call(self): |
| 132 |
args = _Call() |
| 133 |
|
| 134 |
self.assertEqual(args, ()) |
| 135 |
self.assertEqual(args, ('foo',)) |
| 136 |
self.assertEqual(args, ((),)) |
| 137 |
self.assertEqual(args, ('foo', ())) |
| 138 |
self.assertEqual(args, ('foo',(), {})) |
| 139 |
self.assertEqual(args, ('foo', {})) |
| 140 |
self.assertEqual(args, ({},)) |
| 141 |
|
| 142 |
|
| 143 |
def test_named_empty_call(self): |
| 144 |
args = _Call(('foo', (), {})) |
| 145 |
|
| 146 |
self.assertEqual(args, ('foo',)) |
| 147 |
self.assertEqual(args, ('foo', ())) |
| 148 |
self.assertEqual(args, ('foo',(), {})) |
| 149 |
self.assertEqual(args, ('foo', {})) |
| 150 |
|
| 151 |
self.assertNotEqual(args, ((),)) |
| 152 |
self.assertNotEqual(args, ()) |
| 153 |
self.assertNotEqual(args, ({},)) |
| 154 |
self.assertNotEqual(args, ('bar',)) |
| 155 |
self.assertNotEqual(args, ('bar', ())) |
| 156 |
self.assertNotEqual(args, ('bar', {})) |
| 157 |
|
| 158 |
|
| 159 |
def test_call_with_args(self): |
| 160 |
args = _Call(((1, 2, 3), {})) |
| 161 |
|
| 162 |
self.assertEqual(args, ((1, 2, 3),)) |
| 163 |
self.assertEqual(args, ('foo', (1, 2, 3))) |
| 164 |
self.assertEqual(args, ('foo', (1, 2, 3), {})) |
| 165 |
self.assertEqual(args, ((1, 2, 3), {})) |
| 166 |
self.assertEqual(args.args, (1, 2, 3)) |
| 167 |
self.assertEqual(args.kwargs, {}) |
| 168 |
|
| 169 |
|
| 170 |
def test_named_call_with_args(self): |
| 171 |
args = _Call(('foo', (1, 2, 3), {})) |
| 172 |
|
| 173 |
self.assertEqual(args, ('foo', (1, 2, 3))) |
| 174 |
self.assertEqual(args, ('foo', (1, 2, 3), {})) |
| 175 |
self.assertEqual(args.args, (1, 2, 3)) |
| 176 |
self.assertEqual(args.kwargs, {}) |
| 177 |
|
| 178 |
self.assertNotEqual(args, ((1, 2, 3),)) |
| 179 |
self.assertNotEqual(args, ((1, 2, 3), {})) |
| 180 |
|
| 181 |
|
| 182 |
def test_call_with_kwargs(self): |
| 183 |
args = _Call(((), dict(a=3, b=4))) |
| 184 |
|
| 185 |
self.assertEqual(args, (dict(a=3, b=4),)) |
| 186 |
self.assertEqual(args, ('foo', dict(a=3, b=4))) |
| 187 |
self.assertEqual(args, ('foo', (), dict(a=3, b=4))) |
| 188 |
self.assertEqual(args, ((), dict(a=3, b=4))) |
| 189 |
self.assertEqual(args.args, ()) |
| 190 |
self.assertEqual(args.kwargs, dict(a=3, b=4)) |
| 191 |
|
| 192 |
|
| 193 |
def test_named_call_with_kwargs(self): |
| 194 |
args = _Call(('foo', (), dict(a=3, b=4))) |
| 195 |
|
| 196 |
self.assertEqual(args, ('foo', dict(a=3, b=4))) |
| 197 |
self.assertEqual(args, ('foo', (), dict(a=3, b=4))) |
| 198 |
self.assertEqual(args.args, ()) |
| 199 |
self.assertEqual(args.kwargs, dict(a=3, b=4)) |
| 200 |
|
| 201 |
self.assertNotEqual(args, (dict(a=3, b=4),)) |
| 202 |
self.assertNotEqual(args, ((), dict(a=3, b=4))) |
| 203 |
|
| 204 |
|
| 205 |
def test_call_with_args_call_empty_name(self): |
| 206 |
args = _Call(((1, 2, 3), {})) |
| 207 |
|
| 208 |
self.assertEqual(args, call(1, 2, 3)) |
| 209 |
self.assertEqual(call(1, 2, 3), args) |
| 210 |
self.assertIn(call(1, 2, 3), [args]) |
| 211 |
|
| 212 |
|
| 213 |
def test_call_ne(self): |
| 214 |
self.assertNotEqual(_Call(((1, 2, 3),)), call(1, 2)) |
| 215 |
self.assertFalse(_Call(((1, 2, 3),)) != call(1, 2, 3)) |
| 216 |
self.assertTrue(_Call(((1, 2), {})) != call(1, 2, 3)) |
| 217 |
|
| 218 |
|
| 219 |
def test_call_non_tuples(self): |
| 220 |
kall = _Call(((1, 2, 3),)) |
| 221 |
for value in 1, None, self, int: |
| 222 |
self.assertNotEqual(kall, value) |
| 223 |
self.assertFalse(kall == value) |
| 224 |
|
| 225 |
|
| 226 |
def test_repr(self): |
| 227 |
self.assertEqual(repr(_Call()), 'call()') |
| 228 |
self.assertEqual(repr(_Call(('foo',))), 'call.foo()') |
| 229 |
|
| 230 |
self.assertEqual(repr(_Call(((1, 2, 3), {'a': 'b'}))), |
| 231 |
"call(1, 2, 3, a='b')") |
| 232 |
self.assertEqual(repr(_Call(('bar', (1, 2, 3), {'a': 'b'}))), |
| 233 |
"call.bar(1, 2, 3, a='b')") |
| 234 |
|
| 235 |
self.assertEqual(repr(call), 'call') |
| 236 |
self.assertEqual(str(call), 'call') |
| 237 |
|
| 238 |
self.assertEqual(repr(call()), 'call()') |
| 239 |
self.assertEqual(repr(call(1)), 'call(1)') |
| 240 |
self.assertEqual(repr(call(zz='thing')), "call(zz='thing')") |
| 241 |
|
| 242 |
self.assertEqual(repr(call().foo), 'call().foo') |
| 243 |
self.assertEqual(repr(call(1).foo.bar(a=3).bing), |
| 244 |
'call().foo.bar().bing') |
| 245 |
self.assertEqual( |
| 246 |
repr(call().foo(1, 2, a=3)), |
| 247 |
"call().foo(1, 2, a=3)" |
| 248 |
) |
| 249 |
self.assertEqual(repr(call()()), "call()()") |
| 250 |
self.assertEqual(repr(call(1)(2)), "call()(2)") |
| 251 |
self.assertEqual( |
| 252 |
repr(call()().bar().baz.beep(1)), |
| 253 |
"call()().bar().baz.beep(1)" |
| 254 |
) |
| 255 |
|
| 256 |
|
| 257 |
def test_call(self): |
| 258 |
self.assertEqual(call(), ('', (), {})) |
| 259 |
self.assertEqual(call('foo', 'bar', one=3, two=4), |
| 260 |
('', ('foo', 'bar'), {'one': 3, 'two': 4})) |
| 261 |
|
| 262 |
mock = Mock() |
| 263 |
mock(1, 2, 3) |
| 264 |
mock(a=3, b=6) |
| 265 |
self.assertEqual(mock.call_args_list, |
| 266 |
[call(1, 2, 3), call(a=3, b=6)]) |
| 267 |
|
| 268 |
def test_attribute_call(self): |
| 269 |
self.assertEqual(call.foo(1), ('foo', (1,), {})) |
| 270 |
self.assertEqual(call.bar.baz(fish='eggs'), |
| 271 |
('bar.baz', (), {'fish': 'eggs'})) |
| 272 |
|
| 273 |
mock = Mock() |
| 274 |
mock.foo(1, 2 ,3) |
| 275 |
mock.bar.baz(a=3, b=6) |
| 276 |
self.assertEqual(mock.method_calls, |
| 277 |
[call.foo(1, 2, 3), call.bar.baz(a=3, b=6)]) |
| 278 |
|
| 279 |
|
| 280 |
def test_extended_call(self): |
| 281 |
result = call(1).foo(2).bar(3, a=4) |
| 282 |
self.assertEqual(result, ('().foo().bar', (3,), dict(a=4))) |
| 283 |
|
| 284 |
mock = MagicMock() |
| 285 |
mock(1, 2, a=3, b=4) |
| 286 |
self.assertEqual(mock.call_args, call(1, 2, a=3, b=4)) |
| 287 |
self.assertNotEqual(mock.call_args, call(1, 2, 3)) |
| 288 |
|
| 289 |
self.assertEqual(mock.call_args_list, [call(1, 2, a=3, b=4)]) |
| 290 |
self.assertEqual(mock.mock_calls, [call(1, 2, a=3, b=4)]) |
| 291 |
|
| 292 |
mock = MagicMock() |
| 293 |
mock.foo(1).bar()().baz.beep(a=6) |
| 294 |
|
| 295 |
last_call = call.foo(1).bar()().baz.beep(a=6) |
| 296 |
self.assertEqual(mock.mock_calls[-1], last_call) |
| 297 |
self.assertEqual(mock.mock_calls, last_call.call_list()) |
| 298 |
|
| 299 |
|
| 300 |
def test_extended_not_equal(self): |
| 301 |
a = call(x=1).foo |
| 302 |
b = call(x=2).foo |
| 303 |
self.assertEqual(a, a) |
| 304 |
self.assertEqual(b, b) |
| 305 |
self.assertNotEqual(a, b) |
| 306 |
|
| 307 |
|
| 308 |
def test_nested_calls_not_equal(self): |
| 309 |
a = call(x=1).foo().bar |
| 310 |
b = call(x=2).foo().bar |
| 311 |
self.assertEqual(a, a) |
| 312 |
self.assertEqual(b, b) |
| 313 |
self.assertNotEqual(a, b) |
| 314 |
|
| 315 |
|
| 316 |
def test_call_list(self): |
| 317 |
mock = MagicMock() |
| 318 |
mock(1) |
| 319 |
self.assertEqual(call(1).call_list(), mock.mock_calls) |
| 320 |
|
| 321 |
mock = MagicMock() |
| 322 |
mock(1).method(2) |
| 323 |
self.assertEqual(call(1).method(2).call_list(), |
| 324 |
mock.mock_calls) |
| 325 |
|
| 326 |
mock = MagicMock() |
| 327 |
mock(1).method(2)(3) |
| 328 |
self.assertEqual(call(1).method(2)(3).call_list(), |
| 329 |
mock.mock_calls) |
| 330 |
|
| 331 |
mock = MagicMock() |
| 332 |
int(mock(1).method(2)(3).foo.bar.baz(4)(5)) |
| 333 |
kall = call(1).method(2)(3).foo.bar.baz(4)(5).__int__() |
| 334 |
self.assertEqual(kall.call_list(), mock.mock_calls) |
| 335 |
|
| 336 |
|
| 337 |
def test_call_any(self): |
| 338 |
self.assertEqual(call, ANY) |
| 339 |
|
| 340 |
m = MagicMock() |
| 341 |
int(m) |
| 342 |
self.assertEqual(m.mock_calls, [ANY]) |
| 343 |
self.assertEqual([ANY], m.mock_calls) |
| 344 |
|
| 345 |
|
| 346 |
def test_two_args_call(self): |
| 347 |
args = _Call(((1, 2), {'a': 3}), two=True) |
| 348 |
self.assertEqual(len(args), 2) |
| 349 |
self.assertEqual(args[0], (1, 2)) |
| 350 |
self.assertEqual(args[1], {'a': 3}) |
| 351 |
|
| 352 |
other_args = _Call(((1, 2), {'a': 3})) |
| 353 |
self.assertEqual(args, other_args) |
| 354 |
|
| 355 |
def test_call_with_name(self): |
| 356 |
self.assertEqual(_Call((), 'foo')[0], 'foo') |
| 357 |
self.assertEqual(_Call((('bar', 'barz'),),)[0], '') |
| 358 |
self.assertEqual(_Call((('bar', 'barz'), {'hello': 'world'}),)[0], '') |
| 359 |
|
| 360 |
def test_dunder_call(self): |
| 361 |
m = MagicMock() |
| 362 |
m().foo()['bar']() |
| 363 |
self.assertEqual( |
| 364 |
m.mock_calls, |
| 365 |
[call(), call().foo(), call().foo().__getitem__('bar'), call().foo().__getitem__()()] |
| 366 |
) |
| 367 |
m = MagicMock() |
| 368 |
m().foo()['bar'] = 1 |
| 369 |
self.assertEqual( |
| 370 |
m.mock_calls, |
| 371 |
[call(), call().foo(), call().foo().__setitem__('bar', 1)] |
| 372 |
) |
| 373 |
m = MagicMock() |
| 374 |
iter(m().foo()) |
| 375 |
self.assertEqual( |
| 376 |
m.mock_calls, |
| 377 |
[call(), call().foo(), call().foo().__iter__()] |
| 378 |
) |
| 379 |
|
| 380 |
|
| 381 |
class SpecSignatureTest(unittest.TestCase): |
| 382 |
|
| 383 |
def _check_someclass_mock(self, mock): |
| 384 |
self.assertRaises(AttributeError, getattr, mock, 'foo') |
| 385 |
mock.one(1, 2) |
| 386 |
mock.one.assert_called_with(1, 2) |
| 387 |
self.assertRaises(AssertionError, |
| 388 |
mock.one.assert_called_with, 3, 4) |
| 389 |
self.assertRaises(TypeError, mock.one, 1) |
| 390 |
|
| 391 |
mock.two() |
| 392 |
mock.two.assert_called_with() |
| 393 |
self.assertRaises(AssertionError, |
| 394 |
mock.two.assert_called_with, 3) |
| 395 |
self.assertRaises(TypeError, mock.two, 1) |
| 396 |
|
| 397 |
mock.three() |
| 398 |
mock.three.assert_called_with() |
| 399 |
self.assertRaises(AssertionError, |
| 400 |
mock.three.assert_called_with, 3) |
| 401 |
self.assertRaises(TypeError, mock.three, 3, 2) |
| 402 |
|
| 403 |
mock.three(1) |
| 404 |
mock.three.assert_called_with(1) |
| 405 |
|
| 406 |
mock.three(a=1) |
| 407 |
mock.three.assert_called_with(a=1) |
| 408 |
|
| 409 |
|
| 410 |
def test_basic(self): |
| 411 |
mock = create_autospec(SomeClass) |
| 412 |
self._check_someclass_mock(mock) |
| 413 |
mock = create_autospec(SomeClass()) |
| 414 |
self._check_someclass_mock(mock) |
| 415 |
|
| 416 |
|
| 417 |
def test_create_autospec_return_value(self): |
| 418 |
def f(): pass |
| 419 |
mock = create_autospec(f, return_value='foo') |
| 420 |
self.assertEqual(mock(), 'foo') |
| 421 |
|
| 422 |
class Foo(object): |
| 423 |
pass |
| 424 |
|
| 425 |
mock = create_autospec(Foo, return_value='foo') |
| 426 |
self.assertEqual(mock(), 'foo') |
| 427 |
|
| 428 |
|
| 429 |
def test_autospec_reset_mock(self): |
| 430 |
m = create_autospec(int) |
| 431 |
int(m) |
| 432 |
m.reset_mock() |
| 433 |
self.assertEqual(m.__int__.call_count, 0) |
| 434 |
|
| 435 |
|
| 436 |
def test_mocking_unbound_methods(self): |
| 437 |
class Foo(object): |
| 438 |
def foo(self, foo): pass |
| 439 |
p = patch.object(Foo, 'foo') |
| 440 |
mock_foo = p.start() |
| 441 |
Foo().foo(1) |
| 442 |
|
| 443 |
mock_foo.assert_called_with(1) |
| 444 |
|
| 445 |
|
| 446 |
def test_create_autospec_keyword_arguments(self): |
| 447 |
class Foo(object): |
| 448 |
a = 3 |
| 449 |
m = create_autospec(Foo, a='3') |
| 450 |
self.assertEqual(m.a, '3') |
| 451 |
|
| 452 |
|
| 453 |
def test_create_autospec_keyword_only_arguments(self): |
| 454 |
def foo(a, *, b=None): pass |
| 455 |
|
| 456 |
m = create_autospec(foo) |
| 457 |
m(1) |
| 458 |
m.assert_called_with(1) |
| 459 |
self.assertRaises(TypeError, m, 1, 2) |
| 460 |
|
| 461 |
m(2, b=3) |
| 462 |
m.assert_called_with(2, b=3) |
| 463 |
|
| 464 |
|
| 465 |
def test_function_as_instance_attribute(self): |
| 466 |
obj = SomeClass() |
| 467 |
def f(a): pass |
| 468 |
obj.f = f |
| 469 |
|
| 470 |
mock = create_autospec(obj) |
| 471 |
mock.f('bing') |
| 472 |
mock.f.assert_called_with('bing') |
| 473 |
|
| 474 |
|
| 475 |
def test_spec_as_list(self): |
| 476 |
# because spec as a list of strings in the mock constructor means |
| 477 |
# something very different we treat a list instance as the type. |
| 478 |
mock = create_autospec([]) |
| 479 |
mock.append('foo') |
| 480 |
mock.append.assert_called_with('foo') |
| 481 |
|
| 482 |
self.assertRaises(AttributeError, getattr, mock, 'foo') |
| 483 |
|
| 484 |
class Foo(object): |
| 485 |
foo = [] |
| 486 |
|
| 487 |
mock = create_autospec(Foo) |
| 488 |
mock.foo.append(3) |
| 489 |
mock.foo.append.assert_called_with(3) |
| 490 |
self.assertRaises(AttributeError, getattr, mock.foo, 'foo') |
| 491 |
|
| 492 |
|
| 493 |
def test_attributes(self): |
| 494 |
class Sub(SomeClass): |
| 495 |
attr = SomeClass() |
| 496 |
|
| 497 |
sub_mock = create_autospec(Sub) |
| 498 |
|
| 499 |
for mock in (sub_mock, sub_mock.attr): |
| 500 |
self._check_someclass_mock(mock) |
| 501 |
|
| 502 |
|
| 503 |
def test_spec_has_descriptor_returning_function(self): |
| 504 |
|
| 505 |
class CrazyDescriptor(object): |
| 506 |
|
| 507 |
def __get__(self, obj, type_): |
| 508 |
if obj is None: |
| 509 |
return lambda x: None |
| 510 |
|
| 511 |
class MyClass(object): |
| 512 |
|
| 513 |
some_attr = CrazyDescriptor() |
| 514 |
|
| 515 |
mock = create_autospec(MyClass) |
| 516 |
mock.some_attr(1) |
| 517 |
with self.assertRaises(TypeError): |
| 518 |
mock.some_attr() |
| 519 |
with self.assertRaises(TypeError): |
| 520 |
mock.some_attr(1, 2) |
| 521 |
|
| 522 |
|
| 523 |
def test_spec_has_function_not_in_bases(self): |
| 524 |
|
| 525 |
class CrazyClass(object): |
| 526 |
|
| 527 |
def __dir__(self): |
| 528 |
return super(CrazyClass, self).__dir__()+['crazy'] |
| 529 |
|
| 530 |
def __getattr__(self, item): |
| 531 |
if item == 'crazy': |
| 532 |
return lambda x: x |
| 533 |
raise AttributeError(item) |
| 534 |
|
| 535 |
inst = CrazyClass() |
| 536 |
with self.assertRaises(AttributeError): |
| 537 |
inst.other |
| 538 |
self.assertEqual(inst.crazy(42), 42) |
| 539 |
|
| 540 |
mock = create_autospec(inst) |
| 541 |
mock.crazy(42) |
| 542 |
with self.assertRaises(TypeError): |
| 543 |
mock.crazy() |
| 544 |
with self.assertRaises(TypeError): |
| 545 |
mock.crazy(1, 2) |
| 546 |
|
| 547 |
|
| 548 |
def test_builtin_functions_types(self): |
| 549 |
# we could replace builtin functions / methods with a function |
| 550 |
# with *args / **kwargs signature. Using the builtin method type |
| 551 |
# as a spec seems to work fairly well though. |
| 552 |
class BuiltinSubclass(list): |
| 553 |
def bar(self, arg): pass |
| 554 |
sorted = sorted |
| 555 |
attr = {} |
| 556 |
|
| 557 |
mock = create_autospec(BuiltinSubclass) |
| 558 |
mock.append(3) |
| 559 |
mock.append.assert_called_with(3) |
| 560 |
self.assertRaises(AttributeError, getattr, mock.append, 'foo') |
| 561 |
|
| 562 |
mock.bar('foo') |
| 563 |
mock.bar.assert_called_with('foo') |
| 564 |
self.assertRaises(TypeError, mock.bar, 'foo', 'bar') |
| 565 |
self.assertRaises(AttributeError, getattr, mock.bar, 'foo') |
| 566 |
|
| 567 |
mock.sorted([1, 2]) |
| 568 |
mock.sorted.assert_called_with([1, 2]) |
| 569 |
self.assertRaises(AttributeError, getattr, mock.sorted, 'foo') |
| 570 |
|
| 571 |
mock.attr.pop(3) |
| 572 |
mock.attr.pop.assert_called_with(3) |
| 573 |
self.assertRaises(AttributeError, getattr, mock.attr, 'foo') |
| 574 |
|
| 575 |
|
| 576 |
def test_method_calls(self): |
| 577 |
class Sub(SomeClass): |
| 578 |
attr = SomeClass() |
| 579 |
|
| 580 |
mock = create_autospec(Sub) |
| 581 |
mock.one(1, 2) |
| 582 |
mock.two() |
| 583 |
mock.three(3) |
| 584 |
|
| 585 |
expected = [call.one(1, 2), call.two(), call.three(3)] |
| 586 |
self.assertEqual(mock.method_calls, expected) |
| 587 |
|
| 588 |
mock.attr.one(1, 2) |
| 589 |
mock.attr.two() |
| 590 |
mock.attr.three(3) |
| 591 |
|
| 592 |
expected.extend( |
| 593 |
[call.attr.one(1, 2), call.attr.two(), call.attr.three(3)] |
| 594 |
) |
| 595 |
self.assertEqual(mock.method_calls, expected) |
| 596 |
|
| 597 |
|
| 598 |
def test_magic_methods(self): |
| 599 |
class BuiltinSubclass(list): |
| 600 |
attr = {} |
| 601 |
|
| 602 |
mock = create_autospec(BuiltinSubclass) |
| 603 |
self.assertEqual(list(mock), []) |
| 604 |
self.assertRaises(TypeError, int, mock) |
| 605 |
self.assertRaises(TypeError, int, mock.attr) |
| 606 |
self.assertEqual(list(mock), []) |
| 607 |
|
| 608 |
self.assertIsInstance(mock['foo'], MagicMock) |
| 609 |
self.assertIsInstance(mock.attr['foo'], MagicMock) |
| 610 |
|
| 611 |
|
| 612 |
def test_spec_set(self): |
| 613 |
class Sub(SomeClass): |
| 614 |
attr = SomeClass() |
| 615 |
|
| 616 |
for spec in (Sub, Sub()): |
| 617 |
mock = create_autospec(spec, spec_set=True) |
| 618 |
self._check_someclass_mock(mock) |
| 619 |
|
| 620 |
self.assertRaises(AttributeError, setattr, mock, 'foo', 'bar') |
| 621 |
self.assertRaises(AttributeError, setattr, mock.attr, 'foo', 'bar') |
| 622 |
|
| 623 |
|
| 624 |
def test_descriptors(self): |
| 625 |
class Foo(object): |
| 626 |
@classmethod |
| 627 |
def f(cls, a, b): pass |
| 628 |
@staticmethod |
| 629 |
def g(a, b): pass |
| 630 |
|
| 631 |
class Bar(Foo): pass |
| 632 |
|
| 633 |
class Baz(SomeClass, Bar): pass |
| 634 |
|
| 635 |
for spec in (Foo, Foo(), Bar, Bar(), Baz, Baz()): |
| 636 |
mock = create_autospec(spec) |
| 637 |
mock.f(1, 2) |
| 638 |
mock.f.assert_called_once_with(1, 2) |
| 639 |
|
| 640 |
mock.g(3, 4) |
| 641 |
mock.g.assert_called_once_with(3, 4) |
| 642 |
|
| 643 |
|
| 644 |
def test_recursive(self): |
| 645 |
class A(object): |
| 646 |
def a(self): pass |
| 647 |
foo = 'foo bar baz' |
| 648 |
bar = foo |
| 649 |
|
| 650 |
A.B = A |
| 651 |
mock = create_autospec(A) |
| 652 |
|
| 653 |
mock() |
| 654 |
self.assertFalse(mock.B.called) |
| 655 |
|
| 656 |
mock.a() |
| 657 |
mock.B.a() |
| 658 |
self.assertEqual(mock.method_calls, [call.a(), call.B.a()]) |
| 659 |
|
| 660 |
self.assertIs(A.foo, A.bar) |
| 661 |
self.assertIsNot(mock.foo, mock.bar) |
| 662 |
mock.foo.lower() |
| 663 |
self.assertRaises(AssertionError, mock.bar.lower.assert_called_with) |
| 664 |
|
| 665 |
|
| 666 |
def test_spec_inheritance_for_classes(self): |
| 667 |
class Foo(object): |
| 668 |
def a(self, x): pass |
| 669 |
class Bar(object): |
| 670 |
def f(self, y): pass |
| 671 |
|
| 672 |
class_mock = create_autospec(Foo) |
| 673 |
|
| 674 |
self.assertIsNot(class_mock, class_mock()) |
| 675 |
|
| 676 |
for this_mock in class_mock, class_mock(): |
| 677 |
this_mock.a(x=5) |
| 678 |
this_mock.a.assert_called_with(x=5) |
| 679 |
this_mock.a.assert_called_with(5) |
| 680 |
self.assertRaises(TypeError, this_mock.a, 'foo', 'bar') |
| 681 |
self.assertRaises(AttributeError, getattr, this_mock, 'b') |
| 682 |
|
| 683 |
instance_mock = create_autospec(Foo()) |
| 684 |
instance_mock.a(5) |
| 685 |
instance_mock.a.assert_called_with(5) |
| 686 |
instance_mock.a.assert_called_with(x=5) |
| 687 |
self.assertRaises(TypeError, instance_mock.a, 'foo', 'bar') |
| 688 |
self.assertRaises(AttributeError, getattr, instance_mock, 'b') |
| 689 |
|
| 690 |
# The return value isn't isn't callable |
| 691 |
self.assertRaises(TypeError, instance_mock) |
| 692 |
|
| 693 |
instance_mock.Bar.f(6) |
| 694 |
instance_mock.Bar.f.assert_called_with(6) |
| 695 |
instance_mock.Bar.f.assert_called_with(y=6) |
| 696 |
self.assertRaises(AttributeError, getattr, instance_mock.Bar, 'g') |
| 697 |
|
| 698 |
instance_mock.Bar().f(6) |
| 699 |
instance_mock.Bar().f.assert_called_with(6) |
| 700 |
instance_mock.Bar().f.assert_called_with(y=6) |
| 701 |
self.assertRaises(AttributeError, getattr, instance_mock.Bar(), 'g') |
| 702 |
|
| 703 |
|
| 704 |
def test_inherit(self): |
| 705 |
class Foo(object): |
| 706 |
a = 3 |
| 707 |
|
| 708 |
Foo.Foo = Foo |
| 709 |
|
| 710 |
# class |
| 711 |
mock = create_autospec(Foo) |
| 712 |
instance = mock() |
| 713 |
self.assertRaises(AttributeError, getattr, instance, 'b') |
| 714 |
|
| 715 |
attr_instance = mock.Foo() |
| 716 |
self.assertRaises(AttributeError, getattr, attr_instance, 'b') |
| 717 |
|
| 718 |
# instance |
| 719 |
mock = create_autospec(Foo()) |
| 720 |
self.assertRaises(AttributeError, getattr, mock, 'b') |
| 721 |
self.assertRaises(TypeError, mock) |
| 722 |
|
| 723 |
# attribute instance |
| 724 |
call_result = mock.Foo() |
| 725 |
self.assertRaises(AttributeError, getattr, call_result, 'b') |
| 726 |
|
| 727 |
|
| 728 |
def test_builtins(self): |
| 729 |
# used to fail with infinite recursion |
| 730 |
create_autospec(1) |
| 731 |
|
| 732 |
create_autospec(int) |
| 733 |
create_autospec('foo') |
| 734 |
create_autospec(str) |
| 735 |
create_autospec({}) |
| 736 |
create_autospec(dict) |
| 737 |
create_autospec([]) |
| 738 |
create_autospec(list) |
| 739 |
create_autospec(set()) |
| 740 |
create_autospec(set) |
| 741 |
create_autospec(1.0) |
| 742 |
create_autospec(float) |
| 743 |
create_autospec(1j) |
| 744 |
create_autospec(complex) |
| 745 |
create_autospec(False) |
| 746 |
create_autospec(True) |
| 747 |
|
| 748 |
|
| 749 |
def test_function(self): |
| 750 |
def f(a, b): pass |
| 751 |
|
| 752 |
mock = create_autospec(f) |
| 753 |
self.assertRaises(TypeError, mock) |
| 754 |
mock(1, 2) |
| 755 |
mock.assert_called_with(1, 2) |
| 756 |
mock.assert_called_with(1, b=2) |
| 757 |
mock.assert_called_with(a=1, b=2) |
| 758 |
|
| 759 |
f.f = f |
| 760 |
mock = create_autospec(f) |
| 761 |
self.assertRaises(TypeError, mock.f) |
| 762 |
mock.f(3, 4) |
| 763 |
mock.f.assert_called_with(3, 4) |
| 764 |
mock.f.assert_called_with(a=3, b=4) |
| 765 |
|
| 766 |
|
| 767 |
def test_skip_attributeerrors(self): |
| 768 |
class Raiser(object): |
| 769 |
def __get__(self, obj, type=None): |
| 770 |
if obj is None: |
| 771 |
raise AttributeError('Can only be accessed via an instance') |
| 772 |
|
| 773 |
class RaiserClass(object): |
| 774 |
raiser = Raiser() |
| 775 |
|
| 776 |
@staticmethod |
| 777 |
def existing(a, b): |
| 778 |
return a + b |
| 779 |
|
| 780 |
self.assertEqual(RaiserClass.existing(1, 2), 3) |
| 781 |
s = create_autospec(RaiserClass) |
| 782 |
self.assertRaises(TypeError, lambda x: s.existing(1, 2, 3)) |
| 783 |
self.assertEqual(s.existing(1, 2), s.existing.return_value) |
| 784 |
self.assertRaises(AttributeError, lambda: s.nonexisting) |
| 785 |
|
| 786 |
# check we can fetch the raiser attribute and it has no spec |
| 787 |
obj = s.raiser |
| 788 |
obj.foo, obj.bar |
| 789 |
|
| 790 |
|
| 791 |
def test_signature_class(self): |
| 792 |
class Foo(object): |
| 793 |
def __init__(self, a, b=3): pass |
| 794 |
|
| 795 |
mock = create_autospec(Foo) |
| 796 |
|
| 797 |
self.assertRaises(TypeError, mock) |
| 798 |
mock(1) |
| 799 |
mock.assert_called_once_with(1) |
| 800 |
mock.assert_called_once_with(a=1) |
| 801 |
self.assertRaises(AssertionError, mock.assert_called_once_with, 2) |
| 802 |
|
| 803 |
mock(4, 5) |
| 804 |
mock.assert_called_with(4, 5) |
| 805 |
mock.assert_called_with(a=4, b=5) |
| 806 |
self.assertRaises(AssertionError, mock.assert_called_with, a=5, b=4) |
| 807 |
|
| 808 |
|
| 809 |
def test_class_with_no_init(self): |
| 810 |
# this used to raise an exception |
| 811 |
# due to trying to get a signature from object.__init__ |
| 812 |
class Foo(object): |
| 813 |
pass |
| 814 |
create_autospec(Foo) |
| 815 |
|
| 816 |
|
| 817 |
def test_signature_callable(self): |
| 818 |
class Callable(object): |
| 819 |
def __init__(self, x, y): pass |
| 820 |
def __call__(self, a): pass |
| 821 |
|
| 822 |
mock = create_autospec(Callable) |
| 823 |
mock(1, 2) |
| 824 |
mock.assert_called_once_with(1, 2) |
| 825 |
mock.assert_called_once_with(x=1, y=2) |
| 826 |
self.assertRaises(TypeError, mock, 'a') |
| 827 |
|
| 828 |
instance = mock(1, 2) |
| 829 |
self.assertRaises(TypeError, instance) |
| 830 |
instance(a='a') |
| 831 |
instance.assert_called_once_with('a') |
| 832 |
instance.assert_called_once_with(a='a') |
| 833 |
instance('a') |
| 834 |
instance.assert_called_with('a') |
| 835 |
instance.assert_called_with(a='a') |
| 836 |
|
| 837 |
mock = create_autospec(Callable(1, 2)) |
| 838 |
mock(a='a') |
| 839 |
mock.assert_called_once_with(a='a') |
| 840 |
self.assertRaises(TypeError, mock) |
| 841 |
mock('a') |
| 842 |
mock.assert_called_with('a') |
| 843 |
|
| 844 |
|
| 845 |
def test_signature_noncallable(self): |
| 846 |
class NonCallable(object): |
| 847 |
def __init__(self): |
| 848 |
pass |
| 849 |
|
| 850 |
mock = create_autospec(NonCallable) |
| 851 |
instance = mock() |
| 852 |
mock.assert_called_once_with() |
| 853 |
self.assertRaises(TypeError, mock, 'a') |
| 854 |
self.assertRaises(TypeError, instance) |
| 855 |
self.assertRaises(TypeError, instance, 'a') |
| 856 |
|
| 857 |
mock = create_autospec(NonCallable()) |
| 858 |
self.assertRaises(TypeError, mock) |
| 859 |
self.assertRaises(TypeError, mock, 'a') |
| 860 |
|
| 861 |
|
| 862 |
def test_create_autospec_none(self): |
| 863 |
class Foo(object): |
| 864 |
bar = None |
| 865 |
|
| 866 |
mock = create_autospec(Foo) |
| 867 |
none = mock.bar |
| 868 |
self.assertNotIsInstance(none, type(None)) |
| 869 |
|
| 870 |
none.foo() |
| 871 |
none.foo.assert_called_once_with() |
| 872 |
|
| 873 |
|
| 874 |
def test_autospec_functions_with_self_in_odd_place(self): |
| 875 |
class Foo(object): |
| 876 |
def f(a, self): pass |
| 877 |
|
| 878 |
a = create_autospec(Foo) |
| 879 |
a.f(10) |
| 880 |
a.f.assert_called_with(10) |
| 881 |
a.f.assert_called_with(self=10) |
| 882 |
a.f(self=10) |
| 883 |
a.f.assert_called_with(10) |
| 884 |
a.f.assert_called_with(self=10) |
| 885 |
|
| 886 |
|
| 887 |
def test_autospec_data_descriptor(self): |
| 888 |
class Descriptor(object): |
| 889 |
def __init__(self, value): |
| 890 |
self.value = value |
| 891 |
|
| 892 |
def __get__(self, obj, cls=None): |
| 893 |
return self |
| 894 |
|
| 895 |
def __set__(self, obj, value): pass |
| 896 |
|
| 897 |
class MyProperty(property): |
| 898 |
pass |
| 899 |
|
| 900 |
class Foo(object): |
| 901 |
__slots__ = ['slot'] |
| 902 |
|
| 903 |
@property |
| 904 |
def prop(self): pass |
| 905 |
|
| 906 |
@MyProperty |
| 907 |
def subprop(self): pass |
| 908 |
|
| 909 |
desc = Descriptor(42) |
| 910 |
|
| 911 |
foo = create_autospec(Foo) |
| 912 |
|
| 913 |
def check_data_descriptor(mock_attr): |
| 914 |
# Data descriptors don't have a spec. |
| 915 |
self.assertIsInstance(mock_attr, MagicMock) |
| 916 |
mock_attr(1, 2, 3) |
| 917 |
mock_attr.abc(4, 5, 6) |
| 918 |
mock_attr.assert_called_once_with(1, 2, 3) |
| 919 |
mock_attr.abc.assert_called_once_with(4, 5, 6) |
| 920 |
|
| 921 |
# property |
| 922 |
check_data_descriptor(foo.prop) |
| 923 |
# property subclass |
| 924 |
check_data_descriptor(foo.subprop) |
| 925 |
# class __slot__ |
| 926 |
check_data_descriptor(foo.slot) |
| 927 |
# plain data descriptor |
| 928 |
check_data_descriptor(foo.desc) |
| 929 |
|
| 930 |
|
| 931 |
def test_autospec_on_bound_builtin_function(self): |
| 932 |
meth = types.MethodType(time.ctime, time.time()) |
| 933 |
self.assertIsInstance(meth(), str) |
| 934 |
mocked = create_autospec(meth) |
| 935 |
|
| 936 |
# no signature, so no spec to check against |
| 937 |
mocked() |
| 938 |
mocked.assert_called_once_with() |
| 939 |
mocked.reset_mock() |
| 940 |
mocked(4, 5, 6) |
| 941 |
mocked.assert_called_once_with(4, 5, 6) |
| 942 |
|
| 943 |
|
| 944 |
def test_autospec_getattr_partial_function(self): |
| 945 |
# bpo-32153 : getattr returning partial functions without |
| 946 |
# __name__ should not create AttributeError in create_autospec |
| 947 |
class Foo: |
| 948 |
|
| 949 |
def __getattr__(self, attribute): |
| 950 |
return partial(lambda name: name, attribute) |
| 951 |
|
| 952 |
proxy = Foo() |
| 953 |
autospec = create_autospec(proxy) |
| 954 |
self.assertNotHasAttr(autospec, '__name__') |
| 955 |
|
| 956 |
|
| 957 |
def test_autospec_signature_staticmethod(self): |
| 958 |
class Foo: |
| 959 |
@staticmethod |
| 960 |
def static_method(a, b=10, *, c): pass |
| 961 |
|
| 962 |
mock = create_autospec(Foo.__dict__['static_method']) |
| 963 |
self.assertEqual(inspect.signature(Foo.static_method), inspect.signature(mock)) |
| 964 |
|
| 965 |
|
| 966 |
def test_autospec_signature_classmethod(self): |
| 967 |
class Foo: |
| 968 |
@classmethod |
| 969 |
def class_method(cls, a, b=10, *, c): pass |
| 970 |
|
| 971 |
mock = create_autospec(Foo.__dict__['class_method']) |
| 972 |
self.assertEqual(inspect.signature(Foo.class_method), inspect.signature(mock)) |
| 973 |
|
| 974 |
|
| 975 |
def test_spec_inspect_signature(self): |
| 976 |
|
| 977 |
def myfunc(x, y): pass |
| 978 |
|
| 979 |
mock = create_autospec(myfunc) |
| 980 |
mock(1, 2) |
| 981 |
mock(x=1, y=2) |
| 982 |
|
| 983 |
self.assertEqual(inspect.signature(mock), inspect.signature(myfunc)) |
| 984 |
self.assertEqual(mock.mock_calls, [call(1, 2), call(x=1, y=2)]) |
| 985 |
self.assertRaises(TypeError, mock, 1) |
| 986 |
|
| 987 |
|
| 988 |
def test_spec_inspect_signature_annotations(self): |
| 989 |
|
| 990 |
def foo(a: int, b: int=10, *, c:int) -> int: |
| 991 |
return a + b + c |
| 992 |
|
| 993 |
self.assertEqual(foo(1, 2 , c=3), 6) |
| 994 |
mock = create_autospec(foo) |
| 995 |
mock(1, 2, c=3) |
| 996 |
mock(1, c=3) |
| 997 |
|
| 998 |
self.assertEqual(inspect.signature(mock), inspect.signature(foo)) |
| 999 |
self.assertEqual(mock.mock_calls, [call(1, 2, c=3), call(1, c=3)]) |
| 1000 |
self.assertRaises(TypeError, mock, 1) |
| 1001 |
self.assertRaises(TypeError, mock, 1, 2, 3, c=4) |
| 1002 |
|
| 1003 |
|
| 1004 |
def test_spec_function_no_name(self): |
| 1005 |
func = lambda: 'nope' |
| 1006 |
mock = create_autospec(func) |
| 1007 |
self.assertEqual(mock.__name__, 'funcopy') |
| 1008 |
|
| 1009 |
|
| 1010 |
def test_spec_function_assert_has_calls(self): |
| 1011 |
def f(a): pass |
| 1012 |
mock = create_autospec(f) |
| 1013 |
mock(1) |
| 1014 |
mock.assert_has_calls([call(1)]) |
| 1015 |
with self.assertRaises(AssertionError): |
| 1016 |
mock.assert_has_calls([call(2)]) |
| 1017 |
|
| 1018 |
|
| 1019 |
def test_spec_function_assert_any_call(self): |
| 1020 |
def f(a): pass |
| 1021 |
mock = create_autospec(f) |
| 1022 |
mock(1) |
| 1023 |
mock.assert_any_call(1) |
| 1024 |
with self.assertRaises(AssertionError): |
| 1025 |
mock.assert_any_call(2) |
| 1026 |
|
| 1027 |
|
| 1028 |
def test_spec_function_reset_mock(self): |
| 1029 |
def f(a): pass |
| 1030 |
rv = Mock() |
| 1031 |
mock = create_autospec(f, return_value=rv) |
| 1032 |
mock(1)(2) |
| 1033 |
self.assertEqual(mock.mock_calls, [call(1)]) |
| 1034 |
self.assertEqual(rv.mock_calls, [call(2)]) |
| 1035 |
mock.reset_mock() |
| 1036 |
self.assertEqual(mock.mock_calls, []) |
| 1037 |
self.assertEqual(rv.mock_calls, []) |
| 1038 |
|
| 1039 |
def test_dataclass_post_init(self): |
| 1040 |
@dataclass |
| 1041 |
class WithPostInit: |
| 1042 |
a: int = field(init=False) |
| 1043 |
b: int = field(init=False) |
| 1044 |
def __post_init__(self): |
| 1045 |
self.a = 1 |
| 1046 |
self.b = 2 |
| 1047 |
|
| 1048 |
for mock in [ |
| 1049 |
create_autospec(WithPostInit, instance=True), |
| 1050 |
create_autospec(WithPostInit()), |
| 1051 |
]: |
| 1052 |
with self.subTest(mock=mock): |
| 1053 |
self.assertIsInstance(mock, WithPostInit) |
| 1054 |
self.assertIsInstance(mock.a, int) |
| 1055 |
self.assertIsInstance(mock.b, int) |
| 1056 |
|
| 1057 |
# Classes do not have these fields: |
| 1058 |
mock = create_autospec(WithPostInit) |
| 1059 |
msg = "Mock object has no attribute" |
| 1060 |
with self.assertRaisesRegex(AttributeError, msg): |
| 1061 |
mock.a |
| 1062 |
with self.assertRaisesRegex(AttributeError, msg): |
| 1063 |
mock.b |
| 1064 |
|
| 1065 |
def test_dataclass_default(self): |
| 1066 |
@dataclass |
| 1067 |
class WithDefault: |
| 1068 |
a: int |
| 1069 |
b: int = 0 |
| 1070 |
|
| 1071 |
for mock in [ |
| 1072 |
create_autospec(WithDefault, instance=True), |
| 1073 |
create_autospec(WithDefault(1)), |
| 1074 |
]: |
| 1075 |
with self.subTest(mock=mock): |
| 1076 |
self.assertIsInstance(mock, WithDefault) |
| 1077 |
self.assertIsInstance(mock.a, int) |
| 1078 |
self.assertIsInstance(mock.b, int) |
| 1079 |
|
| 1080 |
def test_dataclass_with_method(self): |
| 1081 |
@dataclass |
| 1082 |
class WithMethod: |
| 1083 |
a: int |
| 1084 |
def b(self) -> int: |
| 1085 |
return 1 # pragma: no cover |
| 1086 |
|
| 1087 |
for mock in [ |
| 1088 |
create_autospec(WithMethod, instance=True), |
| 1089 |
create_autospec(WithMethod(1)), |
| 1090 |
]: |
| 1091 |
with self.subTest(mock=mock): |
| 1092 |
self.assertIsInstance(mock, WithMethod) |
| 1093 |
self.assertIsInstance(mock.a, int) |
| 1094 |
mock.b.assert_not_called() |
| 1095 |
|
| 1096 |
def test_dataclass_with_non_fields(self): |
| 1097 |
@dataclass |
| 1098 |
class WithNonFields: |
| 1099 |
a: ClassVar[int] |
| 1100 |
b: InitVar[int] |
| 1101 |
|
| 1102 |
msg = "Mock object has no attribute" |
| 1103 |
for mock in [ |
| 1104 |
create_autospec(WithNonFields, instance=True), |
| 1105 |
create_autospec(WithNonFields(1)), |
| 1106 |
]: |
| 1107 |
with self.subTest(mock=mock): |
| 1108 |
self.assertIsInstance(mock, WithNonFields) |
| 1109 |
with self.assertRaisesRegex(AttributeError, msg): |
| 1110 |
mock.a |
| 1111 |
with self.assertRaisesRegex(AttributeError, msg): |
| 1112 |
mock.b |
| 1113 |
|
| 1114 |
def test_dataclass_special_attrs(self): |
| 1115 |
@dataclass |
| 1116 |
class Description: |
| 1117 |
name: str |
| 1118 |
|
| 1119 |
for mock in [ |
| 1120 |
create_autospec(Description, instance=True), |
| 1121 |
create_autospec(Description(1)), |
| 1122 |
]: |
| 1123 |
with self.subTest(mock=mock): |
| 1124 |
self.assertIsInstance(mock, Description) |
| 1125 |
self.assertIs(mock.__class__, Description) |
| 1126 |
self.assertIsInstance(mock.__dataclass_fields__, MagicMock) |
| 1127 |
self.assertIsInstance(mock.__dataclass_params__, MagicMock) |
| 1128 |
self.assertIsInstance(mock.__match_args__, MagicMock) |
| 1129 |
self.assertIsInstance(mock.__hash__, MagicMock) |
| 1130 |
|
| 1131 |
class TestCallList(unittest.TestCase): |
| 1132 |
|
| 1133 |
def test_args_list_contains_call_list(self): |
| 1134 |
mock = Mock() |
| 1135 |
self.assertIsInstance(mock.call_args_list, _CallList) |
| 1136 |
|
| 1137 |
mock(1, 2) |
| 1138 |
mock(a=3) |
| 1139 |
mock(3, 4) |
| 1140 |
mock(b=6) |
| 1141 |
|
| 1142 |
for kall in call(1, 2), call(a=3), call(3, 4), call(b=6): |
| 1143 |
self.assertIn(kall, mock.call_args_list) |
| 1144 |
|
| 1145 |
calls = [call(a=3), call(3, 4)] |
| 1146 |
self.assertIn(calls, mock.call_args_list) |
| 1147 |
calls = [call(1, 2), call(a=3)] |
| 1148 |
self.assertIn(calls, mock.call_args_list) |
| 1149 |
calls = [call(3, 4), call(b=6)] |
| 1150 |
self.assertIn(calls, mock.call_args_list) |
| 1151 |
calls = [call(3, 4)] |
| 1152 |
self.assertIn(calls, mock.call_args_list) |
| 1153 |
|
| 1154 |
self.assertNotIn(call('fish'), mock.call_args_list) |
| 1155 |
self.assertNotIn([call('fish')], mock.call_args_list) |
| 1156 |
|
| 1157 |
|
| 1158 |
def test_call_list_str(self): |
| 1159 |
mock = Mock() |
| 1160 |
mock(1, 2) |
| 1161 |
mock.foo(a=3) |
| 1162 |
mock.foo.bar().baz('fish', cat='dog') |
| 1163 |
|
| 1164 |
expected = ( |
| 1165 |
"[call(1, 2),\n" |
| 1166 |
" call.foo(a=3),\n" |
| 1167 |
" call.foo.bar(),\n" |
| 1168 |
" call.foo.bar().baz('fish', cat='dog')]" |
| 1169 |
) |
| 1170 |
self.assertEqual(str(mock.mock_calls), expected) |
| 1171 |
|
| 1172 |
|
| 1173 |
def test_propertymock(self): |
| 1174 |
p = patch('%s.SomeClass.one' % __name__, new_callable=PropertyMock) |
| 1175 |
mock = p.start() |
| 1176 |
try: |
| 1177 |
SomeClass.one |
| 1178 |
mock.assert_called_once_with() |
| 1179 |
|
| 1180 |
s = SomeClass() |
| 1181 |
s.one |
| 1182 |
mock.assert_called_with() |
| 1183 |
self.assertEqual(mock.mock_calls, [call(), call()]) |
| 1184 |
|
| 1185 |
s.one = 3 |
| 1186 |
self.assertEqual(mock.mock_calls, [call(), call(), call(3)]) |
| 1187 |
finally: |
| 1188 |
p.stop() |
| 1189 |
|
| 1190 |
|
| 1191 |
def test_propertymock_bare(self): |
| 1192 |
m = MagicMock() |
| 1193 |
p = PropertyMock() |
| 1194 |
type(m).foo = p |
| 1195 |
|
| 1196 |
returned = m.foo |
| 1197 |
p.assert_called_once_with() |
| 1198 |
self.assertIsInstance(returned, MagicMock) |
| 1199 |
self.assertNotIsInstance(returned, PropertyMock) |
| 1200 |
|
| 1201 |
|
| 1202 |
def test_propertymock_returnvalue(self): |
| 1203 |
m = MagicMock() |
| 1204 |
p = PropertyMock(return_value=42) |
| 1205 |
type(m).foo = p |
| 1206 |
|
| 1207 |
returned = m.foo |
| 1208 |
p.assert_called_once_with() |
| 1209 |
self.assertEqual(returned, 42) |
| 1210 |
self.assertNotIsInstance(returned, PropertyMock) |
| 1211 |
|
| 1212 |
|
| 1213 |
def test_propertymock_side_effect(self): |
| 1214 |
m = MagicMock() |
| 1215 |
p = PropertyMock(side_effect=ValueError) |
| 1216 |
type(m).foo = p |
| 1217 |
|
| 1218 |
with self.assertRaises(ValueError): |
| 1219 |
m.foo |
| 1220 |
p.assert_called_once_with() |
| 1221 |
|
| 1222 |
|
| 1223 |
def test_propertymock_attach(self): |
| 1224 |
m = Mock() |
| 1225 |
p = PropertyMock() |
| 1226 |
type(m).foo = p |
| 1227 |
m.attach_mock(p, 'foo') |
| 1228 |
self.assertEqual(m.mock_calls, []) |
| 1229 |
|
| 1230 |
|
| 1231 |
class TestCallablePredicate(unittest.TestCase): |
| 1232 |
|
| 1233 |
def test_type(self): |
| 1234 |
for obj in [str, bytes, int, list, tuple, SomeClass]: |
| 1235 |
self.assertTrue(_callable(obj)) |
| 1236 |
|
| 1237 |
def test_call_magic_method(self): |
| 1238 |
class Callable: |
| 1239 |
def __call__(self): pass |
| 1240 |
instance = Callable() |
| 1241 |
self.assertTrue(_callable(instance)) |
| 1242 |
|
| 1243 |
def test_staticmethod(self): |
| 1244 |
class WithStaticMethod: |
| 1245 |
@staticmethod |
| 1246 |
def staticfunc(): pass |
| 1247 |
self.assertTrue(_callable(WithStaticMethod.staticfunc)) |
| 1248 |
|
| 1249 |
def test_non_callable_staticmethod(self): |
| 1250 |
class BadStaticMethod: |
| 1251 |
not_callable = staticmethod(None) |
| 1252 |
self.assertFalse(_callable(BadStaticMethod.not_callable)) |
| 1253 |
|
| 1254 |
def test_classmethod(self): |
| 1255 |
class WithClassMethod: |
| 1256 |
@classmethod |
| 1257 |
def classfunc(cls): pass |
| 1258 |
self.assertTrue(_callable(WithClassMethod.classfunc)) |
| 1259 |
|
| 1260 |
def test_non_callable_classmethod(self): |
| 1261 |
class BadClassMethod: |
| 1262 |
not_callable = classmethod(None) |
| 1263 |
self.assertFalse(_callable(BadClassMethod.not_callable)) |
| 1264 |
|
| 1265 |
|
| 1266 |
if __name__ == '__main__': |
| 1267 |
unittest.main() |