我在 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 年后遇到这个问题,vscode-python
存储库中的
这个问题救了我的命。基本上,只需执行以下操作:
Python
扩展~/.vscode
文件夹中删除包含扩展名的文件(我的看起来像ms-python.python-[YEAR].[MONTH].[VERSION]
)工作起来很有魅力。
我的 Python 插件和测试资源管理器工作得很好。
在我的例子中,命名没有
test_.py
的类是问题所在。换句话说,命名测试文件时不以 "test_"
开头,而不是 "tests_"
开头,这样资源管理器就看不到问题。例如:test_.py
有效,但 tests_.py
、12345abcde.py
不起作用。
VScode需要指定python配置:
Ctrl
+ shift
+ p
或 command
+ shift
+ p
打开命令面板。>python: configure tests
并按照程序说明进行操作注意:在执行前面的步骤之前,您可能需要(大多数情况下不需要)先执行此操作
coverage 模块而这样做。在我的例子中,测试用例被正确发现,但由于测试覆盖率低,发现最终不断失败,如here所述。因此,首先,请确保pytest
确实可以按照此处的建议收集测试:
pytest --collect-only
然后确保您没有强制进行覆盖范围检查(例如在
setup.cfg
中),例如
addopts= --cov <path> -ra
编辑:阅读完issue 3911后,我降级到 Pytest 4.0.1,测试发现现在可以工作了。
.pytest_cache
并重新运行
Python: Discover Unit Tests
时,我看到新生成的
.pytest_cache/v/cache/nodeids
包含所有测试,但我仍然收到抱怨
No tests discovered
的对话框。
.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
可以。
测试文件夹层次结构需要每个文件夹中有
__init__.py
个文件。我重构了我的测试,引入了额外的文件夹层,但忘记添加
__init__.py
文件。命令行没有抱怨,但 vscode 显示没有找到测试
Output
(选项卡)->
Python
(下拉菜单)获得回溯。
settings.ini
文件中指定测试文件夹的路径,如下所示。下面的三个配置来自大多数其他解决方案。
"python.testing.pytestArgs": [
"${workspaceFolder}/tests"
],
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true,
“python.testing.pytestEnabled”:true在 .vscode 上设置为 false 并重新加载 VScode,并且在尝试运行测试之前,他们通过使用 python 上的命令板选择了正确的 python 解释:选择解释器,并且它工作正常。
因此,当我尝试在扩展上运行测试时,在 VScode 中选择的解释几乎是错误的。
另外,我正在使用 conda 环境,这可能是相关的。