Test equality of unordered collections in pytest
Maintained. Last release 5 months ago, commits in 1 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install pytest-unorderedpytest-unordered: Test collection content, ignoring order
pytest_unordered allows you to write simple (pytest) assertions
to test whether collections have the same content, regardless of order.
For example:
assert [1, 20, 300] == unordered([20, 300, 1])
It is especially useful when testing APIs that return some complex data structures in an arbitrary order, e.g.:
assert response.json() == {
"people": unordered(
# Here we test that the collection type is list
[
{
"name": "Alice",
"age": 20,
"children": unordered(
# Here the collection type is not important
{"name": "Bob", "age": 2},
{"name": "Carol", "age": 3},
),
},
{
"name": "Dave",
"age": 30,
"children": unordered(
{"name": "Eve", "age": 5},
{"name": "Frank", "age": 6},
),
},
]
),
}
Installation
pip install pytest-unordered
Usage
Basics
In most cases you just need the unordered() helper function:
from pytest_unordered import unordered
Compare list or tuples by wrapping your expected value with unordered():
assert [1, 20, 300] == unordered([20, 300, 1]) # Pass
assert (1, 20, 300) == unordered((20, 300, 1)) # Pass
Excessive/missing items will be reported by pytest: