【问题标题】:Getting empty test suite when running with pytest from pycharm从 pycharm 使用 pytest 运行时获取空的测试套件
【发布时间】:2018-08-28 07:12:00
【问题描述】:

我在从 PyCharm 内部运行 pytest 测试时遇到问题

我在 C:\Temp 的名为 agent_automation_2 的文件夹中有一个文件 x_tests.py,该文件的内容是

import pytest
def test_mytest():
    assert  False

当我运行时,我得到以下输出

C:\Python36-32\python.exe "C:\Program Files\JetBrains\PyCharm 2017.3.2\helpers\pycharm_jb_pytest_runner.py" --path C:/Temp/agents_automation_2 在 C:\Temp\agents_automation_2 中使用参数 C:/Temp/agents_automation_2 启动 py.test ============================= 测试开始================== =========== 平台 win32 -- Python 3.6.4、pytest-3.4.2、py-1.5.2、pluggy-0.6.0 根目录:C:\Temp\agents_automation_2,inifile: 插件:xdist-1.22.0,forked-0.2 已收集 0 件商品 ======================== 0.01 秒内没有运行测试 ==================== =====

但是,当我从文件夹内的常规 Windows 命令行运行时,测试运行正常

知道可能是什么问题吗?

谢谢!!!

【问题讨论】:

    标签: python-3.x pycharm


    【解决方案1】:

    您的测试文件的名称不符合默认的 pytest 发现规则。如果您将 x_tests.py 更改为 x_test.py,它将运行测试并按预期失败。请查看this page,了解有关 pytest 发现规则的更多信息。

    【讨论】:

    • 另外,在要测试的文件名中,它可以是 test_*.py 或 *_test.py。两种约定都是正确的。此外,测试函数有两点,即 1. 测试类之外的测试前缀测试函数或方法 2. 测试前缀测试类中的测试前缀测试函数或方法(没有 init 方法)
    【解决方案2】:

    确保将 PyCharm 配置为使用 PyTest。默认情况下,它使用单元测试。查看此文档:https://www.jetbrains.com/help/pycharm/choosing-your-testing-framework.html。正确配置 PyCharm 以使用 PyTest 后,例如,当您打开测试文件的上下文菜单时,您会看到类似 Run 'PyTest in ... '

    【讨论】:

      【解决方案3】:

      就我而言,我只需从项目中删除 pytest.ini 文件。

      【讨论】:

        【解决方案4】:

        对于使用 WSL 的用户,问题可能与 WSL 无法访问有关。 如果您在 WSL 上收到此错误,

        Logon failure: the user has not been granted the requested logon type at this computer.

        尝试在 PowerShell 中重新启动 vmcompute 服务(以管理员身份运行):

        powershell restart-service vmcompute
        

        来源:https://github.com/microsoft/WSL/issues/5401

        【讨论】:

          【解决方案5】:

          在我的例子中,测试类包含 init 方法,也会导致空套件。您需要做的是避免编写init方法。

          【讨论】:

          • 为了清楚起见,您能否重写/编辑您的答案?
          猜你喜欢
          • 1970-01-01
          • 2020-04-25
          • 2021-10-20
          • 2023-02-11
          • 1970-01-01
          • 1970-01-01
          • 2021-09-30
          • 2015-08-14
          • 1970-01-01
          相关资源
          最近更新 更多