python
runtime_error
ai_generated
true
AssertionError: 3 != 5 : expected 5 but got 3
ID: python/unittest-assert-equal-order
80%Fix Rate
85%Confidence
0Evidence
2024-01-22First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.10 | active | — | — | — |
| 3.12 | active | — | — | — |
Root Cause
In unittest, `assertEqual(first, second)` reports the message as `first != second`. Developers often read the message right-to-left and fix the wrong side of the assertion, or assume the function under test is broken when the arguments were swapped.
generic中文
在 unittest 中,`assertEqual(first, second)` 报告的消息格式是 `first != second`。开发者常把消息从右往左读,从而修改了断言错误的一侧,或误以为被测函数有问题,而实际是参数顺序写反了。
Workarounds
-
95% success
Always put the actual value first and add a msg for clarity: self.assertEqual(result, 5, f'unexpected result: {result!r}') -
98% success
Use pytest's rich assertion rewriting instead, which prints both operands clearly: assert result == 5
Dead Ends
Common approaches that don't work:
-
60% fail
The test expectation was reversed; changing production code introduces a bug to satisfy a wrong assertion.
-
90% fail
Silences the test and hides real regressions; the CI still reports green while behavior is wrong.