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...
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 |
To: @mattccrampton
0
Other Posts
When exporting photos from a service like Flickr, perhaps after they've given notice that they're going to delete our photos if you don't subscribe to......
All Truthy and Falsy Javascript Values In Nodejs, every value has an associated boolean, true or false, value. For example, a null value has an......
Google Forcing Nest Cameras Visual Indicator Light To Be On Received the following email from Google today... Full email text... Recently, we shared our commitment......
Posting to Twitter with Python - Part Two: Posting Photos NOTE: This is part two of my posting to Twitter with Python tutorial. If you......
Doubleclick to open a file in VIM from OSX I use VIM for just about everything from note taking to coding to keeping track of......
Sign Into Gmail Without Signing Into Google Chrome Unfortunately, Google has made changes to Chrome since this blog post was posted which removed the options......