Open matahho opened 3 months ago
This is because a method decorator is applied before a namespace that includes the method can be created for the class to be created, only after which the class decorator can be applied, so your method is effectively decorated like:
@mock.patch('module.get_value', return_value='class-level')
@mock.patch('module.get_value', return_value='method-level')
def test_method_level_mock(self, mock_get_value):
...
with a bottom-up order of application per the documentation.
Since the class decorator is applied last, the return value specified there overrides the one specified by the method decorator.
Bug report
Bug description:
Description
I encountered an issue where mocking a value at the method level does not override the class-level mock in unit tests. Even though the method-level mock should take precedence, the value from the class-level mock is still being used.
Steps to Reproduce
Create a test class with a class-level mock using @mock.patch. Add a method-level mock to one of the test methods to override the class-level mock. Run the tests and observe that the class-level mock value is still used instead of the method-level mock.
Expected Behavior
The method-level mock should override the class-level mock, and the value set in the method-level mock should be used in the test.
Actual Behavior
The class-level mock value is used, and the method-level mock is ignored.
Sample code
CPython versions tested on:
3.10
Operating systems tested on:
No response