-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
py.test, assertion for assertDictContainsSubset? #2376
Comments
@tony unfortunately that cant work because 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 😉 |
oh, yes, thanks for the note |
What's about assert dict1.items() <= dict2.items() This should work for Python 3. Edit: assert dict1.viewitems() <= dict2.viewitems() |
This is somewhat tangential, but having a While the below will work just fine assert set1.issubset(set2) Actually printing out the elements that are not within |
I know this is old, but... you could just do this.
|
From #6367 (comment):
cc @ssbarnea |
Isn't |
@henri-hulski , thanks. |
The way to do assert dict2 | dict1 == dict2 on Python 3.9+, or assert {**dict2, **dict1} == dict2 if you need to support older versions of Python. |
Can we consider adding something like assert {'a': 3} <= {'a': 3, 'b': 4} Today we can do this, but it doesn't feel great. assert {'a': 3}.items() <= {'a': 3, 'b': 4}.items() |
what you're suggesting requires a change to python itself and is outside of what pytest can do |
Another approach is the following pattern based on pytest.approx and helpers like https://github.com/utapyngo/pytest-unordered: class DictSubSet:
def __init__(self, items: dict):
self.items = items
def __eq__(self, other):
return self.items == {k: other[k] for k in self.items if k in other}
def __repr__(self):
return repr(self.items)
assert {"foo": "bar", "meh": 4} == DictSubSet({"foo": "bar"}) # passes (class name, capitalization and other details are up for dicsussion of course) As noted above this probably first belongs in a plugin for experimentation. Maybe that already exists somewhere? |
I am looking for something like this, too. But it should support nested data structures. It seems most of the suggestions in this issue only does a shallow comparison of dicts. I found this little used library that almost solves my problem, but doesn't give useful output on failed assertions in pytest. |
@haakenlid my basic implementation of def test_dict_subset_nesting():
assert {1: 2, 3: 4, 5: {6: 7, 8: 9}} == DictSubSet({3: 4, 5: DictSubSet({8: 9})})
assert {1: {2: 3, 4: 5}} == {1: DictSubSet({4: 5})}
assert {1: {2: {3: {4: 5, 6: 7}}}} == {1: {2: DictSubSet({3: DictSubSet({})})}} |
Found a nice way to leverage pytest diffing when asserting a subset of a dict. pytest-dev/pytest#2376 (comment)
Found a nice way to leverage pytest diffing when asserting a subset of a dict. pytest-dev/pytest#2376 (comment)
Found a nice way to leverage pytest diffing when asserting a subset of a dict. pytest-dev/pytest#2376 (comment)
* ✅ test(RasterTileCacheAsset): Add collaborator tests This commit reflects the effort to assert how `raster_tile_cache_asset`'s collaborators are called. The goal is to use these tests to provide feedback during a refactor of this function. While there was code coverage around the function, there weren't any `assert`s for behavior around its dependencies. Now, we have the feedback for more in-depth structural change. * ✅ test: Use `call_args_list` with mocks I definitely think this is a better form than what I was doing previously with `ANY`. The diffs are MUCH nicer. https://stackoverflow.com/questions/21611559/assert-that-a-method-was-called-with-one-argument-out-of-several/56765027#56765027 * ✅ test: Add RasterTileSetSourceCreationOptions tests Found a nice way to leverage pytest diffing when asserting a subset of a dict. pytest-dev/pytest#2376 (comment) * 🎨 refactor: Move `patch` decorator to class declaration This removes lots of boilerplate code! From the python docs: Patch can be used as a TestCase class decorator. It works by decorating each test method in the class. This reduces the boilerplate code when your test methods share a common patchings set. patch() finds tests by looking for method names that start with patch.TEST_PREFIX. By default this is 'test', which matches the way unittest finds tests. You can specify an alternative prefix by setting patch.TEST_PREFIX.
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 asself.assertDictContainsSubset(dict, dict2
.Is this already a feature and I'm missing it? Is py.test open to a PR for it?
The text was updated successfully, but these errors were encountered: