I am having little trouble using the python setUpClass.
For example consider the following case
class MyTest(unittest.case.TestCase):
@classmethod
def setUpClass(cls):
print "Test setup"
try:
1/0
except:
raise
@classmethod
def tearDownClass(cls):
print "Test teardown"
A couple of questions
-
Is the above code the right way to handle test setUpClass exceptions (by raising it so that the python unittest can take care of it), there are fail(), skip() methods, but those can only be used by test instances and not the test classes.
-
When there is a setUpClass exception, how can we ensure that tearDownClass runs (unittest doesn’t run it, should we manualy call it).
You can call
tearDownClasson an exception as Jeff points it out, but you may also implements the__del__(cls)method :Will have the following output :
Note : you should be aware that the
__del__method will be called at the end of the program execution, which is maybe not what you want if you have a more than one test class.Hope it helps