Hi guys my actual method uses envvars which will be set as a constant
PERSONAL_ID = os.getenv("PERSONAL_ID")
PATH_NAME = os.getenv("PATH_NAME")
I use this envvar in my method (not as a input parameter) and now I try to test it but during the assert I get "None"
I have tried this above my test:
@mock.patch.dict(os.environ, {"PERSONAL_ID ": "123", "PATH_NAME": "test_path"})
CodePudding user response:
You don't really need to "mock" the environment variables. You can just set them to an appropriate value:
os.environ['PERSONAL_ID'] = '123'
os.environ['PATH_NAME'] = 'test_path'
These will only be visible within your tests (because that's just how environment variables work -- they can be seen in the current process and its children).
CodePudding user response:
Probably this was caused by a typo, remove space at the end of PERSONAL_ID
:
@mock.patch.dict(os.environ, {"PERSONAL_ID": "123", "PATH_NAME": "test_path"})
This example works:
import os
def uses_env_vars():
return os.getenv('PERSONAL_ID')
@mock.patch.dict(os.environ, {"PERSONAL_ID": "123", "PATH_NAME": "test_path"}, clear=True)
def test_uses_env_vars():
assert uses_env_vars() == os.environ["PERSONAL_ID"]