pytest-dev / pytest-dev/pytest

Add a built-in way to compare sequences ignoring order

Open
#10,032 14 comments 25 reactions 1 assignee View on GitHub

@The-Compiler is already working on this.

Since Jun 17, 2024.

type: proposal
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

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:

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.