adlfkjfadslkjfads

Python Tip of the Day - subTest!

Posted on Sat 23 September 2017 in Posts

Coming from a jUnit background, one of the things I always missed with the vanilla Python unitttest library was parameterized tests.  Oftentimes when writing unit tests for a particular unit you find yourself writing effectively the same test over and over again, but with different inputs. Wouldn't it be nice if we could write the test once and somehow parameterize the test with different inputs? Yes. Yes it would.

Py.test supports this, but what if you really like Nose or some other test runner? Well, in Python 3.4 now in the standard unittest library we have something very similar -- subTests.

The idea is you can write a loop over a set of inputs, and within that loop define a test within a with self.subTest() context. Each iteration will test the given input and failures for each are counted as separate test failures. Really handy for cutting down on unit test boilerplate code.