How to set environment variables for pytest in VSCode

To run tests I usually use database with tmpfs volume, sometimes it makes them more than twice faster. To achieve this I run the database on another port and use env variable in application config, like this

docker run -d --name=mongo_test -p 27018:27017 --tmpfs /data/db mongo:4.2.3
MONGODB_URI=mongodb://localhost:27018/test_db pytest --cov=app tests

The configuration for env variables in VSCode is simple, you need to place .env file into your project and add "python.envFile": "${workspaceFolder}/.env" to .vscode/settings.json, reload the window. When you’ll run tests in VSCode, it will use these variables.

comments powered by Disqus