Mocker patch at rescue.
- 1 minThis mini blog would talk about using mocker patch from pytest to mock variables in object creation flow . This patching can be applied to a variable which is used somewhere deep in the codebase with no direct access for mock using conventional techniques. (Conventional techniques meant directly passing an altered/mocked variable).
A scenario:
For creation of app object we use config. This config object is created in the codeflow somewhere inaccessible for direct mocking eg.
- ServiceApp inherits BaseApp
- BaseApp uses Config class to populate self.config variable which is immediately consumed in self.consume_config function in init flow of app.
- Since config sits deep, it’s difficult to inject the mocked value directly at time of app object initialization.
Here comes, mocker.patch to rescue:
- Create a patch object
config_mock = mocker.patch("<complete_path_to_Config_class>")
- Fetch the method you selectively want to patch
get_config_path = config_mock.return_value.get_config
- Override the return type for this object
get_config_path.return_type = <patched_mocked_config_value>
- Now initialise your app object
app = ServiceApp()
The mocked config values would be used instead.
\cheers