How to Assert List Equality in pytest: Simple Guide
In pytest, you can assert that two lists are equal using the simple
assert list1 == list2 statement. This checks if both lists have the same elements in the same order and will pass or fail the test accordingly.Syntax
The basic syntax to assert list equality in pytest is:
assert list1 == list2Here:
list1andlist2are the lists you want to compare.- The
assertkeyword checks if the condition is true. - If the lists are equal (same elements in the same order), the test passes.
- If not, pytest shows a detailed error message.
python
assert list1 == list2Example
This example shows how to write a pytest test function that asserts two lists are equal. It demonstrates a passing test and what happens if the lists differ.
python
def test_lists_are_equal(): list_a = [1, 2, 3] list_b = [1, 2, 3] assert list_a == list_b def test_lists_are_not_equal(): list_a = [1, 2, 3] list_b = [3, 2, 1] assert list_a == list_b
Output
============================= test session starts ==============================
collected 2 items
test_example.py .F [100%]
=================================== FAILURES ===================================
__________________________ test_lists_are_not_equal ___________________________
def test_lists_are_not_equal():
list_a = [1, 2, 3]
list_b = [3, 2, 1]
> assert list_a == list_b
E AssertionError: assert [1, 2, 3] == [3, 2, 1]
E At index 0 diff: 1 != 3
E Full diff:
E - [1, 2, 3]
E + [3, 2, 1]
test_example.py:8: AssertionError
=========================== short test summary info ===========================
FAILED test_example.py::test_lists_are_not_equal - AssertionError: assert [1, 2, 3] == [3, 2, 1]
============================== 1 failed, 1 passed ==============================
Common Pitfalls
Common mistakes when asserting list equality in pytest include:
- Comparing lists with the same elements but different order, which will fail because order matters.
- Using
assert list1 is list2which checks if both variables point to the same object, not if their contents are equal. - Not considering nested lists or complex objects where deep equality might be needed.
Correct way:
assert list1 == list2Wrong way:
assert list1 is list2 # This checks identity, not equalitypython
def test_wrong_identity_assertion(): list_a = [1, 2, 3] list_b = [1, 2, 3] assert list_a is list_b # This will fail def test_correct_equality_assertion(): list_a = [1, 2, 3] list_b = [1, 2, 3] assert list_a == list_b # This will pass
Output
============================= test session starts ==============================
collected 2 items
test_pitfalls.py F. [100%]
=================================== FAILURES ===================================
__________________________ test_wrong_identity_assertion ______________________
def test_wrong_identity_assertion():
list_a = [1, 2, 3]
list_b = [1, 2, 3]
> assert list_a is list_b # This will fail
E AssertionError
test_pitfalls.py:4: AssertionError
=========================== short test summary info ===========================
FAILED test_pitfalls.py::test_wrong_identity_assertion - AssertionError
============================== 1 failed, 1 passed ==============================
Quick Reference
Tips for asserting list equality in pytest:
- Use
assert list1 == list2to check if lists have the same elements in order. - Order matters:
[1, 2, 3]is not equal to[3, 2, 1]. - For unordered comparison, consider sorting lists first:
assert sorted(list1) == sorted(list2). - Use pytest's detailed error messages to quickly find differences.
Key Takeaways
Use simple assert statements like assert list1 == list2 to check list equality in pytest.
List order matters; identical elements in different order will cause assertion failure.
Avoid using 'is' for list comparison as it checks identity, not content equality.
Sort lists before asserting if order does not matter for your test case.
Pytest shows clear error messages to help identify differences between lists.