35

This question gives the order assertEqual(expected, actual), albeit for the unittest package.

But Pycharm, with pytest, prints out "Expected:..." and "Actual..." based on the order actual==expected.

This is confusing. What is the correct ordering for pytest? The source code and online documentation do not say.

(I note also that JUnit and TestNG disagree on this.)

Joshua Fox
  • 18,704
  • 23
  • 87
  • 147
  • 5
    There is no ordering. In pytest it's a simple `==` clause. Both sides a equivalent (or not). The developers of your IDE might not have read "In the face of ambiguity, refuse the temptation to guess." from PEP 20. – Klaus D. Dec 06 '18 at 12:23
  • 5
    I agree. This is rather a Pycharm issue, not `pytest` one. Aside from that, `pytest` support in Pycharm is insufficient anyway. – hoefling Dec 06 '18 at 12:28

2 Answers2

30

JUnit

assertEquals(expected, actual)

Pytest (Pycharm)

Comments have implied this may be more an issue with the way PyCharm displays the message than pytest itself - ie. this message may not exist outside of PyCharm...

assert actual == expected

For example:

def test_actual_expected():
    expected = 4
    actual = 2+1
    assert actual == expected

Will fail with message

enter image description here

Adam Hughes
  • 14,601
  • 12
  • 83
  • 122
  • Seems to be a pycharm thing indeed. Running the test without pycharm directly in the console does not show any messages that indicate which side is which one. Even a generated junit xml report does not give any hints in this direction. I guess pycharm just doesn't differentiate between expected and actual. In general it is not possible anyways, especially for arbitrary boolean expressions. E.g. what is the expected/actual value in `assert x and y`? – Socowi Nov 09 '22 at 11:10
16

BDFL doesn't like actual/expected terminology and the docs were specifically changed to address this.

If your tooling is expecting arguments in a certain order, then I suppose the most correct thing to do would be to consistently do what works for your tooling.