Oct-10-2021, 10:05 PM
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)
Error:
Output:$ pytest test.py
================================================================================== test session starts ===================================================================================
platform darwin -- Python 3.8.2, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: /Users/hocinema/projets/scripts/python
plugins: hypothesis-5.43.3, anyio-2.0.2
collected 1 item
test.py F [100%]
======================================================================================== FAILURES ========================================================================================
_______________________________________________________________________________ FeatEngShould.test_mockmag _______________________________________________________________________________
self = <test.FeatEngShould testMethod=test_mockmag>
def test_mockmag(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)
E AssertionError: True is not false
test.py:16: AssertionError
================================================================================ short test summary info =================================================================================
FAILED test.py::FeatEngShould::test_mockmag - AssertionError: True is not false
=================================================================================== 1 failed in 0.21s ====================================================================================
I want to make the mock client returning false to make success the test but instead it failed