Skip to content

Commit a625db4

Browse files
committedJun 27, 2024
tests: Adds mock patch autospec fixture
When mocking something, there's the possibility to reference a property that does not exist, or call a method with invalid arguments, but the unit tests to keep passing, resulting in false positives. We've seen this issue several times in OpenStack, and we have created and started using a mock patch fixture (can be seen in OpenStack Nova's test.py) which enables autospec by default. This guarantees 2 things: that the thing we're patching exists, and if it's callable, that the calls respect the callable's signature (e.g.: no unknown arguments can be used).
1 parent 329fde6 commit a625db4

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed
 

‎coriolisclient/tests/cli/test_diagnostics.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class GetCoriolisDiagnosticsTestCase(test_base.CoriolisBaseTestCase):
6060
def setUp(self, mock__init__):
6161
mock__init__.return_value = None
6262
super(GetCoriolisDiagnosticsTestCase, self).setUp()
63-
self.diag = diagnostics.GetCoriolisDiagnostics()
63+
self.diag = diagnostics.GetCoriolisDiagnostics(
64+
mock.sentinel.app, mock.sentinel.app_args)
6465

6566
@mock.patch.object(lister.Lister, 'get_parser')
6667
def test_get_parser(self, mock_get_parser):

‎coriolisclient/tests/test_base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
"""Defines base class for all tests."""
55

66
from oslotest import base
7+
from oslotest import mock_fixture
8+
9+
# NOTE(claudiub): this needs to be called before any mock.patch calls are
10+
# being done, and especially before any other test classes load. This fixes
11+
# the mock.patch autospec issue:
12+
# https://github.com/testing-cabal/mock/issues/396
13+
mock_fixture.patch_mock_module()
714

815

916
class CoriolisBaseTestCase(base.BaseTestCase):

0 commit comments

Comments
 (0)