-1

from the doc

assertEqual(first, second, msg=None) Test that first and second are equal. If the values do not compare equal, the test will fail.

for first and second what should be an expected value and an actual value?

200 not ok
  • 134
  • 10
  • Doesn't matter. It's an equality expression `'abc' == 'abc'` is the same (`True`) regardless if you do the expression in reverse. `'abc' == 'def'` is `False` regardless of order. That being said I usually put actual first and expected after but thats 100% preference. – Error - Syntactical Remorse Aug 26 '19 at 18:09
  • Your question is far too broad, for any given circumstance you'll expect an outcome, and need to assert that the actual value is what you expect. – Sayse Aug 26 '19 at 18:09
  • Possible duplicate of [What is the use of "assert" in Python?](https://stackoverflow.com/questions/5142418/what-is-the-use-of-assert-in-python) – Trenton McKinney Aug 26 '19 at 18:14
  • @Error-SyntacticalRemorse Thank you very much. I was wondering if there's any standard. Now I know it's just a preference. – 200 not ok Aug 26 '19 at 18:16
  • @Trenton_M This question is not a duplicate. The other question is about assert statement, mine is about assert methods from python unittest framework (also asking for best practice not usage). – 200 not ok Aug 26 '19 at 18:31
  • @Error-SyntacticalRemorse Actually [order matters](https://stackoverflow.com/a/57630137/2142055) – Stop harming Monica Aug 29 '19 at 05:26
  • @Goyo not really. True, the left object's `__eq__` function is called as with ALL equality checks in python (including `==` and all other comparison magic methods) but during equality checking it is normal that both objects are of the same time (since your are comparing equality) and thus order shouldn't matter in almost all usecases. – Error - Syntactical Remorse Aug 29 '19 at 13:01
  • 1
    @Error-SyntacticalRemorse Of course I should have said that **sometimes** it matters. It mattered a lot for the guy asking that question so for him it wasn't 100% preference . – Stop harming Monica Aug 29 '19 at 13:14

2 Answers2

1

Even the answer and comments imply differently, there is actual best practice: be consistent!.

So, choose an ordering (for example your hard-coded/fixture value is first) and stick to that for the rest of your code.

ipaleka
  • 3,745
  • 2
  • 13
  • 33
0

assertEqual itself doesn't really care:

======================================================================
FAIL: test_foo (foo.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/private/tmp/foo.py", line 6, in test_foo
    self.assertEqual("actual string", "expected string")
AssertionError: 'actual string' != 'expected string'
- actual string
+ expected string
Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65