ModulenotFoundError试图在VS代码上配置单元测试

发布于 2025-02-10 17:35:51 字数 1666 浏览 0 评论 0原文

我在同一目录中为模块添加了一个模块和一个测试文件,并希望从VS代码运行测试。但是,在配置VS代码以使用Python 3.8.3(Pyenv)并使用UniteSts时,测试资源管理器中存在错误,该错误指出错误发现UNITSEST测试的错误(请参阅输出> Python)

并在输出中Python

Failed to import test module: shared-scripts.utils.run_command_test
Traceback (most recent call last):
  File "/Users/username/.pyenv/versions/3.8.13/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/Users/username/.pyenv/versions/3.8.13/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/file_path/shared-scripts/utils/run_command_test.py", line 2, in <module>
    import run_command
ModuleNotFoundError: No module named 'run_command'

操作系统:MACOS 12.4(蒙特雷),Visual Studio代码:版本:1.68.1

启动。JSON

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}

设置。JSON

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

这在Pycharm Community Edition中正常工作,我不确定如何在VS代码上使用此功能。有什么建议吗?

I have added a module and a test file for the module in the same directory and want to run tests from VS Code. However, upon configuring VS Code to use Python 3.8.3 (pyenv) and to use unittests there is an error in the Test Explorer that states Error discovering unittest tests (see Output > Python).

And in Output > Python

Failed to import test module: shared-scripts.utils.run_command_test
Traceback (most recent call last):
  File "/Users/username/.pyenv/versions/3.8.13/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/Users/username/.pyenv/versions/3.8.13/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/file_path/shared-scripts/utils/run_command_test.py", line 2, in <module>
    import run_command
ModuleNotFoundError: No module named 'run_command'

Operating System: MacOS 12.4 (Monterey), Visual Studio Code: Version: 1.68.1

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}

settings.json

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

This works fine in PyCharm Community Edition and I am unsure on how to get this working on VS Code. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文