All Python Assert Methods In One List

Every Python Assert Method in One List

I am constantly looking up assert methods on the Python 2 Unit Test Documentation and it's driving me crazy because that page is super long and hard to mentally parse. The lists of assert methods available are broken up in different sections on the page and I'm done searching around it.

Writing unit tests in Python obviously requires heavy use of assert methods, but for some reason they're buried in the Python documentation.

So here's a compiled list of all of them for my reference, but maybe this is useful for someone else out there too...

Pause reading for 5 seconds...
Join My Mailing List!
Note: I will never share your email with anyone else.
Awesome! Thanks so much.
Submitting...
Ok, lets continue.

Python Assert Methods

Method Checks Version
assertEqual a == b  
assertNotEqual a != b  
assertTrue bool(x) is True  
assertFalse bool(x) is False  
assertIs a is b 2.7
assertIsNot a is not b 2.7
assertIsNone x is None 2.7
assertIsNotNone x is not None 2.7
assertIn a in b 2.7
assertNotIn a not in b 2.7
assertIsInstance is instance(a,b) 2.7
assertNotIsInstance not is instance(a,b) 2.7
assertRaises fun(*args,**kwds) raises exc  
assertRaisesRegexp fun(*args,**kwds) raises exc(regex) 2.7
assertAlmostEqual round(a-b,7) == 0  
assertNotAlmostEqual round(a-b,7) != 0  
assertGreater a > b 2.7
assertGreaterEqual a >= b 2.7
assertLess a < b 2.7
assertLessEqual a <= b 2.7
assertRegexpMatches r.search(s) 2.7
assertNotRegexpMatches not r.search(s) 2.7
assertItemsEqual sorted(a) == sorted(b) 2.7
assertDictContainsSubset all the key/value pairs in a exist in b 2.7
assertMultiLineEqual strings 2.7
assertSequenceEqual sequences 2.7
assertListEqual lists 2.7
assertTupleEqual tuples 2.7
assertSetEqual sets or frozensets 2.7
assertDictEqual dicts 2.7
Post A Twitter Response To This Blog Post

To: @mattccrampton

0