-
-
Couldn't load subscription status.
- Fork 2.9k
Description
What's the problem this feature will solve?
A convenient way to assert that two sequences contains the exact same elements regardless of their order.
Describe the solution you'd like
assert [1, 2, 3] == pytest.unordered([2, 1, 3])For example, I could use it when order of elements are not part of the specification of a function:
# Per "Path.iterdir()" documentation: "The children are yielded in arbitrary order"
assert list(tmp_path.iterdir()) == pytest.unordered([tmp_path / "some_file.txt", tmp_path / "another_file.txt"])Alternative Solutions
Sorting the sequence manually, using unittest.TestCase.assertCountEqual, using pytest-unordered plugin.
Additional context
This issue duplicates:
- Provide an implementation of UnitTest.assertCountEqual #5548
- assert helper to compare list contents, ignoring order #7899
These tickets were closed with the suggestion to first create an external plugin and testing it during a few years. Following this, @utapyngo created the pytest-unordered plugin. It has been several years. I would like to bring back the suggestion of a built-in pytest.unordered() helper similar to pytest.approx() because I think pytest users would greatly benefit from it.
I don't think it opens the room for too many assertion helpers: with approx() and unordered() all main cases are covered. For comparison, here is the list of helpers provided by the Catch2 C++ testing library. Python provides many built-in functions to ease assertion, but unordered() is missing and is non-trivial.
What do you think?
Metadata
Metadata
Assignees
Labels
Type
Projects
Status