Skip to content

Commit 24b0e8d

Browse files
miss-islingtonshaunagmwillingc
authored
[3.13] gh-118912: Remove description of issue fixed in 3.5 from autospeccing guide (GH-119232) (#119284)
gh-118912: Remove description of issue fixed in 3.5 from autospeccing guide (GH-119232) * Remove description of issue fixed in 3.5 from autospeccing guide * Make autospeccing note text more succint and lint whitespace * Add linting changes (missed in last commit) --------- (cherry picked from commit 7e57640) Co-authored-by: Shauna <shaunagm@gmail.com> Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
1 parent d8fbe5b commit 24b0e8d

File tree

1 file changed

+8
-32
lines changed

1 file changed

+8
-32
lines changed

Doc/library/unittest.mock.rst

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,40 +2584,16 @@ called incorrectly.
25842584

25852585
Before I explain how auto-speccing works, here's why it is needed.
25862586

2587-
:class:`Mock` is a very powerful and flexible object, but it suffers from two flaws
2588-
when used to mock out objects from a system under test. One of these flaws is
2589-
specific to the :class:`Mock` api and the other is a more general problem with using
2590-
mock objects.
2591-
2592-
First the problem specific to :class:`Mock`. :class:`Mock` has two assert methods that are
2593-
extremely handy: :meth:`~Mock.assert_called_with` and
2594-
:meth:`~Mock.assert_called_once_with`.
2595-
2596-
>>> mock = Mock(name='Thing', return_value=None)
2597-
>>> mock(1, 2, 3)
2598-
>>> mock.assert_called_once_with(1, 2, 3)
2599-
>>> mock(1, 2, 3)
2600-
>>> mock.assert_called_once_with(1, 2, 3)
2601-
Traceback (most recent call last):
2602-
...
2603-
AssertionError: Expected 'mock' to be called once. Called 2 times.
2604-
2605-
Because mocks auto-create attributes on demand, and allow you to call them
2606-
with arbitrary arguments, if you misspell one of these assert methods then
2607-
your assertion is gone:
2608-
2609-
.. code-block:: pycon
2610-
2611-
>>> mock = Mock(name='Thing', return_value=None)
2612-
>>> mock(1, 2, 3)
2613-
>>> mock.assret_called_once_with(4, 5, 6) # Intentional typo!
2587+
:class:`Mock` is a very powerful and flexible object, but it suffers from a flaw which
2588+
is general to mocking. If you refactor some of your code, rename members and so on, any
2589+
tests for code that is still using the *old api* but uses mocks instead of the real
2590+
objects will still pass. This means your tests can all pass even though your code is
2591+
broken.
26142592

2615-
Your tests can pass silently and incorrectly because of the typo.
2593+
.. versionchanged:: 3.5
26162594

2617-
The second issue is more general to mocking. If you refactor some of your
2618-
code, rename members and so on, any tests for code that is still using the
2619-
*old api* but uses mocks instead of the real objects will still pass. This
2620-
means your tests can all pass even though your code is broken.
2595+
Before 3.5, tests with a typo in the word assert would silently pass when they should
2596+
raise an error. You can still achieve this behavior by passing ``unsafe=True`` to Mock.
26212597

26222598
Note that this is another reason why you need integration tests as well as
26232599
unit tests. Testing everything in isolation is all fine and dandy, but if you

0 commit comments

Comments
 (0)