Debug Test in vscode fails inside container

Viewed 601

Configuration:

I am using the following version of VSCode:

Version: 1.46.1
Commit: cd9ea6488829f560dc949a8b2fb789f3cdc05f5d
Date: 2020-06-17T21:13:08.304Z
Electron: 7.3.1
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Linux x64 5.3.0-59-generic snap

With the Python Extension ms-python.python version v2020.6.8. The only PIPs installed are:

Package           Version
----------------- -------
astroid           2.4.2
attrs             19.3.0
autopep8          1.5.3
isort             4.3.21
lazy-object-proxy 1.4.3
mccabe            0.6.1
more-itertools    8.4.0
packaging         20.4
pip               20.1.1
pluggy            0.13.1
py                1.8.2
pycodestyle       2.6.0
pylint            2.5.3
pyparsing         2.4.7
pytest            5.4.3
setuptools        47.1.1
six               1.15.0
toml              0.10.1
wcwidth           0.2.4
wheel             0.34.2
wrapt             1.12.1

Problem:

I am trying to debug my python tests in vscode and i get the following error in the debug console:

============================= test session starts ==============================
platform linux -- Python 3.8.3, pytest-5.4.3, py-1.8.2, pluggy-0.13.1
rootdir: /workspaces/test_python_debugging
collected 0 items / 1 error

==================================== ERRORS ====================================
_________________ ERROR collecting tst/dummy/test_unit_math.py _________________
ImportError while importing test module '/workspaces/test_python_debugging/tst/dummy/test_unit_math.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tst/dummy/test_unit_math.py:5: in <module>
    from dummy.math import add
E   ModuleNotFoundError: No module named 'dummy'
--------------- generated xml file: /tmp/tmp-188wLObs4r6II9x.xml ---------------
=========================== short test summary info ============================
ERROR tst/dummy/test_unit_math.py
=============================== 1 error in 0.19s ===============================
ERROR: not found: /workspaces/test_python_debugging/tst/dummy/test_unit_math.py::test_addition
(no name '/workspaces/test_python_debugging/tst/dummy/test_unit_math.py::test_addition' in any of [<Module tst/dummy/test_unit_math.py>])

The tests themselves run fine in the terminal. In other terms there is no issue with not being able to find the "missing" module. Example:

0f617e2a72ee [/workspaces/test_python_debugging]$ python -m pytest tst/**/te
st*.py -k test_addition
=========================== test session starts ============================
platform linux -- Python 3.8.3, pytest-5.4.3, py-1.8.2, pluggy-0.13.1
rootdir: /workspaces/test_python_debugging
collected 1 item                                                           

tst/dummy/test_unit_math.py .                                        [100%]

============================ 1 passed in 0.01s =============================
0f617e2a72ee [/workspaces/test_python_debugging]$ 

The way I am launching the debugger is by clicking on the Debug Test link for the given test. Example:

Launching debugger on test

Below is the .devcontainer.json configuration being used:

{
  "image": "test_python_debugging",
  "extensions": [
    "ms-python.python"
  ],
  "mounts": [
    "source=${localWorkspaceFolder},target=/app,type=bind,consistency=cached"
  ],
  "settings": {
    "terminal.integrated.shell.linux": "/bin/ash",
    "terminal.integrated.shellArgs.linux": [
      "-l"
    ],
    "python.testing.pytestArgs": [
      "tst",
      "-k test_unit"
    ],
    "python.testing.unittestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.pytestEnabled": true,
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true,
    "python.pythonPath": "/usr/local/bin/python"
  },
  "containerUser": "app",
  "containerEnv": {
    "PYTHONPATH": "/app/src"
  }
}

Would someone know what I am missing in terms of configurations?

2 Answers

You need to specify your PYTHONPATH appropriately for using src/. Your .devcontainer.json puts the directory as /app but your traceback in your tests suggests the code lives in /workspaces/test_python_debugging.

Had the same problem and this fixed it.

In settings.json in VScode remove the argument "app" from python.testing.pytestArgs as below.

Also set an ignore on any directories that may have issues with tests that you wish to ignore as below.

{
    "python.testing.pytestArgs": [
        "--ignore=BadFolder"
    ],
    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true
}
Related