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.

Answer from Jill Cheng on Stack Overflow
Discussions

When debugging a pytest unittest in VSCode, the python debugger detaches after ~60s, and I can't find an overriding timeout setting
Type: Bug Behaviour Expected vs. Actual Expected When debugging a pytest unittest using the inline right-click debug from a specific test, I can debug for as long as I want; i.e., the debugger stay... More on github.com
🌐 github.com
3
August 7, 2023
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
Pytest: stop on any failure in tests
When I’m debugging a test with VSCode, I’m expecting the debugger to automatically add a breakpoint at the line where the test is failing. This is the behavior of pytest with the option --pdb, so I think it’s reasonable to expect the sam... More on github.com
🌐 github.com
7
March 18, 2025
Can't debug tests in python
pytest ran `main.py` not `test_main.py`. On which current buffer did you run the command? I advise you to use https://github.com/nvim-neotest/neotest-python which will make dap + pytest integration much easier. More on reddit.com
🌐 r/neovim
6
1
November 16, 2023
🌐
GitHub
github.com › microsoft › debugpy › issues › 544
Can't debug my pytest unittests from VS Code · Issue #544 · microsoft/debugpy
February 12, 2021 - ============================= test session starts ============================= platform win32 -- Python 3.7.9, pytest-6.2.0, py-1.10.0, pluggy-0.13.1 rootdir: c:\dev\ingest_oba, configfile: obabr\pytest.ini plugins: hypothesis-6.0.3, forked-1.3.0, profiling-1.7.0, timeout-1.4.2, xdist-2.2.0 collected 1 item obabr\tests\download_test.py . [100%] - generated xml file: C:\Users\ppppp\AppData\Local\Temp\tmp-15396J3k1TtjEH4i3.xml - ============================== 1 passed in 0.13s ============================== And nothing happens. :-( If from my activated workspace in a _ bash shell_ I run the following command: python -m debugpy --wait-for-client --listen 0.0.0.0:5678 ../../Scripts/pytest.exe download_test.py
Author   pauloneves
🌐
Pyleecan
pyleecan.org › tests_tutorials › how.to.debug.with.pytest.html
Debugging with pytest — PYthon Library for Electrical Engineering Computational ANalysis
April 28, 2023 - def test_build_geometry_two_hole_with_magnet(self): """check that curve_list is correct (one hole)""" test_obj = LamHole(is_internal=True, is_stator=False, Rext=0.075) test_obj.hole = list() test_obj.hole.append( HoleM50( Zh=8, W0=50e-3, W1=2e-3, W2=1e-3, W3=1e-3, W4=20.6e-3, H0=17.3e-3, H1=1.25e-3, H2=0.5e-3, H3=6.8e-3, H4=1e-3, magnet_0=MagnetType10(Wmag=0.01, Hmag=0.02), ) ) a = 5 pytest.set_trace() result = test_obj.hole[0].build_geometry() assert len(result) == 6 for surf in result: assert type(surf) is SurfLine assert result[0].label[:5] == "Hole_" assert result[0].label[-9:] == "_R0_T0_S0" assert len(result[0].line_list) == 7
🌐
GitHub
github.com › microsoft › debugpy › issues › 1347
When debugging a pytest unittest in VSCode, the python debugger detaches after ~60s, and I can't find an overriding timeout setting · Issue #1347 · microsoft/debugpy
August 7, 2023 - Type: Bug Behaviour Expected vs. Actual Expected When debugging a pytest unittest using the inline right-click debug from a specific test, I can debug for as long as I want; i.e., the debugger stays connected and breaks on breakpoints, e...
Author   haughki
🌐
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",
          }
        }
🌐
SeleniumBase
seleniumbase.com › home › the ultimate pytest debugging guide
The ultimate pytest debugging guide - SeleniumBase
June 25, 2022 - The pytest unit-testing framework ... debugger (“Debug Mode”), which utilizes the following two libraries: the included pdb library, and the improved (but optional) ipdb library....
Find elsewhere
🌐
GitHub
github.com › microsoft › debugpy › issues › 1873
Pytest: stop on any failure in tests · Issue #1873 · microsoft/debugpy
March 18, 2025 - When I’m debugging a test with VSCode, I’m expecting the debugger to automatically add a breakpoint at the line where the test is failing. This is the behavior of pytest with the option --pdb, so I think it’s reasonable to expect the sam...
Author   k4nar
🌐
Towards Data Science
towardsdatascience.com › home › latest › pytest tutorial: an introduction to unit testing
Pytest Tutorial: An Introduction To Unit Testing | Towards Data Science
July 23, 2025 - Up to this point, we have only passed one test case for each test function. However, there may be multiple edge cases you want to test and verify. Pytest makes this process very easy through the parametrize decorator:
🌐
Reddit
reddit.com › r/neovim › can't debug tests in python
r/neovim on Reddit: Can't debug tests in python
November 16, 2023 -

I use nvim-dap with nvim-dap-python and nvim-dap-ui and I can't for the life of me make the debugger work with pytest.

.
├── pyrightconfig.json
├── pytest.ini
├── src
│   └── main
│       ├── __init__.py
│       ├── input.in
│       ├── main.py
│       └── tests
│           ├── __init__.py
│           └── test_main.py
└── venv

