Or Chen
Or Chen

Reputation: 489

Vscode pytest test discovery fails due to missing environment variable

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'

Upvotes: 24

Views: 18101

Answers (3)

sir_dance_a_lot
sir_dance_a_lot

Reputation: 510

I had a similar problem and solved it by declaring an environment variable file .env in my workspace folder which could look like:

EVO_DB_USER_NAME=<your_name>
OTHER_ENVIRONMENT_VARIABLE=<other_name>

Such it imports the environment variables, before running the test discovery.

Further, you find this discussed in following issue of visual studio code.

Upvotes: 36

Andrew
Andrew

Reputation: 4338

The environment variables are defined in my .bashrc file.

If you define them in the file .env in your workspace directory, the default vscode python setting should pick them up from there. This default can be changed.

See https://code.visualstudio.com/docs/python/environments :

By default, the Python extension looks for and loads a file named .env in the current workspace folder, then applies those definitions. The file is identified by the default entry "python.envFile": "${workspaceFolder}/.env" in your user settings (see General Python settings). You can change the python.envFile setting at any time to use a different definitions file.

Upvotes: 0

Otoa
Otoa

Reputation: 31

Long story short... installing pytest-dotenv package may solve this problem.


Since it has been more than 2 years, maybe this isn't a problem for you anymore...

But I was just facing the same problem - pytest not respecting my .env file in a vscode python project - and I found this python package called pytest-dotenv (with python-dotenv) which solved the problem for me.

With the project setup of loading .env file (which is located in the root of my workspace) for linting and runnng in terminal, and these python packages, I was wable to make .env file loaded by the test explorer and pytest.

In the shell (after activating my virtualenv for my project), I ran to install these packages:

$ pip install python-dotenv pytest-dotenv

Then in the settings.json of my workspace, I put the lines below. ("app" is the subfolder where all my python files of my project is located in my workspace):

{
    ...
    "python.envFile": "${workspaceFolder}/.env",
    ...
    "python.testing.pytestEnabled": true,
    "python.testing.pytestArgs": [
        "-v", "-s",
        "--rootdir", "${workspaceFolder}/app",
    ],
    ...
}

In my .env file, I also have PYTHONPATH variable pointing to my "app" subfolder with absolute path:

export PYTHONPATH="/x/.../.../app"

Also, the environment variable "NEO4J_URI" - which I want the test to load - is written in the same .env file:

My .env file and its location

Here is the result

The test of checking of environment variable "NEO4J_URI" being loaded and its result

I hope this helps you or some people who are in the same situation...

Upvotes: 3

Related Questions