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 thelaunch.jsonfile in the.vscodefolder 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.jsonfile disables thejustMyCodesetting 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 OverflowI'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 thelaunch.jsonfile in the.vscodefolder 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.jsonfile disables thejustMyCodesetting 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.
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"
}
JustMyCode doesn't work when debugging python tests through the GUI
Unable to disable justMyCode in Test Debugging
Python separate pytest config for running tests vs debugging
how to disable justMyCode in unittest debug
If you want to enter debugging when running pytest in VSCode and stay at a line of code, you could click 'Debug Test' at the top of the method after selecting the pytest test, as shown in the screenshot:

In addition, "python.testing.pytestArgs": [], in .vscode\settings.json is the folder path of the tested file, for example, my test file is in Test_cc under the a folder.
> "python.testing.pytestArgs": [
> "a/Test_cc"
> ],
If this is not what you want, please let me know and describe the details of your needs.
Reference: Debug tests.
Update:
Usually, when we debug the test in VSCode, without setting a breakpoint for it, it will only display the test result. (success or failure). It will only display relevant test information in the console OUTPUT.
When I use the extension 'Python Test Explorer for Visual Studio code', it will display the debugging test information on the console, prompting the issue.

This Answer actually solves the problem. I wish there would be a more straight-forward way, either from Visual Studio Code or a simple raise flag from pytest.
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",
}
}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
}
]
```
Just adding "justMyCode": false to launch.json doesn't work. You need to add a separate config in launch.json like below. FYI each {} represents a config.
"configurations": [
{
.... # existing config
},
{
"name": "Debug Unit Test",
"type": "python",
"request": "test",
"justMyCode": false,
}
]
As pointed out in here
For this you need to change the launch.json file. Inside the launch.json file you have to set "justMyCode" to false.
As described here. (I was pointed to that link through this post on the Visual Studio Code site.)
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.
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"
}