Extending on @bman's answer, exploiting that the comparison operators for set-like objects are overloaded as subset operators, you can use assertGreaterEqual
for (arguably) better error messages.
Compare the two tests:
import unittest
class SubsetTestCase(unittest.TestCase):
def test_dict_1(self):
a = {1: 1, 2: 2}
b = {1: 2}
self.assertTrue(a.items() >= b.items())
def test_dict_2(self):
a = {1: 1, 2: 2}
b = {1: 2}
self.assertGreaterEqual(a.items(), b.items())
unittest.main()
The result is:
======================================================================
FAIL: test_dict_1 (__main__.SubsetTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 9, in test_dict_1
self.assertTrue(a.items() >= b.items())
AssertionError: False is not true
======================================================================
FAIL: test_dict_2 (__main__.SubsetTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 15, in test_dict_2
self.assertGreaterEqual(a.items(), b.items())
AssertionError: dict_items([(1, 1), (2, 2)]) not greater than or equal to dict_items([(1, 2)])
----------------------------------------------------------------------
With assertGreaterEqual
, you can see the contents of the two dictionaries from the error message.