14

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.

3
  • were you ever able to make any progress on this task? Commented Jan 10, 2019 at 0:10
  • 1
    nope, I just ran them in local env in the end :( Commented Jan 10, 2019 at 12:58
  • even, i am facing a lot of trouble doing this, i tired both in pycharm and vscode Commented Jun 7, 2019 at 12:55

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Where are you placing the CLI command to execute tests?
3

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:

  1. Go to VS code extensions and install Dev Containers from Microsoft.

  2. 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 Button is purple).

  3. 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.

  4. 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.

  5. 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"
       ],
       },
     ]
    }
    
  6. 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!

1 Comment

I had an issue with that configuration. It was not working for me unless I added "version": "0.2.0", before "configurations":
2

I came across this question before discovering that Visual Studio Code has had built-in-support for remote debugging docker containers since 2019!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.