I have some settings in a file, which read from my environment:
set1 = os.getenv(...)
SETTINGS = {
key1: set1,
...
}
In another File, I am importing them:
from File1 import SETTINGS
MyClass():
...
In my tests file, I am mocking the MyClass methods, however, it is trying to access env vars, due to previous imports.
Is there anyway I can mock File2 imports?
This is my tests file:
from unittest.mock import patch
import pytest
from File2 import MyClass
@patch.object(
MyClass,
"my_method",
return_value=None,
)
def test_my_test(mock1):
my_class = MyClass()
....
Summary: How do I avoid the os.getenv() for my tests?
Instead of trying to avoid it, you could set a value for the environment variable in your conftest.py
file which would allow you to test closer to the actual code.
import os
os.environ["var"] = "foo"