python runtime_error ai_generated true

pytest.fail() 被调用但未提供消息;请使用 pytest.fail(reason='...')

pytest.fail() called without a message; use pytest.fail(reason='...')

ID: python/pytest-fail-without-message

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2026-09-01首次发现

版本兼容性

版本状态引入弃用备注
3.10 active

根因分析

在较新的 pytest 版本中,调用 pytest.fail() 而不提供 reason 参数已被弃用或引发错误,以强制提供描述性的失败消息。

English

Calling pytest.fail() without a reason argument is deprecated or raises an error in newer pytest versions to enforce descriptive failure messages.

generic

解决方案

  1. 95% 成功率
    Always provide a reason: pytest.fail(reason='Expected condition not met')
  2. 90% 成功率
    Use pytest.fail('message') as a positional argument, which is equivalent to pytest.fail(reason='message').

无效尝试

常见但无效的做法:

  1. 50% 失败

    Using a bare raise or assert False may work but loses the structured failure reporting of pytest.

  2. 60% 失败

    Ignoring the warning and continuing to use pytest.fail() without argument may cause test failures in future pytest versions.