No custom configs except for keybindings. Using pytest in the terminal works normally as intended, but using nvim-dap-python's :lua require('dap-python').test_method() results to this:

============================= test session starts ==============================
platform linux -- Python 3.10.12, pytest-7.4.3, pluggy-1.3.0
rootdir: /home/user/path/to/my/project/
configfile: pytest.ini
collected 0 items                                     
                         

============================ no tests ran in 0.01s =============================
ERROR: not found: /home/user/path/to/my/project/src/main/main.py::reverse_matrix_column (no name '/home/user/path/to/my/project/src/main/main.py::reverse_matrix_column' in any of [<Module main.py>])


[Process exited 4]

What am I doing wrong? Please help!

🌐
Qxf2 BLOG
qxf2.com › home › debugging in python using pytest.set_trace()
Debugging in Python using pytest.set_trace() - Qxf2 BLOG
October 30, 2024 - pytest by default comes with Python debugger support and allows to open the debugger pdb prompt(via a command line option) at the start of a test or when there are test failures. You can use different command options like l (list), a(args), ...
🌐
Visual Studio Code
code.visualstudio.com › docs › python › testing
Python testing in Visual Studio Code
November 3, 2021 - The Python extension builds on the built-in testing features in VS Code and provides test discovery, test coverage, and running and debugging tests for Python's built-in unittest framework and pytest.
🌐
GitHub
github.com › microsoft › debugpy › issues › 1289
Pytest debug breakpoints not hit using python 3.11 · Issue #1289 · microsoft/debugpy
May 11, 2023 - Type: Bug Behaviour Running a pytest in debug mode no longer stops at breakpoints when using Python 3.11 (works in Python3.10). Expected vs. Actual Pytest running in debug mode should stop on break...
Published   May 11, 2023
Author   morybc
🌐
pytest
docs.pytest.org › en › stable › how-to › failures.html
How to handle test failures - pytest documentation
To stop the testing process after the first (N) failures: · Python comes with a builtin Python debugger called pdb. pytest allows one to drop into the pdb prompt via a command line option:
🌐
GitHub
github.com › microsoft › debugpy › issues › 1242
1.6.6: pytest is failing · Issue #1242 · microsoft/debugpy
March 16, 2023 - build is performed in env which is cut off from access to the public network (pytest is executed with -m "not network") I'm assuming that probably still sometjing is missing in build env of that package however I was unable to figure out what exactly it could be. May I ask for help/some hints? ... =========================== short test summary info ============================ SKIPPED [3] tests/debugpy/test_args.py:40: Shell expansion is not supported for "internalConsole" SKIPPED [5] tests/debugpy/test_multiproc.py:48: https://github.com/microsoft/ptvsd/issues/1887 SKIPPED [3] tests/debugpy/t
Author   kloczek
🌐
Capital One
capitalone.com › tech › software-engineering › how-to-use-python-debugger-pdb
How to use pdb with pytest: Debugging unit tests in Python
December 1, 2021 - If you run your tests with pytest --pdb, it will automatically drop into a debugger on every test that fails or has an error of some kind. If you love using the REPL, IPython or Jupyter in your normal work, then you know how handy it is to be in an interactive environment when trying to fix errors!
🌐
Reddit
reddit.com › r/vscode › debugpy env option in launch.json
launch.json の debugpy 環境オプション : r/vscode
March 7, 2024 -

In a previous post I asked how to how to make use two different pytest.ini files so that I could run in parallel with coverage when running but still hit breakpoints when I debug a single test. I figured out a way to do it but it only works for python mode not debugpy.

If I set the type to python this works perfectly but of course it is also marked as deprecated and scheduled to be removed. If I set this to debugpy the env options are never set.

Does anyone know of a way to add env options to debugpy so that this will keep working going forward or is this something that can be added to vscode?

{
"name": "Python: Debug Tests", 
"type": "python", 
"request": "launch", 
"program": "${file}", 
"purpose": ["debug-test"], 
"console": "integratedTerminal", 
"justMyCode": false, 
"env": { "PYTEST_ADDOPTS": "-c pytest_alt.ini",
}
}
🌐
JetBrains
jetbrains.com › help › pycharm › run-debug-configuration-py-test.html
Pytest | PyCharm Documentation
March 10, 2026 - Use this dialog to create a run/debug configuration for pytests.
🌐
GitHub
github.com › microsoft › debugpy › issues › 106
Cannot launch pytest due to existence of __pydevd_ret_val_dict in enum · Issue #106 · microsoft/debugpy
April 7, 2020 - mneilly@Docker:/local/mneilly/foo$ env DEBUGPY_LAUNCHER_PORT=34608 /usr/bin/python3 /eng/mneilly/.vscode-server/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/no_wheels/debugpy/launcher -m pytest -s --verbose /local/mneilly/foo/foo.py Traceback (most recent call last): File "/usr/lib64/python3.6/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code exec(code, run_globals) File "/eng/mneilly/.vscode-server/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/no_wheels/debugpy/__ma
Author   mneilly
🌐
pytest
docs.pytest.org › en › 7.4.x › how-to › failures.html
How to handle test failures — pytest documentation
To stop the testing process after the first (N) failures: · Python comes with a builtin Python debugger called pdb. pytest allows one to drop into the pdb prompt via a command line option: