Python unittest Assertions

Enjoy this cheat sheet at its fullest within Dash, the macOS documentation browser.

Assertions

Method Checks that New in

assertEqual(a, b)

a == b

assertNotEqual(a, b)

a != b

assertTrue(x)

bool(x) is True

assertFalse(x)

bool(x) is False

assertIs(a, b)

a is b

2.7

assertIsNot(a, b)

a is not b

2.7

assertIsNone(x)

x is None

2.7

assertIsNotNone(x)

x is not None

2.7

assertIn(a, b)

a in b

2.7

assertNotIn(a, b)

a not in b

2.7

assertIsInstance(a, b)

isinstance(a, b)

2.7

assertNotIsInstance(a, b)

not isinstnace(a, b)

2.7

assertRaises(exc, fun, args, *kwds)

fun(*args, **kwds) raises exc

assertRaisesRegexp(exc, r, fun, args, *kwds)

round(a-b, 7) == 0

2.7

assertAlmostEqual(a, b)

round(a-b, 7) == 0

assertNotAlmostEqual(a, b)

round(a-b, 7) != 0

assertGreater(a, b)

a > b

2.7

assertGreaterEqual(a, b)

a >= b

assertLess(a, b)

a < b

2.7

assertLessEqual(a, b)

a <= b

2.7

assertRegexpMatches(s, r)

r.search(s)

2.7

assertNotRegexpMatches(a, b)

not r.search(s)

2.7

assertItemsEqual(a, b)

sorted(a) == sorted(b)

Works with unhashable objects

assertDictContainsSubset(a, b)

All the key/value pairs in a exist in b

2.7

Notes