4 ms·
> It's fantastically useful to know (for example) not just that two lists aren't equal, but specifically what elements differ. Pytest shows the diff as well wh
by codedokode 2y ago
> It's fantastically useful to know (for example) not just that two lists aren't equal, but specifically what elements differ.
Pytest shows the diff as well when using assert:
> assert y == x
E assert [1, 2, 3] == [1, 4, 3, 6]
E
E At index 1 diff: 2 != 4
E Right contains one more item: 6
E Use -v to get more diff
- stickfigure 2y agoGreat, 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.