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.

VS Code / Python / Debugging pytest Test with the Debugger - Stack Overflow
Debug of parametrized pytest fails to find test when using zsh shells
Debugging pytest fails
Python separate pytest config for running tests vs debugging
Videos
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 encountered the same issue and your post almost solves the problem. When I tried implementing your solution I encountered the following issue:
ImportError while loading conftest '/app/tests/conftest.py'.
tests/conftest.py:36: in <module>
ptvsd.enable_attach("secret_text", address=("0.0.0.0", 5678))
E TypeError: enable_attach() got multiple values for argument 'address'
Removing the "secret_text" value allowed me to hit the wait_for_attach() point and successfully attach the debugger to the code. I was able to hit breakpoints in my tests. Thank you!
.vscode/launch.json
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"localRoot": "${workspaceFolder}/path/to/code",
"remoteRoot": "/app",
"port": 5678,
"host": "localhost"
}
docker-compose.yml
ports:
- "5678:5678"
conftest.py
import ptvsd
ptvsd.enable_attach(address=("0.0.0.0", 5678))
ptvsd.wait_for_attach()
Note: The ptvsd lines are placed after all the imports.
CLI command to execute tests:
import subprocess
import click
@click.command()
def cli():
return subprocess.call("pytest test", shell=True)
Sequence to debug tests:
docker-compose up(get container running)docker-compose exec MODULE CONTAINER_NAME FUNCTION_THAT_EXECUTES_TEST- Attach debugger in VSCode
Tests will execute and hit whatever breakpoint you have setup.
I couldn't connect neither, even following https://github.com/microsoft/vscode-docker/issues/3946. What worked for me was after following @therightstuff answer, which I think it needs better explanation.
Steps to reproduce:
Go to VS code extensions and install Dev Containers from Microsoft.
In your VS code window, in the left-bottom corner there's the button >< in blue. Click it! (or green if you are connected to a remote host) (in this image the
is purple).This will open a contextual menu on top of VS Code. After installing the previous extension now you can click "Attach to Running Container". Click there.
VS Code will open a new window that is running within your docker. Now in this window you must install Python extension from Microsoft to enable debugging.
Go to debug panel in this window and add a new configuration for Python. Add the following:
{ "configurations": [ { "name": "Python: Pytest", "type": "python", "request": "launch", "module": "pytest", "args": [ "$file" ], }, ] }Now open the file you want to test, and click on the green triangle in the debug configuration with the name "Python: Pytest", which is the config you just added. If instead of running a unique file you want to run all tests or run a single test, in the previous config file put "your pytest arguments" instead of "$file" inside "args" option.
EDIT
Alternatively to step 5. and 6., you can open the folder .vscode (which will be added automatically, or if not create it) and create a new file called settings.json with the following code:
{
"python.testing.pytestEnabled": true,
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestArgs": [
"app/tests"
]
}
app/tests is where I have my tests, and for the rest of options, enable the kind of framework you use for testing (in my case that was only for pytest). Then go to menu View->Command Palette and type: Python: Configure Tests and click the option from the menu you'll see.
After this, vs code will detect all tests in that path, and you'll see a little green triangle next to every test definition so by clicking over you can run it. If instead of left click you press right click, there's an option for "debug test", which allows you to debug that test. Remember that, at least in the case of pytest all tests definitions must start by "test_". Otherwise vs code won't detect it!
» pip install pytest-vscodedebug