python
runtime_error
ai_generated
true
断言错误:断言 5 == 3,'期望 5 但得到 3'
AssertionError: assert 5 == 3, 'Expected 5 but got 3'
ID: python/pytest-assertion-with-message-missing
80%修复率
85%置信度
0证据数
2024-03-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
断言失败,但自定义消息未显示,因为使用带有元组的普通 assert 时,pytest 不会显示消息。
English
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.
解决方案
-
95% 成功率
with pytest.raises(AssertionError, match='Expected 5 but got 3'): assert 5 == 3 -
99% 成功率
assert 5 == 3, 'Expected 5 but got 3'
无效尝试
常见但无效的做法:
-
90% 失败
Parentheses create a tuple, which pytest interprets as a comparison, not a message.
-
85% 失败
The issue is not the f-string but the tuple; f-string still becomes a tuple element.