pytest-env

Install

Per https://stackoverflow.com/a/39162893/13697228:

conda install -c conda-forge pytest-env

Or:

pip install pytest-env

pytest.ini

Create pytest.ini file in project directory:

[pytest]
env = 
    ENV_VAR=RandomStuff

Python

In your code, load environment variables as you normally would:

import os
env_var = os.environ["ENV_VAR"]

pytest / VS Code

Either run:

pytest

(Notice how it says configfile: pytest.ini)

C:\Users\sterg\Documents\GitHub\sparks-baird\mp-time-split> pytest
==================================== test session starts ===================================== platform win32 -- Python 3.9.12, pytest-7.1.1, pluggy-1.0.0 rootdir:
C:\Users\sterg\Documents\GitHub\sparks-baird\mp-time-split,
configfile: pytest.ini plugins: anyio-3.6.1, cov-3.0.0, env-0.6.2
collecting ...

Or:

This only seems to work with breakpoints that have manually been set, I'm guessing some other change is needed to pause on errors.

Python for VS Code Extension

Apparently the Python for VS Code extension recognizes a .env file automatically. E.g. .env file:

ENV_VAR=RandomStuff

Haven't verified, but I'd assume this has the same behavior as using pytest-env with a pytest.ini file.

When all else fails

When I don't feel like dealing with the strange hackery necessary to get VS Code, Anaconda environments, and pytest playing nicely together (and/or forget how I fixed it before), I call my tests manually and run it like a normal script (see below). This may not work with more advanced pytest trickery using fixtures for example. At the end of your Python script, you can add something like:

if __name__ == "__main__":
    my_first_test()
    my_second_test()

and run it in debug mode (F5) as normal.

Answer from Sterling on Stack Overflow
🌐
Visual Studio Code
code.visualstudio.com › docs › python › testing
Python testing in Visual Studio Code
November 3, 2021 - rootdir is dynamically adjusted based on the presence of a python.testing.cwd setting in your workspace. You can also configure pytest using a pytest.ini file as described on pytest Configuration.
🌐
Donjayamanne
donjayamanne.github.io › pythonVSCodeDocs › docs › unittests_pytest-framework
py.test | Python in Visual Studio Code
This section outlines the details necessary to get you up and started with using the pytest testing framework with Visual Studio Code. Detailed info on failing assert statements (no need to remember self.assert* names); ... Assign the value true against the setting python.unitTest.pyTestEnabled as outlined here.
Discussions

