unittest.TestCase
has an assertCountEqual
method (assertItemsEqual
in Python 2, which is arguably a better name), which compares two iterables and checks that they contain the same number of the same objects, without regard for their order.
Does pytest provide something similar? All of the obvious alternatives (e.g. calling set(x)
, sorted(x)
or Counter(list(x))
on each side as mentioned in the documentation) don't work, because the things I'm comparing are lists of dictionaries, and dictionaries aren't hashable.
pytest supports running Python unittest -based tests out of the box. It's meant for leveraging existing unittest -based test suites to use pytest as a test runner and also allow to incrementally adapt the test suite to take full advantage of pytest's features.
Which is better – pytest or unittest? Although both the frameworks are great for performing testing in python, pytest is easier to work with. The code in pytest is simple, compact, and efficient. For unittest, we will have to import modules, create a class and define the testing functions within that class.
How many tests are run, when below code is tested using pytest import unittest def test_sample1(): assert 3 == 3 class SampleTestClass(unittest. TestCase): def test_sample2(self): self. assertEqual(3, 3)
pytest does not provide an assertCountEqual
, but we can just use unittest's:
import unittest def test_stuff(): case = unittest.TestCase() a = [{'a': 1}, {'b': 2}] b = [{'b': 2}] case.assertCountEqual(a, b)
And the output is decent, too
$ py.test ============================= test session starts ============================== platform linux -- Python 3.6.2, pytest-3.2.1, py-1.4.34, pluggy-0.4.0 rootdir: /home/they4kman/.virtualenvs/tmp-6626234b42fb350/src, inifile: collected 1 item test_stuff.py F =================================== FAILURES =================================== __________________________________ test_stuff __________________________________ def test_stuff(): case = unittest.TestCase() a = [{'a': 1}, {'b': 2}] b = [{'b': 2}] > case.assertCountEqual(a, b) test_stuff.py:7: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ /usr/lib/python3.6/unittest/case.py:1182: in assertCountEqual self.fail(msg) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <unittest.case.TestCase testMethod=runTest> msg = "Element counts were not equal:\nFirst has 1, Second has 0: {'a': 1}" def fail(self, msg=None): """Fail immediately, with the given message.""" > raise self.failureException(msg) E AssertionError: Element counts were not equal: E First has 1, Second has 0: {'a': 1} /usr/lib/python3.6/unittest/case.py:670: AssertionError =========================== 1 failed in 0.07 seconds ==========================
Side note: the implementation of assertCountEqual
includes a branch specifically for unhashable types, which does a bunch of bookkeeping and compares each item with every other item.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With