Python Forum
Using Mock with python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Mock with python
#1
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
Larz60+ write Oct-10-2021, 08:06 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts,
Reply


Messages In This Thread
Using Mock with python - by Hocinema - Oct-10-2021, 12:17 AM
RE: Using Mock with python - by ndc85430 - Oct-10-2021, 12:01 PM
RE: Using Mock with python - by Hocinema - Oct-10-2021, 10:05 PM
RE: Using Mock with python - by Hocinema - Oct-11-2021, 12:33 PM
RE: Using Mock with python - by Hocinema - Oct-11-2021, 09:09 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020