I'd try following the instructions from the VS Code Python docs' section on debugging tests, which states:

To customize settings for debugging tests, you can specify "purpose": ["debug-test"] in the launch.json file in the .vscode folder from your workspace. This configuration will be used when you run Test: Debug All Tests, Test: Debug Tests in Current File and Test: Debug Test at Cursor commands.

For example, the configuration below in the launch.json file disables the justMyCode setting for debugging tests:

{
  "name": "Python: Debug Tests",
  "type": "debugpy",
  "request": "launch",
  "program": "${file}",
  "purpose": ["debug-test"],
  "console": "integratedTerminal",
  "justMyCode": false
}

If you have more than one configuration entry with "purpose": ["debug-test"], the first definition will be used since we currently don't support multiple definitions for this request type.

Note: I've also seen older configs floating around that use "request": "test" instead of "purpose": ["debug-test"] (Ex. this), so you can try that if "purpose": ["debug-test"] doesn't work for you.

There also seems to be a "debugStdLib": true property you can use if you want to step into standard library things (source).

There's an open issue ticket where this is apparently not working for version 2023.8.0 of the Python extension (#21249), but it will be fixed in later versions. Also potentially related: Pytest restart debugger works only with code-workspace files #21365.

Answer from starball on Stack Overflow
🌐
Visual Studio Code
code.visualstudio.com › docs › python › testing
Python testing in Visual Studio Code
November 3, 2021 - For more information on test coverage, visit VS Code's Test Coverage documentation. To debug tests, right-click on the gutter decoration next to the function definition and select Debug Test, or select the Debug Test icon next to that test in ...
Top answer
1 of 2
23

I'd try following the instructions from the VS Code Python docs' section on debugging tests, which states:

To customize settings for debugging tests, you can specify "purpose": ["debug-test"] in the launch.json file in the .vscode folder from your workspace. This configuration will be used when you run Test: Debug All Tests, Test: Debug Tests in Current File and Test: Debug Test at Cursor commands.

For example, the configuration below in the launch.json file disables the justMyCode setting for debugging tests:

{
  "name": "Python: Debug Tests",
  "type": "debugpy",
  "request": "launch",
  "program": "${file}",
  "purpose": ["debug-test"],
  "console": "integratedTerminal",
  "justMyCode": false
}

If you have more than one configuration entry with "purpose": ["debug-test"], the first definition will be used since we currently don't support multiple definitions for this request type.

Note: I've also seen older configs floating around that use "request": "test" instead of "purpose": ["debug-test"] (Ex. this), so you can try that if "purpose": ["debug-test"] doesn't work for you.

There also seems to be a "debugStdLib": true property you can use if you want to step into standard library things (source).

There's an open issue ticket where this is apparently not working for version 2023.8.0 of the Python extension (#21249), but it will be fixed in later versions. Also potentially related: Pytest restart debugger works only with code-workspace files #21365.

2 of 2
6

This is a limitation in current VSCode version: VSCode only uses launch.json file to configure pytest debugging options, it ignores the workspace launch section.
It is planned to be fixed soon: https://github.com/microsoft/vscode-python/issues/21249
As a workaround, we can duplicate the workspace launch section in a .vscode/launch.json file, ex:

{
    "configurations": [
        {
            "name": "Python: Debug Tests",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "purpose": ["debug-test"],
            "console": "integratedTerminal",
            "justMyCode": false,
            "presentation": {
                "hidden": true, // keep original launch order in 'run and debug' tab
            }
        },
    ],
    "version": "0.2.0"
}
Discussions

JustMyCode doesn't work when debugging python tests through the GUI
Type: Bug Behaviour Expected vs. Actual I cannot disable "just my code" and debug tests through the GUI. Also, [object Object] notification keeps popping Steps to reproduce: When I setup ... More on github.com
🌐 github.com
11
May 15, 2023
Unable to disable justMyCode in Test Debugging
The sample project consists of ... to use pytest, python 3.12.1: import logging logger = logging.getLogger() def test_test(): logger = logging.getLogger() The debugger doesn't step into any of these logging.getLogger() when run as test, and successfully does it if run directly. When the debugger skips a step-in, the notification appears: Frame skipped from debugging during step-in. Note: may have been skipped because of "justMyCode" option (default ... More on github.com
🌐 github.com
1
February 9, 2024
Python separate pytest config for running tests vs debugging
Unfortunately, as of now, there's no built-in way in VS Code to specify a different pytest configuration when debugging a single test. More on reddit.com
🌐 r/vscode
3
2
February 15, 2024
how to disable justMyCode in unittest debug
I search it , and i cannot find solution More on github.com
🌐 github.com
6
August 23, 2019
🌐
GitHub
github.com › microsoft › vscode-python › issues › 21249
JustMyCode doesn't work when debugging python tests through the GUI · Issue #21249 · microsoft/vscode-python
May 15, 2023 - ============================= test session starts ============================== platform linux -- Python 3.8.10, pytest-6.2.4, py-1.11.0, pluggy-0.13.1 rootdir: /home/dev/projects/test plugins: html-3.1.1, metadata-2.0.1 collected 1 item ...... Frame skipped from debugging during step-in. Note: may have been skipped because of "justMyCode" option (default == true).
Published   May 15, 2023
Author   NicolasM-Forsk
🌐
GitHub
github.com › microsoft › vscode-python-debugger › issues › 207
Unable to disable justMyCode in Test Debugging · Issue #207 · microsoft/vscode-python-debugger
February 9, 2024 - The sample project consists of ... to use pytest, python 3.12.1: import logging logger = logging.getLogger() def test_test(): logger = logging.getLogger() The debugger doesn't step into any of these logging.getLogger() when run as test, and successfully does it if run directly. When the debugger skips a step-in, the notification appears: Frame skipped from debugging during step-in. Note: may have been skipped because of "justMyCode" option (default ...
Author   roman-anna-money
🌐
Endjin
endjin.com › blog › 2024 › 09 › how-to-step-into-external-code-when-debugging-a-python-behave-test-in-vs-code
How to step into external code when debugging a Python Behave test in VS Code | endjin
September 26, 2024 - If you want to step into external code when debugging a Python behave test, you will find that the usual VS Code mechanisms for configuring debugging do not work. In this post I'll show you how you can enable this. TLDR; In settings.json add the following line: "behave-vsc.justMyCode": false.
🌐
Reddit
reddit.com › r/vscode › python separate pytest config for running tests vs debugging
r/vscode on Reddit: Python separate pytest config for running tests vs debugging
February 15, 2024 -

