Pytest: py.test, assertion for assertDictContainsSubset?

Created on 21 Apr 2017  路  12Comments  路  Source: pytest-dev/pytest

self.assertDictContainsSubset is in unittest in Standard Library.

See https://github.com/python/cpython/blob/bbd3cf8/Lib/unittest/case.py#L1122.

Googled and looked around, I haven't seen a way to do it.

In py.test, assert <dict> in <dict2> should be the same as self.assertDictContainsSubset(dict, dict2.

Is this already a feature and I'm missing it? Is py.test open to a PR for it?

rewrite tracebacks proposal

Most helpful comment

What's about

assert dict1.items() <= dict2.items()

This should work for Python 3.

Edit:
For Python 2 it would be:

assert dict1.viewitems() <= dict2.viewitems()

All 12 comments

@tony unfortunately that cant work because a in b in python always means b contains a as an element so py.test has no equivalent of that unittest method yet

there is no clear consens on how to add it propperly

Any ideas come to mind?

Standard library has this functionality, so I'm inclined to say it may be better of it's in pytest. Do you think so? Or would it be better as a plugin?

how about creating a new infix operator? Like 'assert dict1 contains dict2'?

@sallner python does not support adding new operators

@tony a plugin may be a good place to experiment with it, py.test is in need for a good way to do more complex assertions than normal python operators allow

however it usually takes a few tries to get it to a good quality level, so a plugin to spearhead that would probably be best

i did create an issue about this in #95 a while back, but i haven't found the time to revisit

@RonnyPfannschmidt you mean @smehan and not @sallner :wink:

oh, yes, thanks for the note

What's about

assert dict1.items() <= dict2.items()

This should work for Python 3.

Edit:
For Python 2 it would be:

assert dict1.viewitems() <= dict2.viewitems()

This is somewhat tangential, but having a assertIsSubset could be useful, particularly with set objects.

While the below will work just fine

assert set1.issubset(set2)

Actually printing out the elements that are not within set1 (but in set2) would be a nifty/simple feature to have off of the shelf.

I know this is old, but... you could just do this.

expected = {'important': 'value to test'}
actual = {'important': 'value to test', 'irrelevant': 'potato', 'un-important': 'hair dryer'}
assert expected == {k: v for k, v in actual.items() if k in expected}

From https://github.com/pytest-dev/pytest/issues/6367#issuecomment-569252514:

While @asottile is correct, I do wonder if we should perhaps have an official assertion library/plugin, as it solves practical problems. Immediately comes to mind how often I use fnmatch_lines even though it is private.

But before we introduce assertions, I would like to see a possible list of "assertion helpers" that should be included, so we can take a look at the landscape and decide if this should be indeed a separate library (perhaps under pytest-dev directly) or introduced into pytest itself (I tend for the former, as it doesn't depend on the internals and might have a different release cycle than the core).

This has been brought up before: #2376

cc @ssbarnea

Isn't pytest.raises() an example of an assertion built-in to pytest? It doesn't have an assert in the name but acts like it.

@henri-hulski , thanks.
Using self.assertLessEqual(a.items(), b.items()) works fine.

Was this page helpful?
0 / 5 - 0 ratings