dockervisual-studio-codepytestvscode-debugger

Debug Pytest in docker container using VS Code


I have troubles to setup debugging of py.test code in docker container using VS Code.

After studying this: https://code.visualstudio.com/docs/python/debugging And this: How to remote debug python code in a Docker Container with VS Code

I have setup following debug configuration in vscode:

{
  "name": "Python: Attach",
  "type": "python",
  "request": "attach",
  "localRoot": "${workspaceFolder}",
  "remoteRoot": "/capi",
  "port": 3000,
  "secret": "secret_text",
  "host": "localhost"
}

I have imported this bit into my test file:

import ptvsd
ptvsd.enable_attach("secret_text", address = ('0.0.0.0', 3000))
ptvsd.wait_for_attach()

And I made sure I open that 3000 port in docker-compose file:

 ports:
      - 3000:3000

I double checked that the port is open:

nmap -p 3000 localhost

Starting Nmap 7.60 ( https://nmap.org ) at 2018-07-19 10:53 CEST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000074s latency).

PORT     STATE SERVICE
3000/tcp open  ppp

Nmap done: 1 IP address (1 host up) scanned in 0.11 seconds

It seems to be the case. When I run pytest file from the container it starts and waits for debugger to be connected:

===================================================== test session starts =====================================================
platform linux2 -- Python 2.7.15, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: /capi, inifile:
plugins: requests-mock-1.5.0, xdist-1.14, metadata-1.7.0, html-1.16.1, cov-2.5.1
collecting 0 items  

But when I run this configuration from VS Code nothing seems to happen.

enter image description here It seems to hang. Nothing in the debug console or in the docker container.

I have setup remote debug for a simple hello-world.py console app just for testing and it seems to work. So my assumption is it has something to do with the fact that I'm trying to debug a pytest.

Have anyone managed to do this? I would appreciate some help.


Solution

  • 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:

    1. docker-compose up (get container running)
    2. docker-compose exec MODULE CONTAINER_NAME FUNCTION_THAT_EXECUTES_TEST
    3. Attach debugger in VSCode

    Tests will execute and hit whatever breakpoint you have setup.