python runtime_error ai_generated true

AssertionError: assert 5 == 3, 'Expected 5 but got 3'

ID: python/pytest-assertion-with-message-missing

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-03-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

The assertion failed and the custom message is not being displayed because pytest doesn't show the message when using plain assert with a tuple.

generic

中文

断言失败,但自定义消息未显示,因为使用带有元组的普通 assert 时,pytest 不会显示消息。

Workarounds

  1. 95% success
    with pytest.raises(AssertionError, match='Expected 5 but got 3'):
        assert 5 == 3
  2. 99% success
    assert 5 == 3, 'Expected 5 but got 3'

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Parentheses create a tuple, which pytest interprets as a comparison, not a message.

  2. 85% fail

    The issue is not the f-string but the tuple; f-string still becomes a tuple element.