VS Code 找不到 pytest 测试

2024-01-11

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

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

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 code 内的 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 查找测试的步骤?


如果有人在2020年后遇到这个问题,这个问题 https://github.com/microsoft/vscode-python/issues/8184 in the vscode-python回购救了我的命。基本上,只需执行以下操作:

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

工作起来就像一个魅力。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

VS Code 找不到 pytest 测试 的相关文章