我在 vs-code 中设置了 PyTest,但是即使从命令行运行 pytest 工作正常,也没有找到任何测试。
(我正在使用 MiniConda 和 Python 3.6.6 虚拟环境在 Win10 上开发 Django 应用程序。VS Code 已完全更新,并且我安装了 Python 和用于 Chrome 扩展的调试器)
测试.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 代码找到测试的步骤?
原文由 Ben 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果有人在 2020 年后遇到 这个问题,那么
vscode-python
存储库中的这个问题救了我的命。基本上,只需执行以下操作:Python
扩展~/.vscode
文件夹中删除包含扩展名的文件(我的看起来像ms-python.python-[YEAR].[MONTH].[VERSION]
)工作起来很有魅力。