Ben*_*Ben 7 python pytest python-3.x visual-studio-code pytest-django
我在 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
Run Code Online (Sandbox Code Playgroud)
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"]
}
}
Run Code Online (Sandbox Code Playgroud)
最后,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 =========================
Run Code Online (Sandbox Code Playgroud)
我是否缺少让 vs-code 找到测试的任何步骤?
小智 6
我的 Python 插件和测试资源管理器工作得很好。
就我而言,命名没有的类test_.py是问题所在。换句话说,命名测试文件时不以"test_", not"tests_"开头,这样浏览器就看不到问题。例如:test_.py有效,但是tests_.py,12345abcde.py不起作用。
另一件需要检查的事情是,如果 vscode 无法发现测试,请确保它不会因为启用了覆盖模块而这样做。就我而言,测试用例被正确发现,但由于测试覆盖率低,发现最终不断失败,如此处所述。因此,首先,确保测试实际上可以按照此处的pytest建议收集:
pytest --collect-only
Run Code Online (Sandbox Code Playgroud)
然后确保您没有强制进行覆盖检查(例如在 中setup.cfg),例如
addopts= --cov <path> -ra
Run Code Online (Sandbox Code Playgroud)
小智 5
2021 年 9 月,我通过将 VS Code Python 扩展从版本 2021.9.1191016588 降级到版本 v2021.8.1159798656,能够让 VS code 再次找到测试目录。要降级,请右键单击扩展,然后单击“安装另一个版本...”
| 归档时间: |
|
| 查看次数: |
15864 次 |
| 最近记录: |