python config_error ai_generated true

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-09-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active — — —
8.x active — — —

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 70% fail

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

  2. 85% fail

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