4 ms·
Great, what's the equivalent of this? assertThat(someCollection).map(Thing::getValue).containsExactlyInAnyOrder(3, 4, 5);
by stickfigure 2y ago
Great, what's the equivalent of this?
assertThat(someCollection).map(Thing::getValue).containsExactlyInAnyOrder(3, 4, 5);
- codedokode 2y agoIf the list contains numbers or strings, you can use assert sorted(x) == sorted(y) If you want to add map, then you have to write assert list(sorted(i.getValue() for i in someCollection)) == [3, 4, 5] If the list contains non-sortable values, but they are hashable and unique, you can use sets: assert set(x) == set(y) If the values are not unique, but hashable, you can use a counter (like a set but with count for repeating values): assert Counter(x) == Counter(y) (by the way I learned about this trick from a LLM) And if the values are neither sortable, nor hashable, you'll have to write a helper function. But still, pytest tests are less wordy and they don't require you to create a class. This situation is familiar to me, I had to write such helper function when writing tests in PHP, for some reason it is not included in PHPUnit assertions.