![]() |
Using Mock with python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code Review (https://python-forum.io/forum-46.html) +--- Thread: Using Mock with python (/thread-35216.html) |
Using Mock with python - Hocinema - Oct-10-2021 Hello, I need help to mock some Class/variable in python test. Here is the scenario: app_client.py class AppClient: def __init__(self): self.val = None def get_value(self, feature_eng): .................................................... app_client_instance.py from app_client import AppClient app_client_instance = AppClient() .................................................... from app_client_instance import app_client_instance class FeatEng: @staticmethod def is_activ(feature_eng): client = app_client_instance value = client.get_value(feature_eng) return "true" == value .................................................... unit test with magicMock def test_mockmag(self): client = MagicMock(spec=AppClient, name=AppClient) client.get_value.return_value = "false" instance = MagicMock(spec=app_client_instance) instance.app_client_instance.return_value = client result = FeatEng.is_activ("MIG_ENG")I have a static class FeatEng importing a variable app_client_instance, this last creates an object of the AppClient class. I want to mock app_client_instance and AppClient except FeatEng but it is not working. I don't know how to pass mocks to the FeatEng as no constructor on static class. Have you a way to help? Regards RE: Using Mock with python - ndc85430 - Oct-10-2021 You need to pass the instance in as a function parameter then. That's what dependency injection is - you take the responsibility of supplying the dependency out of the class that uses it, so that you can substitute different implementations as necessary (like here for testing). Also, do you really need to use MagicMock ? I mean, it's pretty much straightforward to define a class that has the right method and returns the value you want:class FakeClient: def get_value(self, feature_eng): return "true"Since Python has duck typing, you don't need to worry about inheriting from AppClient
RE: Using Mock with python - Hocinema - Oct-10-2021 app_client.py ------------- class AppClient: def __init__(self): self.val = "true" def get_value(self, feature_eng): return self.val .................................................... app_client_instance.py ---------------------- from app_client import AppClient app_client_instance = AppClient() .................................................... feat_eng.py ----------- from app_client_instance import app_client_instance class FeatEng: @staticmethod def is_activ(feature_eng): client = app_client_instance value = client.get_value(feature_eng) return "true" == value .................................................... test.py ------- import unittest from unittest.mock import MagicMock from app_client import AppClient from app_client_instance import app_client_instance from feat_eng import FeatEng class FeatEngShould(unittest.TestCase): def test_mockmagshouldFalse(self): client = MagicMock(spec=AppClient, name=AppClient) client.get_value.return_value = "false" instance = MagicMock() instance.app_client_instance.return_value = client result = FeatEng.is_activ("MIG_ENG") self.assertFalse(result)
I want to make the mock client returning false to make success the test but instead it failed
RE: Using Mock with python - Hocinema - Oct-11-2021 Anything else to implement magicMock with static class? RE: Using Mock with python - Hocinema - Oct-11-2021 I found the solution by only mocking the decorated static method. I also initialized the method variables with mocked values, et redefined the method returned value. |