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.
You could do a list comprehension to check equality of all values. If you call all
on the list comprehensions result, it will return True
if all parameters are equal.
actual = ['bl', 'direction', 'day']
expected = ['bl', 'direction', 'day']
assert len(actual) == len(expected)
assert all([a == b for a, b in zip(actual, expected)])
print(all([a == b for a, b in zip(actual, expected)]))
>>> True