Skip to content

Provide an implementation of UnitTest.assertCountEqual #5548

Closed
@software-opal

Description

@software-opal

Currently if I want to test that two lists contain the same elements, ignoring their order, I need to use a set(only on hashable types) or use a UnitTest subclass(which means I loose some functionality).

It would be good to provide some guidance on how to do within PyTest.

I'm thinking it would look something like this:

import pytest

def my_reverse(list):
  return reversed(list)

def test_reverse_keeps_items():
  my_list = ['a', {'key': 'value'}, object()]
  # I'm just spitballing the name; I don't know what would be a good name for it.
  assert my_list == pytest.unordered(reversed(my_list))
  # or
  pytest.assert_count_equal(my_list, my_reverse(my_list))

If this isn't a good idea it would be nice to add a note in the docs on how to do it so that people can easily discover it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions