When you write unit tests in Python you can use these widgets:
self.assertEqual(var1, var2, msg=None) self.assertNotEqual(var1, var2, msg=None) self.assertTrue(expr, msg=None) self.assertRaises(exception, func, para, meters, ...)
That's fine but is it "pythonic" enough? The alternative is to do with with "pure python". Eg:
assert var1 == var2, msg assert var1 != var2, msg assert expr, msg try: func(para, meter) raise Exception except exception: pass
I'm sure there are several benefits with using the
unittest methods that I don't understand but I understand the benefits of brevity and readability. The more tests you write the more tedious it becomes to write
self.assertEquals(..., ...) every time. In my own code I prefer to use simple
assert statements rather than the verbose
unittest alternative. Partially because I'm lazy and partially because they read better and the word
assert is highlit in red in my editor so it just looks nicer from a distance.
Perhaps some much more clever people than me can explain what a cardinal sin it is to not use the
unittest methods over the lazy more pythonic ones.
Incidentally, during the course of jotting down this blog I reviewed some old inherited code and changed this:
assert not errors
Isn't that just nicer to use/read/write?