Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an implementation of UnitTest.assertCountEqual #5548

Closed
software-opal opened this issue Jul 3, 2019 · 13 comments
Closed

Provide an implementation of UnitTest.assertCountEqual #5548

software-opal opened this issue Jul 3, 2019 · 13 comments
Labels
type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@software-opal
Copy link

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.

@asottile
Copy link
Member

asottile commented Jul 4, 2019

I don't think pytest should grow an assertion library, you can already get essentially this by using sets or counters (and with better assertion messages usually)

@RonnyPfannschmidt
Copy link
Member

pytest should grow assertion helpers, something like the "unordered" or a "sorted" object would be helpfull in many ways

but this should be started outside of pytest and experimented wtith for a few years before bringing it into core

@Zac-HD Zac-HD added the type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature label Jul 9, 2019
@Zac-HD
Copy link
Member

Zac-HD commented Jul 9, 2019

Closing this issue, as it's suitable to be demonstrated in a plugin before further discussion in Pytest.

@Zac-HD Zac-HD closed this as completed Jul 9, 2019
@utapyngo
Copy link

Has anyone started working on a plugin for this?

@RonnyPfannschmidt
Copy link
Member

im unaware, if anyone is please link

@utapyngo
Copy link

Here is a related SO question: https://stackoverflow.com/questions/41605889/does-pytest-have-an-assertitemsequal-assertcountequal-equivalent

@blueyed
Copy link
Contributor

blueyed commented Oct 28, 2019

For reference: pytest-dev/pytest-django#709 (using Django's helpers).

@utapyngo
Copy link

Closing this issue, as it's suitable to be demonstrated in a plugin before further discussion in Pytest.

Is a plugin able to inject a function into the pytest namespace? I want to use it as pytest.unordered(), the same way as pytest.approx().

@RonnyPfannschmidt
Copy link
Member

intentionally no longer

@utapyngo
Copy link

utapyngo commented Oct 29, 2019

What hook the plugin should use then?
Should it be a pytest- plugin at all? It seems that a simple package exporting one function is enough.

@RonnyPfannschmidt
Copy link
Member

@utapyngo well, it needs assertrepr hooks for explaining whats different/not different

@utapyngo
Copy link

@utapyngo
Copy link

@RonnyPfannschmidt, could you please review pytest-unordered?

@software-opal, will this work for you?

from pytest_unordered import unordered

def test_reverse_keeps_items():
    my_list = ['a', {'key': 'value'}, object()]
    assert my_list == unordered(list(reversed(my_list)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

6 participants