From 09b25036c616c046a576b38f979e624c6343ea45 Mon Sep 17 00:00:00 2001 From: Shauna Gordon-McKeon Date: Mon, 20 May 2024 12:34:09 -0400 Subject: [PATCH 1/3] Remove description of issue fixed in 3.5 from autospeccing guide --- Doc/library/unittest.mock.rst | 41 ++++++++--------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index 9496c35f5267d0..afe345b9cba042 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -2584,40 +2584,17 @@ called incorrectly. Before I explain how auto-speccing works, here's why it is needed. -:class:`Mock` is a very powerful and flexible object, but it suffers from two flaws -when used to mock out objects from a system under test. One of these flaws is -specific to the :class:`Mock` api and the other is a more general problem with using -mock objects. - -First the problem specific to :class:`Mock`. :class:`Mock` has two assert methods that are -extremely handy: :meth:`~Mock.assert_called_with` and -:meth:`~Mock.assert_called_once_with`. - - >>> mock = Mock(name='Thing', return_value=None) - >>> mock(1, 2, 3) - >>> mock.assert_called_once_with(1, 2, 3) - >>> mock(1, 2, 3) - >>> mock.assert_called_once_with(1, 2, 3) - Traceback (most recent call last): - ... - AssertionError: Expected 'mock' to be called once. Called 2 times. - -Because mocks auto-create attributes on demand, and allow you to call them -with arbitrary arguments, if you misspell one of these assert methods then -your assertion is gone: - -.. code-block:: pycon - - >>> mock = Mock(name='Thing', return_value=None) - >>> mock(1, 2, 3) - >>> mock.assret_called_once_with(4, 5, 6) # Intentional typo! +:class:`Mock` is a very powerful and flexible object, but it suffers from a flaw which +is general to mocking. If you refactor some of your code, rename members and so on, any +tests for code that is still using the *old api* but uses mocks instead of the real +objects will still pass. This means your tests can all pass even though your code is +broken. -Your tests can pass silently and incorrectly because of the typo. +.. versionchanged:: 3.5 -The second issue is more general to mocking. If you refactor some of your -code, rename members and so on, any tests for code that is still using the -*old api* but uses mocks instead of the real objects will still pass. This -means your tests can all pass even though your code is broken. + Before 3.5, Mocks suffered from an additional flaw where tests with a typo in + the word assert would silently pass when they should raise an error. You can still + achieve this behavior by passing ``unsafe=True`` to the Mock class. Note that this is another reason why you need integration tests as well as unit tests. Testing everything in isolation is all fine and dandy, but if you From 71aabe890c0734ef3914df9b490024baf946b516 Mon Sep 17 00:00:00 2001 From: Shauna Gordon-McKeon Date: Mon, 20 May 2024 14:15:43 -0400 Subject: [PATCH 2/3] Make autospeccing note text more succint and lint whitespace --- Doc/library/unittest.mock.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index afe345b9cba042..5901a008ef3d6c 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -2592,9 +2592,8 @@ broken. .. versionchanged:: 3.5 - Before 3.5, Mocks suffered from an additional flaw where tests with a typo in - the word assert would silently pass when they should raise an error. You can still - achieve this behavior by passing ``unsafe=True`` to the Mock class. + Before 3.5, tests with a typo in the word assert would silently pass when they should + raise an error. You can still achieve this behavior by passing ``unsafe=True`` to Mock. Note that this is another reason why you need integration tests as well as unit tests. Testing everything in isolation is all fine and dandy, but if you From 05b3d500e2f24d5229007eba92204f0a8305eed6 Mon Sep 17 00:00:00 2001 From: Shauna Gordon-McKeon Date: Mon, 20 May 2024 14:26:28 -0400 Subject: [PATCH 3/3] Add linting changes (missed in last commit) --- Doc/library/unittest.mock.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index 5901a008ef3d6c..ae9092b454cba4 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -2585,12 +2585,12 @@ called incorrectly. Before I explain how auto-speccing works, here's why it is needed. :class:`Mock` is a very powerful and flexible object, but it suffers from a flaw which -is general to mocking. If you refactor some of your code, rename members and so on, any -tests for code that is still using the *old api* but uses mocks instead of the real -objects will still pass. This means your tests can all pass even though your code is +is general to mocking. If you refactor some of your code, rename members and so on, any +tests for code that is still using the *old api* but uses mocks instead of the real +objects will still pass. This means your tests can all pass even though your code is broken. -.. versionchanged:: 3.5 +.. versionchanged:: 3.5 Before 3.5, tests with a typo in the word assert would silently pass when they should raise an error. You can still achieve this behavior by passing ``unsafe=True`` to Mock.