Hello, I am hoping someone here has an idea on how to do this.

I have a pytest.ini that looks like this
[pytest]
addopts = -n auto --dist worksteal --cov-report xml:coverage.xml

This works great when running the tests so they run quickly and I get test coverage. However, when I try to debug a single by right clicking on the green arrow for the test and selecting debug it won't hit any breakpoints in the test. If I remove the pytest.ini config then it will hit the breakpoints.

Does anyone know of a way to have a separate config when running a single test.

Thanks

UPDATE: I figured out a way to make this work. It turns out using type debugpy will not work and doesn't even read the environment variable. However, if I set the type to python it works just fine. It looks like this is a feature that debugpy needs to support.

{
            "name": "Python: Debug Tests",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "purpose": ["debug-test"],
            "console": "integratedTerminal",
            "justMyCode": false,
            "env": {
                "PYTEST_ADDOPTS": "-c pytest_alt.ini",
          }
        }
🌐
ComfyUI
mslinn.com › blog › 2023 › 08 › 20 › pytest-intro.html
Pytest and Visual Studio Code
August 20, 2023 - Run and Debug configurations that invoke the pytest module work perfectly. 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 }, ] } 😁
Find elsewhere
🌐
CopyProgramming
copyprogramming.com › howto › vs-code-python-debugging-pytest-test-with-the-debugger
Python Debugging with Pytest in VS Code: Complete Guide for 2026
December 3, 2025 - For pytest debugging, you need a specific configuration with the "purpose": ["debug-test"] setting. Create or update your .vscode/launch.json file with this configuration: { "version": "0.2.0", "configurations": [ { "name": "Python: Debug Tests", "type": "debugpy", "request": "launch", "program": "${file}", "purpose": ["debug-test"], "console": "integratedTerminal", "justMyCode": false } ] }
🌐
Darklab8
darklab8.github.io › blog › article_visual_debugger_in_vscode.html
Vscode debugger recipes for python and docker- blog
git clone https://github.com/d... left sidebar, click on file.py in the directory tree. Select in the top menu: Run -> Star Debugging (F5) Now let’s check it is working for pytest test debug:...
🌐
GitHub
github.com › microsoft › vscode-python › issues › 7083
how to disable justMyCode in unittest debug · Issue #7083 · microsoft/vscode-python
August 23, 2019 - area-debugginginfo-neededIssue requires more information from posterIssue requires more information from poster
Author   chen19901225
🌐
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 - [tool:pytest] testpaths = src/ addopts = -n4 --cov=src --cov-report=term --cov-report=lcov:.coverage-dir/lcov.info --cov-report=html:.coverage-dir/html required_plugins = pytest-cov pytest-xdist [coverage:run] omit = **/*/tests/* branch = true data_file = .coverage [coverage:report] fail_under = 100 · Here's my /.vscode/launch.json · { "version": "0.2.0", "configurations": [ { "name": "Python: Debug Tests", "type": "debugpy", "request": "launch", "program": "${file}", "purpose": ["debug-test"], "console": "integratedTerminal", "justMyCode": false, "env": { "PYTEST_ADDOPTS": "--no-cov" }, } ] } For further actions, you may consider blocking this person and/or reporting abuse ·
🌐
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
🌐
Reddit
reddit.com › r/djangolearning › how to setup vscode to debug django app tests (pytest)?
r/djangolearning on Reddit: How to setup VScode to debug Django app tests (pytest)?
March 28, 2023 -

I need to stop debugger on one breakpoint in tests but i don't know how to do it.

How "configurations" in launch.json file should looks like? I trying with something like that but its doesnt work:

```

"version": "0.2.0",

"configurations": [

{

"name": "Python: Django:web_cloud tests",

"python": "/home/szymon/app_project/venv/web_cloud/bin/python3",

"type": "python",

"request": "launch",

"program": "/home/szymon/app_project/app/web_cloud/manage.py",

"args": [

"pytest"

],

"console": "internalConsole",

"django": true,

"justMyCode": true

}

]

```

🌐
GitHub
github.com › microsoft › vscode-python › issues › 7347
"justMyCode" does not enable standard library debugging · Issue #7347 · microsoft/vscode-python
September 12, 2019 - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "subProcess": true, "justMyCode": false, } ] } Simple .venv created and pytest installed, attempting to debug this:
Author   DazEdword
🌐
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, 2026 - Debug pytest tests by setting breakpoints in your test code, then right-clicking the test and selecting “Debug Test”. VSCode launches the debugger, pauses at breakpoints, and provides access to the Debug Console for inspecting variables ...
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"
    }