Best way to assert unit test cases with many conditions

Dan Strohl D.Strohl at F5.com
Tue Jul 18 13:32:30 EDT 2017


Ganesh;

I'm not 100% sure what you are trying to do.. so let me throw out a few things I do and see if that helps...

If you are trying to run a bunch of similar tests on something, changing only (or mostly) in the parameters passed, you can use self.subTest().

Like this:

Def test_this(self):
    For i in range(10):
        with self.subTest('test number %s) % i):
            self.assertTrue(I <= 5)

With the subTest() method, if anything within that subTest fails, it won't stop the process and will continue with the next step.

If you are trying to run a single test at the end of your run to see if something messed something up (say, corrupted a file or something), you can, (at least with the default unittest) name your test something like test_zzz_do_this_at_end, and unless you have over-ridden how the tests are being handled (or are using a different testing environment), unittest should run it last (of the ones in that TestCase class).

From: https://docs.python.org/2/library/unittest.html#organizing-test-code  "Note that the order in which the various test cases will be run is determined by sorting the test function names with respect to the built-in ordering for strings."



More information about the Python-list mailing list