【问题标题】:VS Code no Python unittest tests discoveredVS Code 没有发现 Python 单元测试测试
【发布时间】:2020-09-05 17:35:47
【问题描述】:

有很多类似的问题(例如123456,...),但没有一个问题完全组合在一起(即 unittest 发现在 shell 提示符下工作),这些问题的解决方案对我不起作用。

当我在 Visual Studio Code 中运行 Python: Discover Tests 时出现错误

未发现测试,请检查测试的配置设置。

我的 settings.json 文件如下所示

{
    "python.testing.unittestArgs": [
        "--verbose",
        "--start-directory",
        "./preprocessing/",
        "--pattern",
        "test_*.py"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": true
}

但我知道单元测试是可发现的,因为如果我从项目的根目录运行此命令,所有测试都会被发现并运行:

python -m unittest discover --verbose --start-directory "./preprocessing/" --pattern "test_*.py"

我的测试显然可以被 Python 的 unittest 框架发现,但 VS Code 没有找到它们。我该如何解决这个问题?

【问题讨论】:

  • 首先,注释所有导入语句并发现测试。如果可行,请检查您的导入语句。

标签: python visual-studio-code python-unittest


【解决方案1】:

我会尝试在没有--verbose 的情况下运行。这不是必需的,因为扩展程序正在为您进行发现,而且我敢打赌它的额外输出会破坏某些东西。

【讨论】:

  • 感谢您的建议,虽然没用,但值得一试。看起来@alexzshl 的建议是正确的,即这是我的模块导入
【解决方案2】:

您需要在项目根目录的 .env 文件中设置 PYTHONPATH。确保所有包含 src 文件和测试的目录都在 pythonpath 上。 详情see here

【讨论】:

    猜你喜欢
    • 2019-07-17
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 2011-03-18
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多