vscode中的调试测试在容器内失败

2024-05-05 08:59:39 发布

您现在位置:Python中文网/ 问答频道 /正文

配置:

我正在使用以下版本的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

使用Python扩展ms-python.python版本v2020.6.8。仅安装了以下PIP:

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

问题:

我试图在vscode中调试python测试,在调试控制台中出现以下错误:

============================= 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>])

测试本身在终端中运行良好。换句话说,找不到“缺失”模块没有问题。例如:

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]$ 

启动调试器的方法是单击给定测试的Debug Test链接。例如:

Launching debugger on test

下面是正在使用的.devcontainer.json配置:

{
  "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"
  }
}

有人知道我在配置方面缺少什么吗


Tags: inpytestpytestlinuxuniterrormath