[Python] python unit test mocking - dynamically evaluating instance variable

Discussão em 'Python' iniciado por Stack, Setembro 23, 2024.

  1. Stack

    Stack Membro Participativo

    I have a python file with instance variables that are getting assigned at run time, as below; now the challenge is that I need to mock this instance variable's external library method call "client.x". I tried patch

    Sample Code below


    import os
    from unittest.mock import patch

    import pytest
    @pytest.fixture()
    def setup():
    os.environ["SOME_REGION"] = "abc"
    os.environ["GIT_CREDS"] = "GIT_CREDS"
    os.environ["GIT_ORG"] = "some-org"
    os.environ["GIT_REPO"] = 'some-repo'
    os.environ['GIT_ENDPOINT'] = 'https://dummy'

    @pytest.fixture
    def util(setup):
    from abc import GitCommons
    return GitCommons()


    @patch("client.get_secret_value")
    def test_read_template_from_git(util, setup):
    util.read_template_from_git("test", "test")


    The below is instance code from file that is under test

    session = boto3.session.Session()
    client = session.client(
    service_name='someValue',
    region_name=SOME_REGION
    )
    get_secret_value_response = client.get_secret_value(
    SecretId=SOME_ID
    )
    git_token = get_secret_value_response['someString']


    Please guide me if any of you have experience in similar mocking. Please let me know if you need any more details.

    I tried @patch("client.get_secret_value")

    I am getting error as " ModuleNotFoundError: No module named 'client'"

    Continue reading...

Compartilhe esta Página