2023-01-30 16:52:42 |
Sofia Enriquez |
description |
Cinder has some tests with assertion typo's or plain calls to Mock() or MagicMock() resulting in those tests not testing code correctly. One example of that is: https://review.opendev.org/c/openstack/cinder/+/831600
The refactored code would replace plain Mock() calls with Mock(spec) calls, which limits the API of mocks to the API of an original object (the spec). This ensures that any assertion typo's or incorrect calls will be caught by raising a TypeError. This makes it easier to identify and fix any errors in the tests, ensuring that they are testing code correctly.
For example:
diff --git a/cinder/tests/unit/volume/test_volume_manager.py b/cinder/tests/unit/volume/test_volume_manager.py
index 9a7a50ff6..5666f7442 100644
--- a/cinder/tests/unit/volume/test_volume_manager.py
+++ b/cinder/tests/unit/volume/test_volume_manager.py
@@ -20,6 +20,7 @@ import ddt
from cinder.common import constants
from cinder import exception
+ from cinder import context
from cinder.message import message_field
from cinder.tests.unit import fake_constants as fake
from cinder.tests.unit import fake_volume
@@ -231,7 +232,7 @@ class VolumeManagerTestCase(base.BaseVolumeTestCase):
'volume_encryption_metadata_get')
mock_attach_encryptor.side_effect = ValueError
- ctxt = mock.Mock()
+ ctxt = mock.Mock(spec=context.RequestContext)
vol = fake_volume.fake_volume_obj(ctxt)
The refactored code replaces plain calls to Mock() with Mock(spec) calls, which specify the object class that the mock should be based on (in this case, context.RequestContext). This allows the tests to more accurately simulate how the code will behave when it is run in production, as it will be using objects of those classes rather than generic mocks that don't have any of the methods or attributes of those classes available to them.
Documentation unittest.mock#autospeccing: https://docs.python.org/3/library/unittest.mock.html#autospeccing |
Cinder has some tests with assertion typo's or plain calls to Mock() or MagicMock() resulting in those tests not testing code correctly. One example of that is: https://review.opendev.org/c/openstack/cinder/+/831600
The refactored code would replace plain Mock() calls with Mock(spec) calls
or you can either pass autospec=True to patch() / patch.object() or use the create_autospec() function to create a mock with a spec. We want stronger mocks by using specs, do the right thing where needed to get this.
Using autospec/spec limits the API of mocks to the API of an original object (the spec). This ensures that any assertion typo's or incorrect calls will be caught by raising a TypeError. This makes it easier to identify and fix any errors in the tests, ensuring that they are testing code correctly.
Mock(spec) example:
diff --git a/cinder/tests/unit/volume/test_volume_manager.py b/cinder/tests/unit/volume/test_volume_manager.py
index 9a7a50ff6..5666f7442 100644
--- a/cinder/tests/unit/volume/test_volume_manager.py
+++ b/cinder/tests/unit/volume/test_volume_manager.py
@@ -20,6 +20,7 @@ import ddt
from cinder.common import constants
from cinder import exception
+ from cinder import context
from cinder.message import message_field
from cinder.tests.unit import fake_constants as fake
from cinder.tests.unit import fake_volume
@@ -231,7 +232,7 @@ class VolumeManagerTestCase(base.BaseVolumeTestCase):
'volume_encryption_metadata_get')
mock_attach_encryptor.side_effect = ValueError
- ctxt = mock.Mock()
+ ctxt = mock.Mock(spec=context.RequestContext)
vol = fake_volume.fake_volume_obj(ctxt)
The refactored code replaces plain calls to Mock() with Mock(spec) calls, which specify the object class that the mock should be based on (in this case, context.RequestContext). This allows the tests to more accurately simulate how the code will behave when it is run in production, as it will be using objects of those classes rather than generic mocks that don't have any of the methods or attributes of those classes available to them.
Documentation unittest.mock#autospeccing: https://docs.python.org/3/library/unittest.mock.html#autospeccing |
|