0

I was checking this trying to assign a method call in a module-level variable with the intention of being executed only once, but not sure why before running any of my unit tests it will pass through all the global references of the module, the problem with this is that I have a third party method that I am assigning to a global variable and fails because is trying to execute the actual method in this first pass through, I see that this behaviour is the same even with a simple local method, here an example to replicate it, this is in a file called

project_name.app.py

def printing_values():
    # this is corrected mocked, as I am using patch in unit test to
    # mock this but only available in the context of the test but not
    # globally
    print('from mocked printing_values method', SSM_VALUE) 
    return SSM_VALUE


def get_ssm():
    return "value_from_method"

# this line will execute get_ssm before any unit test, 
# how mock this to always have a mock value
SSM_VALUE = get_ssm()

Here is my unit test

""" response_transformer TESTS """
import unittest
from unittest import mock

import project_name.app


class TestGlobalVariable(unittest.TestCase):

    @mock.patch('project_name.app.SSM_VALUE', 'testing_value')
    def test_success_response_global_variable(self):
        response = project_name.app.printing_values()
        assert response == "testing_value"

So I would like to mock SSM_VALUE but not executing the get_ssm method associated with it, how should I achieve this?

jam
  • 489
  • 1
  • 4
  • 19
  • You can't avoid loading the module, if you're going to test it. However, I have similar cases in django, settings.py, where I have to check if "test" in sys.argv: in order to do different things for a test run. Your check may be slightly different for other environments. – Kenny Ostrom May 24 '21 at 15:46
  • So for example an environment variable call TESTING and just place this behind the condition? – jam May 24 '21 at 15:48
  • That sounds consistent with the way I handled a similar issue in a different environment. Fundamentally, you're calling that initialization code during import, so you'll have to check it then, or change how it initializes itself. I'll try to make a better "answer" post later. – Kenny Ostrom May 24 '21 at 15:52

1 Answers1

0

""" response_transformer TESTS """
import unittest from unittest import mock
import project_name.app


class TestGlobalVariable(unittest.TestCase):

    @mock.patch('project_name.app.printing_values')
    def test_success_response_global_variable(self, mock_printing_values):
        mock_printing_values.return_value = 'testing_value'
        response = project_name.app.printing_values()
        assert response == "testing_value"
mnille
  • 1,328
  • 4
  • 16
  • 20
Pyro Fury
  • 5
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 03 '22 at 04:16