python
runtime_error
ai_generated
true
AssertionError: assert 5 == 3, 'Expected 5 but got 3'
ID: python/pytest-assertion-with-message-missing
80%Fix Rate
85%Confidence
0Evidence
2024-03-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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
-
95% success
with pytest.raises(AssertionError, match='Expected 5 but got 3'): assert 5 == 3 -
99% success
assert 5 == 3, 'Expected 5 but got 3'
Dead Ends
Common approaches that don't work:
-
90% fail
Parentheses create a tuple, which pytest interprets as a comparison, not a message.
-
85% fail
The issue is not the f-string but the tuple; f-string still becomes a tuple element.