python config_error ai_generated true

PytestReturnNotNoneWarning: 期望返回 None,但 tests/test_x.py::test_foo 返回了 1,这将在未来版本的 pytest 中成为错误

PytestReturnNotNoneWarning: Expected None, but tests/test_x.py::test_foo returned 1, which will be an error in a future version of pytest

ID: python/pytest-return-not-none

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

版本兼容性

版本状态引入弃用备注
7.x active — — —
8.x active — — —

根因分析

测试函数返回值而非使用 assert。常见于移植 unittest 风格测试,或辅助函数返回了本意作为断言的真值。

English

A test function returns a value instead of using assert. Common when porting unittest-style tests or when a helper returns a truthy value that was intended as an assertion.

generic

解决方案

  1. 98% 成功率
    # before
    def test_foo():
        return compute() == 1
    
    # after
    def test_foo():
        assert compute() == 1
  2. 90% 成功率
    [tool.pytest.ini_options]
    filterwarnings = ["error::pytest.PytestReturnNotNoneWarning"]

无效尝试

常见但无效的做法:

  1. 70% 失败

    Silences the warning but the test may pass even when the intended condition is false, because no assertion occurs.

  2. 85% 失败

    Hides a real bug: the test is not actually asserting anything.