Pytest mock a function inside a global variable, before the test

23 views Asked by At

I have a python code which has something like this:

class ImportantValues:
    def __init__():
        self.values = from_external_service(user, password)

important_values = ImportantValues()

Other file

from importantvalues import important_values
def usign_important_values():
    return something(important_values.values)

from_external_service is a API service where we get those values, and it needs a user and password And I need to test usign_important_values, so I have something like this: test

from usignvalues import usign_important_values

def test_usign_important_files():
    assert usign_important_values() == 200

But I cannot run the test because I cannot set on the test the real user and password, I want to mock the response of this function (from_external_service), but I cannot because this line was executed before the test.

There is some way to mock from_external_service?

I already tried to patch the from_external_service function inside the test function and also tried to use a fixture with the session scope. But both methods are inside the test function and the test function is executed after the import of from_external_service on the code.

0

There are 0 answers