【问题标题】:VS Code not finding pytest testsVS Code 找不到 pytest 测试
【发布时间】:2019-06-20 14:19:22
【问题描述】:

我在 vs-code 中设置了 PyTest,但即使从命令行运行 pytest 工作正常,也没有找到任何测试。

(我正在使用 MiniConda 和 Python 3.6.6 虚拟环境在 Win10 上开发 Django 应用程序。VS Code 已完全更新,我安装了 Python 和 Chrome 扩展调试器)

Pytest.ini:

[pytest]
DJANGO_SETTINGS_MODULE = callsign.settings
python_files = tests.py test_*.py *_tests.py

vs-code 工作区设置:

{
    "folders": [
        {
            "path": "."
        }
    ],
    "settings": {
        "python.pythonPath": "C:\\ProgramData\\Miniconda3\\envs\\callsign\\python.exe",
        "python.unitTest.unittestEnabled": false,
        "python.unitTest.nosetestsEnabled": false,
        "python.unitTest.pyTestEnabled": true,
        "python.unitTest.pyTestArgs": ["--rootdir=.\\callsign", "--verbose"]
    }
}

最后,VS 代码中 Python 测试日志的输出:

============================= test session starts =============================
platform win32 -- Python 3.6.6, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
Django settings: callsign.settings (from ini file)
rootdir: c:\Users\benhe\Projects\CallsignCopilot\callsign, inifile: pytest.ini
plugins: django-3.4.5
collected 23 items
<Package c:\Users\benhe\Projects\CallsignCopilot\callsign\transcription>
  <Module test_utils.py>
    <Function test_n_digits>
    <Function test_n_alpha>
    <Function test_n_hex>
    <Function test_n_digits_in_range>
    <Function test_v1_audiofilename>
    <Function test_v2_audiofilename>
    <Function test_v1_bad_int_filename>
    <Function test_v1_bad_non_int_filename>
    <Function test_bad_format>
    <Function test_no_format>
    <Function test_too_many_segments>
    <Function test_too_few_segments>
    <Function test_good_v2_filename>
    <Function test_bad_year_v2_filename>
    <Function test_bad_month_v2_filename>
    <Function test_bad_day_v2_filename>
    <Function test_bad_date_v2_filename>
    <Function test_bad_short_serial_v2_filename>
    <Function test_bad_long_serial_v2_filename>
    <Function test_good_v3_filename>
    <Function test_good_lowercase_block_v3_filename>
    <Function test_bad_non_alpha_block_v3_filename>
    <Function test_real_filenames>

======================== no tests ran in 1.12 seconds =========================

我是否遗漏了获取 vs-code 以查找测试的任何步骤?

【问题讨论】:

    标签: python python-3.x visual-studio-code pytest pytest-django


    【解决方案1】:

    编辑:我在阅读 issue 3911 后降级到 Pytest 4.0.1,现在可以使用测试发现。


    我也是。当我吹走.pytest_cache 并重新运行Python: Discover Unit Tests 时,我看到新生成的.pytest_cache/v/cache/nodeids 包含所有测试,但我仍然收到抱怨No tests discovered 的对话框。

    • Python 3.7.2
    • macOS 10.13.6
    • VS 代码 1.30.2
    • Python 扩展 2018.12.1
    • Pytest 4.1.0

    .vscode/settings.json:

    {
        "python.linting.enabled": false,
        "python.unitTest.unittestEnabled": false,
        "python.unitTest.nosetestsEnabled": false,
        "python.unitTest.pyTestEnabled": true,
        "python.pythonPath": "venv3/bin/python"
    }
    

    测试位于名为test 的顶级子目录中。手动运行pytest 即可。

    【讨论】:

    • 重新运行 Python:发现测试后,测试是否会显示在测试资源管理器中?降级后还是什么都看不到。
    • 10 月 9 日更新:您应该使用:json "python.testing.unittestEnabled": false, "python.testing.nosetestsEnabled": false, "python.testing.pytestEnabled": true,
    【解决方案2】:

    如果有人在 2020 年遇到这种情况,this issue 存储库中的 vscode-python 救了我的命。基本上,只需执行以下操作:

    1. 卸载Python 扩展
    2. 从您的~/.vscode 文件夹中删除包含扩展名的文件(我的看起来像ms-python.python-[YEAR].[MONTH].[VERSION]
    3. 重新安装扩展

    工作就像一个魅力。

    【讨论】:

    • @Peter 您是否在卸载/删除后重新加载了 VSCode 窗口并在重新安装后再次加载?
    【解决方案3】:

    我的 Python 插件和测试资源管理器运行良好。

    在我的例子中,没有test_.py 的类命名是个问题。换句话说,以"test_" 而不是"tests_" 开头的不带它的测试文件命名,使得资源管理器看不到问题。例如:test_.py 有效,但 tests_.py12345abcde.py 无效。

    【讨论】:

    • 呃,终于,有些东西奏效了。我通常test_,但刚刚发胖了。谢谢!!!
    【解决方案4】:

    如果 vscode 未能发现测试,要检查的另一件事是确保它不会因为启用了 coverage 模块而这样做。在我的情况下,测试用例被正确发现,但由于测试覆盖率低,发现最终一直失败,如here 所述。所以首先,确保测试实际上可以由pytest 收集,正如here 建议的那样:

    pytest --collect-only
    

    然后确保你没有强制覆盖检查(例如在setup.cfg中),例如

    addopts= --cov <path> -ra
    

    【讨论】:

    • 您先生,为我节省了很多时间!谢谢!
    • 我有一个语法错误 - “预期缩进”或类似的(我有一个悬空的 if __name__ == "__main__": 语句)。在我运行pytest --collect-only 之前,该错误似乎没有出现(尽管如果我只是运行pytest 而不是单击VS Code 按钮,它可能会出现)。谢谢!
    【解决方案5】:

    2021 年 9 月,通过将 VS Code Python 扩展从 2021.9.1191016588 版本降级到 v2021.8.1159798656 版本,我能够再次获得 VS Code 以找到测试目录。要降级,请右键单击扩展并单击“安装另一个版本...”

    【讨论】:

    • 这在最新版本的 Visual Studio 上对我有用。
    • 也为我工作,现在是最新的 v2021.10.1336267007。谢谢! (不知道原因是什么,在任何地方都看不到使用新扩展版本记录的任何错误,就像项目没有测试一样,而pytest 命令行发现并运行测试就好了。)
    【解决方案6】:

    以下步骤对我有用(假设 Visual Studio Code 中安装了最新的 PyTest):

    1. 确保没有错误(Visual Studio Code 的左下角)
    2. 在 Visual Studio Code 中的 File 菜单中选择:File > Close Folder(即关闭当前项目文件夹)并关闭 Visual Studio Code。
    3. 重新打开 Visual Studio Code 并选择:文件 > 打开文件夹(重新打开包含项目/测试文件的文件夹)。
    4. 选择测试,应该可以从这里看到测试...

    【讨论】:

      【解决方案7】:

      VScode需要指定python配置:

      1. 通过Ctrl + shift + p 打开命令面板
      2. 写下这个&gt;python: configure tests 并按照程序说明进行操作

      注意:在执行前面的步骤之前,您可能需要(大多数情况下不需要)先执行this

      【讨论】:

        猜你喜欢
        • 2023-01-27
        • 2021-11-27
        • 2020-12-16
        • 1970-01-01
        • 1970-01-01
        • 2018-05-31
        • 2021-05-06
        • 1970-01-01
        相关资源
        最近更新 更多