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.

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.
it's real simple: put an assert 0 where you want to start debugging in your code and run your tests with:
py.test --pdb
done :)
Alternatively, if you are using pytest-2.0.1 or above, there also is the pytest.set_trace() helper which you can put anywhere in your test code. Here are the docs. It will take care to internally disable capturing before sending you to the pdb debugger command-line.
I found that I can run py.test with capture disabled, then use pdb.set_trace() as usual.
> py.test --capture=no
============================= test session starts ==============================
platform linux2 -- Python 2.5.2 -- pytest-1.3.3
test path 1: project/lib/test/test_facet.py
project/lib/test/test_facet.py ...> /home/jaraco/projects/project/lib/functions.py(158)do_something()
-> code_about_to_run('')
(Pdb)
When debugging a pytest unittest in VSCode, the python debugger detaches after ~60s, and I can't find an overriding timeout setting
Python separate pytest config for running tests vs debugging
Pytest: stop on any failure in tests
Can't debug tests in python
Videos
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 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!
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",
}
}