title: How to mock a method call in a global variable
body: <p>I was checking <a href="https://stackoverflow.com/questions/38336090/mocking-a-global-variable">this</a> 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</p>
  <blockquote>
  <p>project_name.app.py</p>
  </blockquote>
  <pre><code>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 &quot;value_from_method&quot;
  
  # this line will execute get_ssm before any unit test, 
  # how mock this to always have a mock value
  SSM_VALUE = get_ssm()
  </code></pre>
  <p>Here is my unit test</p>
  <pre><code>&quot;&quot;&quot; response_transformer TESTS &quot;&quot;&quot;
  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 == &quot;testing_value&quot;
  </code></pre>
  <p>So I would like to mock SSM_VALUE but not executing the get_ssm method associated with it, how should I achieve this?</p>