How can I assert lists equality with pytest

See this:

Note:

You can simply use the assert statement for asserting test
expectations. pytest’s Advanced assertion introspection will
intelligently report intermediate values of the assert expression
freeing you from the need to learn the many names of JUnit legacy
methods.

And this:

Special comparisons are done for a number of cases:

  • comparing long strings: a context diff is shown
  • comparing long sequences: first failing indices
  • comparing dicts: different entries

And the reporting demo:

failure_demo.py:59: AssertionError
_______ TestSpecialisedExplanations.test_eq_list ________

self = <failure_demo.TestSpecialisedExplanations object at 0xdeadbeef>

    def test_eq_list(self):
>       assert [0, 1, 2] == [0, 1, 3]
E       assert [0, 1, 2] == [0, 1, 3]
E         At index 2 diff: 2 != 3
E         Use -v to get the full diff

See the assertion for lists equality with literal == over there? pytest has done the hard work for you.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)