Skip to content

Commit 3d88145

Browse files
authored
gh-109350: Fix outdated captured output in unittest.mock documentation (#109353)
1 parent 92ed7e4 commit 3d88145

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Doc/library/unittest.mock-examples.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ instantiate the class in those tests.
339339
>>> mock.old_method()
340340
Traceback (most recent call last):
341341
...
342-
AttributeError: object has no attribute 'old_method'
342+
AttributeError: Mock object has no attribute 'old_method'. Did you mean: 'class_method'?
343343

344344
Using a specification also enables a smarter matching of calls made to the
345345
mock, regardless of whether some parameters were passed as positional or
@@ -798,7 +798,8 @@ If your mock is only being called once you can use the
798798
>>> mock.foo_bar.assert_called_once_with('baz', spam='eggs')
799799
Traceback (most recent call last):
800800
...
801-
AssertionError: Expected to be called once. Called 2 times.
801+
AssertionError: Expected 'foo_bar' to be called once. Called 2 times.
802+
Calls: [call('baz', spam='eggs'), call()].
802803

803804
Both ``assert_called_with`` and ``assert_called_once_with`` make assertions about
804805
the *most recent* call. If your mock is going to be called several times, and
@@ -927,8 +928,9 @@ Here's an example implementation:
927928
>>> c.assert_called_with(arg)
928929
Traceback (most recent call last):
929930
...
930-
AssertionError: Expected call: mock({1})
931-
Actual call: mock(set())
931+
AssertionError: expected call not found.
932+
Expected: mock({1})
933+
Actual: mock(set())
932934
>>> c.foo
933935
<CopyingMock name='mock.foo' id='...'>
934936

@@ -1292,8 +1294,9 @@ sufficient:
12921294
>>> mock.assert_called_with(Foo(1, 2))
12931295
Traceback (most recent call last):
12941296
...
1295-
AssertionError: Expected: call(<__main__.Foo object at 0x...>)
1296-
Actual call: call(<__main__.Foo object at 0x...>)
1297+
AssertionError: expected call not found.
1298+
Expected: mock(<__main__.Foo object at 0x...>)
1299+
Actual: mock(<__main__.Foo object at 0x...>)
12971300

12981301
A comparison function for our ``Foo`` class might look something like this:
12991302

Doc/library/unittest.mock.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ code if they are used incorrectly:
189189
>>> mock_function('wrong arguments')
190190
Traceback (most recent call last):
191191
...
192-
TypeError: <lambda>() takes exactly 3 arguments (1 given)
192+
TypeError: missing a required argument: 'b'
193193

194194
:func:`create_autospec` can also be used on classes, where it copies the signature of
195195
the ``__init__`` method, and on callable objects where it copies the signature of
@@ -315,6 +315,7 @@ the *new_callable* argument to :func:`patch`.
315315
Traceback (most recent call last):
316316
...
317317
AssertionError: Expected 'method' to have been called once. Called 2 times.
318+
Calls: [call(), call()].
318319

319320
.. versionadded:: 3.6
320321

@@ -342,7 +343,7 @@ the *new_callable* argument to :func:`patch`.
342343
Traceback (most recent call last):
343344
...
344345
AssertionError: Expected 'mock' to be called once. Called 2 times.
345-
346+
Calls: [call('foo', bar='baz'), call('other', bar='values')].
346347

347348
.. method:: assert_any_call(*args, **kwargs)
348349

@@ -392,6 +393,7 @@ the *new_callable* argument to :func:`patch`.
392393
Traceback (most recent call last):
393394
...
394395
AssertionError: Expected 'hello' to not have been called. Called 1 times.
396+
Calls: [call()].
395397

396398
.. versionadded:: 3.5
397399

@@ -954,7 +956,7 @@ object::
954956
>>> asyncio.run(main())
955957
>>> mock.assert_awaited_once()
956958
>>> asyncio.run(main())
957-
>>> mock.method.assert_awaited_once()
959+
>>> mock.assert_awaited_once()
958960
Traceback (most recent call last):
959961
...
960962
AssertionError: Expected mock to have been awaited once. Awaited 2 times.
@@ -972,7 +974,7 @@ object::
972974
>>> mock.assert_awaited_with('other')
973975
Traceback (most recent call last):
974976
...
975-
AssertionError: expected call not found.
977+
AssertionError: expected await not found.
976978
Expected: mock('other')
977979
Actual: mock('foo', bar='bar')
978980

0 commit comments

Comments
 (0)