VS Code 未找到 pytest 测试

问题描述 投票:0回答: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 查找测试的步骤?

python python-3.x visual-studio-code pytest pytest-django
11个回答
25
投票

如果有人在 2020 年后遇到这个问题,vscode-python 存储库中的

这个问题
救了我的命。基本上,只需执行以下操作:

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

工作起来很有魅力。


6
投票

我的 Python 插件和测试资源管理器工作得很好。

在我的例子中,命名没有

test_.py
的类是问题所在。换句话说,命名测试文件时不以
"test_"
开头,而不是
"tests_"
开头,这样资源管理器就看不到问题。例如:
test_.py
有效,但
tests_.py
12345abcde.py
不起作用。


6
投票

VScode需要指定python配置:

  1. 对于 Mac 用户,通过
    Ctrl
    +
    shift
    +
    p
    command
    +
    shift
    +
    p
    打开命令面板。
  2. 并写下这个
    >python: configure tests
    并按照程序说明进行操作

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


5
投票
如果 vscode 无法发现测试,需要检查的另一件事是确保它不会因为启用了

coverage 模块而这样做。在我的例子中,测试用例被正确发现,但由于测试覆盖率低,发现最终不断失败,如here所述。因此,首先,请确保pytest

确实可以按照
此处的建议收集测试:

pytest --collect-only

然后确保您没有强制进行覆盖范围检查(例如在

setup.cfg

中),例如

addopts= --cov <path> -ra
    

5
投票
2021 年 9 月,我通过将 VS Code Python 扩展从版本 2021.9.1191016588 降级到版本 v2021.8.1159798656,能够让 VS code 再次找到测试目录。要降级,请右键单击扩展,然后单击“安装其他版本...”


4
投票

编辑:阅读完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
 可以。


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

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


3
投票
一件明显的事情吸引了我......

测试文件夹层次结构需要每个文件夹中有

__init__.py

 个文件。

我重构了我的测试,引入了额外的文件夹层,但忘记添加

__init__.py

 文件。
命令行没有抱怨,但 vscode 显示没有找到测试


2
投票
就我而言,问题是源文件中存在语法错误,导致 pytest 发现失败。另请注意,您可以从

Output

(选项卡)-> 
Python
(下拉菜单)获得回溯。


0
投票
如果这里的一切都失败并且您检查了所有基本配置,请尝试在工作区的

settings.ini

 文件中指定测试文件夹的路径,如下所示。下面的三个配置来自大多数其他解决方案。

"python.testing.pytestArgs": [ "${workspaceFolder}/tests" ], "python.testing.unittestEnabled": false, "python.testing.nosetestsEnabled": false, "python.testing.pytestEnabled": true,
    

0
投票
我变了

“python.testing.pytestEnabled”:true

在 .vscode 上设置为 false 并重新加载 VScode,并且在尝试运行测试之前,他们通过使用 python 上的命令板选择了正确的 python 解释:选择解释器,并且它工作正常。

因此,当我尝试在扩展上运行测试时,在 VScode 中选择的解释几乎是错误的。

另外,我正在使用 conda 环境,这可能是相关的。

© www.soinside.com 2019 - 2024. All rights reserved.