I have config.py module that loads environment variables necessary for connecting to MongoDB. When I try to run the test discovery in vscode pytest doesn't find these environment variables. In the test files, I use some methods from a module that imports the config module, so the code that reads these env vars always run.
The environment variables are defined in my .bashrc file.
Pytest alone can run the test using these environment variables if I run it from the terminal
python /home/orch/.vscode/extensions/ms-python.python-2019.9.34911/pythonFiles/testing_tools/run_adapter.py discover pytest -- -s --cache-clear tests
That's the command vscode runs when activating the tests discovery tool
I expect the vs code to find the tests
the out is the following error:
tests/test_db.py:5: in <module>
from src.core.db import MongoHandler
src/core/db.py:5: in <module>
from src.core.loggers import gcl_log_event
src/core/loggers.py:8: in <module>
from configuration import config
configuration/config.py:22: in <module>
user_name = os.environ['EVO_DB_USER_NAME']
../../.local/share/virtualenvs/evolve-sjbSOQds/lib/python3.7/os.py:678: in __getitem__
raise KeyError(key) from None
E KeyError: 'EVO_DB_USER_NAME'
I had a similar problem and solved it by declaring an environment variable file .envs in my workspace folder which could look like:
EVO_DB_USER_NAME=<your_name>
OTHER_ENVIRONMENT_VARIABLE=<other_name>
With this, you can then extend your .vscode/settings.json as:
{
...,
"python.testing.pytestEnabled": true,
"python.envFile": "${workspaceFolder}/.envs",
...,
}
Such it imports the environment variables, before running the test discovery.
Further, you find this discussed in following issue of visual studio code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With