Thursday, July 11, 2013

Python: skip a unittest with unittest.skipTest

There's a neat way to skip python tests built into unittest. You can use decorators for the test or whole class of tests that will skip a test unconditionally or based on a condition.

In my case there was significant setup to do before I knew whether or not this test should be skipped, in which case you can call unittest.TestCase.skipTest, which just raises a unittest.SkipTest exception that is handled by the test runner. It looks like this:
In [40]: runner = unittest.TextTestRunner()

In [41]: class testing(unittest.TestCase):
   
def testblah(self):
       
pass
   
def setUp(self):
       
self.skipTest('asdfasdf')
   
def runTest(self):
       
pass
   
....:

In [42]: thistestclass=testing()

In [43]: runner.run(thistestclass)
s
----------------------------------------------------------------------
Ran 1 test in 0.000s  

OK
(skipped=1)
Out[43]:

No comments: