mercredi 3 août 2016

python: mock variable at the module

I'm trying to find a good way to mock a variable set at the module level. Say I'm testing the verify_ssl function in the mymodule.py module and mymodule has a global variable VERIFY_SSL. How do I mock the mock the global value of VERIFY_SSL

The example below is a bit contrived, but it illustrates the issue:

# mymodule.py

VERIFY_SSL = True

def verify_ssl():
    return VERIFY_SSL

Here is the associated test:

# tests.py
import mock
from mymodule import verify_ssl

@mock.patch('mymodule')
def test_record_lastrun_time__basic(self, mock_mymodule):
    # setup
    mock_mymodule.VERIFY_SSL = True

    # test this
    return_value = verify_ssl()

    # checks
    self.assertTrue(return_value)

The above test fails with the following error:

AssertionError: False is not true

So the mock doesn't appear to be happening. Am I going about this test the correct way?

Aucun commentaire:

Enregistrer un commentaire