When main.py is executed, the Python interpreter scans all files and executes any functions that were called at the beginning.
In source/settings.py, there's a function called get_settings(): which act as global variable with function call.
def get_settings():
# Retrieves settings for production environment
return {} # some_dict
settings = get_settings()
This function is intended to run in production or other environments.
However, when pytest starts, I want to replace the execution of get_settings() with a different function located in tests/conftest.py:
def get_local_settings():
# Retrieves settings from the development database
return {} # some_dict
with patch('source.settings.get_settings') as mock_settings:
mock_settings.return_value = get_local_settings()
I thought that conftest.py will be called in beginning of test and with mock_settings.return_value will be assigned. So, whenever get_settings() is called get_local_settings() value will be returned.
Despite implementing this logic, get_settings() continues to be called when pytest starts.
I'm seeking a solution that doesn't involve modifying source/settings.py with conditional logic like:
if ENV == LOCAL:
settings = tests.conftest.get_local_settings()
else:
settings = get_settings()