ModuleNotFoundError:使用 VS Code 测试选项卡运行测试时没有名为 src 的模块

问题描述 投票:0回答:1

我有一个具有以下(简化)文件夹结构的项目

- my_project/
  - src/
    - core/
      - core.py
      - __init__.py
    - other/
      - other.py
      - __init__.py
    - models/
      - myModel.py
      - __init__.py
  - test/
    - unit/
      - test_core.py
      - __init__.py
    - integration/
      - test_all.py
      - __init__.py

添加了所有

__init__.py
文件以尝试解决该问题。 test_all.py 和 test_core.py 都有
from src.models.myModel import myModel
因为我在测试中需要 myModel 。 我可以通过在终端中运行
python -m pytest
来运行所有测试(test_core 和 test_all)。测试全部执行,我收到成功和失败的报告。
我的一个测试无法正常工作,所以我想对其进行调试。为此,我尝试使用 VS Code 中的测试选项卡。它要求我“配置 python 测试,我在其中选择以下选项:测试框架:pytest,包含测试的目录:test。这会生成以下 settings.json 文件:

my_project

然后我在测试选项卡中收到以下错误:

{ "python.testing.unittestArgs": [ "-v", "-s", "./test", "-p", "test_*.py" ], "python.testing.pytestEnabled": true, "python.testing.unittestEnabled": false, "python.testing.pytestArgs": [ "test" ] }

为什么
______ ERROR collecting test/integration/test_all.py ______ ImportError while importing test module 'path/to/test_all.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: C:\Python310\lib\importlib\__init__.py:126: in import_module return _bootstrap._gcd_import(name[level:], package, level) test\integration\test_all.py:4: in <module> from src.models.myModel import myModel E ModuleNotFoundError: No module named 'src' ______________ ERROR collecting test/unit/test_core.py ______________ ImportError while importing test module 'path/to/test_core.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: C:\Python310\lib\importlib\__init__.py:126: in import_module return _bootstrap._gcd_import(name[level:], package, level) test\unit\test_core.py:2: in <module> from src.models.myModel import myModel E ModuleNotFoundError: No module named 'src' =========================== short test summary info =========================== ERROR test/integration/test_all.py ERROR test/unit/test_core.py !!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!! ==================== no tests collected, 2 errors in 0.20s ==================== Traceback (most recent call last): File "c:\Users\Domin\.vscode\extensions\ms-python.python-2023.10.1\pythonFiles\testing_tools\run_adapter.py", line 22, in <module> main(tool, cmd, subargs, toolargs) File "c:\Users\Domin\.vscode\extensions\ms-python.python-2023.10.1\pythonFiles\testing_tools\adapter\__main__.py", line 99, in main parents, result = run(toolargs, **subargs) File "c:\Users\Domin\.vscode\extensions\ms-python.python-2023.10.1\pythonFiles\testing_tools\adapter\pytest\_discovery.py", line 47, in discover raise Exception("pytest discovery failed (exit code {})".format(ec)) Exception: pytest discovery failed (exit code 2) at ChildProcess.<anonymous> (c:\Users\Domin\.vscode\extensions\ms-python.python-2023.10.1\out\client\extension.js:2:241783) at Object.onceWrapper (node:events:628:26) at ChildProcess.emit (node:events:513:28) at maybeClose (node:internal/child_process:1121:16) at ChildProcess._handle.onexit (node:internal/child_process:304:5)]

我很感激我能得到的所有帮助,因为我已经被这个问题困扰了一段时间了。

    

python pytest python-unittest directory-structure
1个回答
1
投票
python -m pytest work, but the testing tab in VS Code not?

中添加空文件

__init__.py
,这样
这个文件夹才能作为包受到威胁
我不确定添加这个文件是否足以解决 VS Code 的问题,但在我的系统中如果我执行以下命令:
src

没有
> cd /path/to/my_project > python -m pytest

中的

__init__.py
文件,使用pytest执行的输出是:
src

所以你的文件夹结构必须变成:

========================= test session starts ========================= platform linux2 -- Python 2.7.17, pytest-4.6.11, py-1.9.0, pluggy-0.13.1 rootdir: /path/to/my_project collected 0 items / 2 errors ========================= ERRORS ========================= _________________ ERROR collecting test/integrate/test_all.py __________________ ImportError while importing test module '/path/to/my_project/test/integrate/test_all.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: test/integrate/test_all.py:2: in <module> from src.models.myModel import myModel E ImportError: No module named src.models.myModel ___________________ ERROR collecting test/unit/test_core.py ____________________ ImportError while importing test module '/path/to/my_project/test/unit/test_core.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: test/unit/test_core.py:2: in <module> from src.models.myModel import myModel E ImportError: No module named src.models.myModel !!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!

通过添加 
- my_project/ - src/ - __init__.py <----- add this file - core/ - core.py - __init__.py - other/ - other.py - __init__.py - models/ - myModel.py - __init__.py - test/ - unit/ - test_core.py - __init__.py - integration/ - test_all.py - __init__.py

文件,pytest 执行具有以下输出:

__init__.py

这篇文章

与同一主题相关,可能是对您来说另一个有用的示例。


有关 PyCharm 和 VSC 的注意事项

我使用 PyCharm 作为 Python 的 IDE,使用此 IDE,我需要将文件夹 ========================= test session starts ========================= platform linux2 -- Python 2.7.17, pytest-4.6.11, py-1.9.0, pluggy-0.13.1 rootdir: /path/to/my_project collected 2 items test/integrate/test_all.py . [50%] test/unit/test_core.py .
添加到

my_project
(通过项目的“设置”菜单),以便能够在
Project Structure
test_all.py
中执行测试没有导入错误。
我不知道这个答案对你来说是否足够,但很抱歉:我无法通过 VS Code 在我的系统中测试你的代码。

© www.soinside.com 2019 - 2024. All rights reserved.