vscode debugger - How to configure VS code for pytest with environment variable - Stack Overflow
Could not really figure out how to fix "unit" test debugging with Vscode. But with Pytest one can call tests from the command line like this: python -m pytest That means the VSCode launch.json debug configuration file can be setup using the "module" property for Python: More on stackoverflow.com
🌐 stackoverflow.com
Issues setting up python testing in VSCODE using pytest - Stack Overflow
I am trying to use the testing extension in VSCode with the Python extension. I am using pytest as my testing library. My folder structure looks like this: PACKAGENAME/ ├─ PACKAGENAME/ │ ├─ __init... More on stackoverflow.com
🌐 stackoverflow.com
On Visual Studio Code, how do I specify my pytest.ini file for test discovery - Stack Overflow
How to I specify the path to the ... that the vscode-python plugin can correctly/successfully discover my test files? No matter what I try, I get Test discovery failed, with no reasons given. ... Configure the testing framework you want to use, in this case PyTest (ctrl+shift+p > Python: Configure Tests > Pytest > {pytest rootdir} Open the settings.json file ... More on stackoverflow.com
🌐 stackoverflow.com
Visual Studio Code triggers use of pytest when configured for unittest - Stack Overflow
Instead I was able to remove pytest discovery by adding these lines to my settings.json. Now VSCode no longer uses pytest discovery in these directories: More on stackoverflow.com
🌐 stackoverflow.com
🌐
Pytest with Eric
pytest-with-eric.com › introduction › how-to-run-pytest-in-vscode
How To Run Pytest In VS Code (Easy To Follow Step-By-Step Tutorial) | Pytest with Eric
August 13, 2023 - You can see I’ve set it to look for tests in the tests folder. And for Pytest to detect your Python file as a unit test, don’t forget to use test as a prefix or suffix in your file name. For e.g test_example.py. Test discovery is an automatic feature that detects your tests or notifies you if tests are not detected. It will automatically initiate when you enable Pytest for your Python project.
Top answer
1 of 4
22

pytest-env

Install

Per https://stackoverflow.com/a/39162893/13697228:

conda install -c conda-forge pytest-env

Or:

pip install pytest-env

pytest.ini

Create pytest.ini file in project directory:

[pytest]
env = 
    ENV_VAR=RandomStuff

Python

In your code, load environment variables as you normally would:

import os
env_var = os.environ["ENV_VAR"]

pytest / VS Code

Either run:

pytest

(Notice how it says configfile: pytest.ini)

C:\Users\sterg\Documents\GitHub\sparks-baird\mp-time-split> pytest
==================================== test session starts ===================================== platform win32 -- Python 3.9.12, pytest-7.1.1, pluggy-1.0.0 rootdir:
C:\Users\sterg\Documents\GitHub\sparks-baird\mp-time-split,
configfile: pytest.ini plugins: anyio-3.6.1, cov-3.0.0, env-0.6.2
collecting ...

Or:

This only seems to work with breakpoints that have manually been set, I'm guessing some other change is needed to pause on errors.

Python for VS Code Extension

Apparently the Python for VS Code extension recognizes a .env file automatically. E.g. .env file:

ENV_VAR=RandomStuff

Haven't verified, but I'd assume this has the same behavior as using pytest-env with a pytest.ini file.

When all else fails

When I don't feel like dealing with the strange hackery necessary to get VS Code, Anaconda environments, and pytest playing nicely together (and/or forget how I fixed it before), I call my tests manually and run it like a normal script (see below). This may not work with more advanced pytest trickery using fixtures for example. At the end of your Python script, you can add something like:

if __name__ == "__main__":
    my_first_test()
    my_second_test()

and run it in debug mode (F5) as normal.

2 of 4
10

Could not really figure out how to fix "unit" test debugging with Vscode. But with Pytest one can call tests from the command line like this:
python -m pytest <test file>

That means the VSCode launch.json debug configuration file can be setup using the "module" property for Python:

"configurations": [
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "pytest",
            "args": ["--headful","--capture=no", "--html=report.html"],
        }

This is good enough to do debugging of python tests. Also you can then insert environment variables

   "env": {
        "ENV_VAR":"RandomStuff"
    }
🌐
Atlassian
openedx.atlassian.net › wiki › spaces › AC › pages › 987136306 › VSCode
To configure pytest tests in VSCode - Open edX Community
To configure pytest tests in VSCode: Install the Python Extension for VSCode · Run the Python: Configure Tests command · In Settings > Python > Testing: Pytest Args, add the --no-cov argument · Run the Python: Discover Tests command · This adds the Run Test | Debug Test buttons in VSCode ...
🌐
Graycode
graycode.ie › home › how to set up testing explorer with python pytest in vscode
How to set up Testing Explorer with Python Pytest in VSCode - Graycode
October 5, 2023 - Discover how to set up Testing Explorer with Python Pytest in VSCode. Follow step-by-step instructions to configure the Testing tab, select the Python test framework, specify the test directory, and resolve import module errors. Learn how to create a '.env' file to define the Python environment ...
Find elsewhere
🌐
YouTube
youtube.com › python 360
Pytest | Visual Studio Code - YouTube
VSCODE - Pytest tips :◼️ Missing "test tube" icon : fixed◼️ 'Module not found' : fixed◼️ Specify correct Python interpreter! (Ctrl + Shift + P )◼️ Green Tick...
Published   September 6, 2022
Views   23K
🌐
DZone
dzone.com › coding › languages › how to set up visual studio code for python testing and development
How to Set Up Visual Studio Code for Python Testing and Development
August 30, 2019 - Open Command Palette (ctrl +shift +P) and start typing ‘python: select linter.’ It will display a list of available Python linters. You can add any of the settings to your user settings.json file (opened with the File > Preferences > Settings: ...
🌐
GitHub
github.com › hendrics › python-vscode-pytest-example
GitHub - hendrics/python-vscode-pytest-example: Example how to configure vscode to see pytest assert failures in code and problem view · GitHub
This repository gives a simple example how to configure the problem matcher for a Test Task of vscode in order to get the test failures and other issues into the problems view and highlighted in the code view.
Starred by 14 users
Forked by 4 users
Languages   Python
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
Python Test Explorer for Visual Studio Code - Visual Studio Marketplace
February 3, 2024 - Extension for Visual Studio Code - Run your Python tests in the Sidebar of Visual Studio Code
🌐
ComfyUI
mslinn.com › blog › 2023 › 08 › 20 › pytest-intro.html
Pytest and Visual Studio Code
August 20, 2023 - Here is an example .vscode/launch.json that demonstrates this: ... { "version": "0.2.0", "configurations": [ { "name": "PyTest All", "type": "python", "request": "launch", "module": "pytest", "justMyCode": true }, { "args": ["--nf", "--lf"], "name": "PyTest New and Failing", "type": "python", "request": "launch", "module": "pytest", "justMyCode": true }, ] } 😁 · You can set breakpoints in your code and debug your tests this way.
🌐
GitHub
github.com › hendrics › python-vscode-pytest-example › blob › master › .vscode › settings.json
python-vscode-pytest-example/.vscode/settings.json at master · hendrics/python-vscode-pytest-example
Example how to configure vscode to see pytest assert failures in code and problem view - hendrics/python-vscode-pytest-example
Author   hendrics
🌐
DEV Community
dev.to › yamakanto › how-i-set-up-vscode-for-python-tests-coverage-profiling-2jf4
How I set up VSCode for Python (tests, coverage, profiling) - DEV Community
January 7, 2021 - run pytest --cov=main_module --cov-report=xml tests · --cov=main_module enables coverage for the main_module · --cov-report=xml generates a coverage.xml file that can be used with the vscode extension coverage gutters.
🌐
Reddit
reddit.com › r/vscode › how to set up pytest for subdirectory? pytest discovery error
r/vscode on Reddit: how to set up pytest for subdirectory? Pytest Discovery Error
July 31, 2023 -

I am working on a data science project and my project tree looks like this:

data/
notebooks/
scripts/
utils/
- __init__.py
- utils1.py
- utils2.py
- utils3.py
- utils4.py
- tests/
-- test_utils1.py
-- test_utils2.py
-- test_utils3.py
-- test_utils4.py

I import functions I wrote in utils1.py, utils2.py, etc. to reuse throughout my notebooks and scripts. To run the tests for these, I cd into utils/ and then run python -m pytest from the terminal, which works fine.

I saw that there is a beaker icon for testing in VSCode, but my tests are not being discovered:

screenshot

Contents of __init__.py look a little like this:

from utils.utils4 import foo, barfrom .utils1 import foo2, bar2from .utils2 import function1, function2from .utils3 import *

See the comment below for Output.

It breaks my flow to switch to the terminal each time I modify a utility function to verify tests are passing. How should I configure VSCode to make better use of the Test Explorer?

Update: see my comment for the solution

Top answer
1 of 4
1
2023-07-26 15:21:31.015 [info] Discover tests for workspace name: project-name-here - uri: /Users/username/repos/project-name-here 2023-07-26 15:21:31.020 [info] > /opt/homebrew/bin/python3.11 ~/.vscode/extensions/ms-python.python-2023.12.0/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir ./utils/tests/ -s --cache-clear . 2023-07-26 15:21:31.020 [info] cwd: ./utils/tests/ 2023-07-26 15:21:31.214 [error] Error discovering pytest tests: n [Error]: ============================= test session starts ============================== platform darwin -- Python 3.11.0, pytest-7.3.2, pluggy-1.0.0 rootdir: /Users/username/repos/project-name-here/utils/tests plugins: anyio-3.6.2 collected 0 items / 4 errors ==================================== ERRORS ==================================== ___________________ ERROR collecting test_utils1.py ____________________ ImportError while importing test module '/Users/username/repos/project-name-here/utils/tests/test_utils1.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: /opt/homebrew/Cellar/python@3.11/3.11.0/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py:126: in import_module return _bootstrap._gcd_import(name[level:], package, level) ../__init__.py:5: in from .utils2 import function1, function2 ../utils2.py:26: in from numpy import array as np_array E ModuleNotFoundError: No module named 'numpy' ____________________ ERROR collecting test_utils2.py _____________________ ImportError while importing test module '/Users/username/repos/project-name-here/utils/tests/test_utils2.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: /opt/homebrew/Cellar/python@3.11/3.11.0/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py:126: in import_module return _bootstrap._gcd_import(name[level:], package, level) ../__init__.py:5: in from .utils2 import function1, function2 ../utils2.py:26: in from numpy import array as np_array E ModuleNotFoundError: No module named 'numpy' _____________________ ERROR collecting test_utils3.py ______________________ ImportError while importing test module '/Users/username/repos/project-name-here/utils/tests/test_utils3.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: /opt/homebrew/Cellar/python@3.11/3.11.0/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py:126: in import_module return _bootstrap._gcd_import(name[level:], package, level) ../__init__.py:5: in from .utils2 import function1, function2 ../utils2.py:26: in from numpy import array as np_array E ModuleNotFoundError: No module named 'numpy' ________________________ ERROR collecting test_utils4.py ________________________ ImportError while importing test module '/Users/username/repos/project-name-here/utils/tests/test_utils4.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: /opt/homebrew/Cellar/python@3.11/3.11.0/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py:126: in import_module return _bootstrap._gcd_import(name[level:], package, level) ../__init__.py:5: in from .utils2 import function1, function2 ../utils2.py:26: in from numpy import array as np_array E ModuleNotFoundError: No module named 'numpy' =========================== short test summary info ============================ ERROR test_utils1.py ERROR test_utils2.py ERROR test_utils3.py ERROR test_utils4.py !!!!!!!!!!!!!!!!!!! Interrupted: 4 errors during collection !!!!!!!!!!!!!!!!!!!! ==================== no tests collected, 4 errors in 0.04s ===================== Traceback (most recent call last): File "/Users/username/.vscode/extensions/ms-python.python-2023.12.0/pythonFiles/testing_tools/run_adapter.py", line 22, in main(tool, cmd, subargs, toolargs) File "/Users/username/.vscode/extensions/ms-python.python-2023.12.0/pythonFiles/testing_tools/adapter/__main__.py", line 99, in main parents, result = run(toolargs, **subargs) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/username/.vscode/extensions/ms-python.python-2023.12.0/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 47, in discover raise Exception("pytest discovery failed (exit code {})".format(ec)) Exception: pytest discovery failed (exit code 2) at ChildProcess. (/Users/username/.vscode/extensions/ms-python.python-2023.12.0/out/client/extension.js:2:241817) at Object.onceWrapper (node:events:628:26) at ChildProcess.emit (node:events:513:28) at maybeClose (node:internal/child_process:1121:16) at Socket. (node:internal/child_process:479:11) at Socket.emit (node:events:513:28) at Pipe. (node:net:757:14)
2 of 4
1
Here's what I did to fix my pytest config: selected the desired Python interpreter based on my virtual environment using the Command Palette (Python: Select Interpreter) added __init__.py to utils/tests/ In the tests/test_utils#.py files, I was previously importing like from .utils# import foo, bar but I changed it to from utils.utils# import foo, bar verified that my workspace settings had the following lines:"python.testing.unittestEnabled": false,"python.testing.pytestEnabled": true,"python.testing.pytestArgs": ["."],
🌐
TMS Outsource
tms-outsource.com › home › how to run pytest in vscode for testing
How to Run Pytest in VSCode for Testing - TMS Outsource
January 7, 2025 - VSCode stores this preference in settings.json and enables automatic test discovery across your workspace. Press Ctrl+, to open Settings. Search for “python testing”. Find “Python > Testing: Pytest Enabled” and check the box.
Top answer
1 of 2
20

After much experimentation I finally found how to do it. What I needed was to pass user name and password to my script in order to allow the code to log into a test server. My test looked like this:
my_module_test.py

import pytest
import my_module

def login_test(username, password):
    instance = my_module.Login(username, password)
    # ...more...

conftest.py

import pytest

def pytest_addoption(parser):
    parser.addoption('--username', action='store', help='Repository user')
    parser.addoption('--password', action='store', help='Repository password')

def pytest_generate_tests(metafunc):
    username = metafunc.config.option.username
    if 'username' in metafunc.fixturenames and username is not None:
        metafunc.parametrize('username', [username])

    password = metafunc.config.option.password
    if 'password' in metafunc.fixturenames and password is not None:
        metafunc.parametrize('password', [password])

Then in my settings file I can use:
.vscode/settings.json

{
    // ...more...
    "python.testing.autoTestDiscoverOnSaveEnabled": true,
    "python.testing.unittestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.pytestEnabled": true,
    "python.testing.pytestArgs": [
        "--exitfirst",
        "--verbose",
        "test/",
        "--username=myname",
        "--password=secret",
    // ...more...
    ],
}

An alternative way is to use pytest.ini file instead:
pytest.ini

[pytest]
junit_family=legacy
addopts = --username=myname --password=secret

2 of 2
2

If this is just for the debugger then you can specify things in "args" in your launch.json file. See https://code.visualstudio.com/docs/python/debugging#_args for more details.