Python unittest setUp function -


i relatively new python. according unittest.setup documentation:

setup()
method called prepare test fixture. this called before calling test method; exception raised method considered error rather test failure. default implementation nothing.

my question setup follows:

in our testing code base, have seen customized python testing framework inheriting unittest.testcase. originally, unittest.testcase has names setup , teardown.in customized class, have setuptestcase , teardowntestcase. each time 2 functions called instead of original counterparts.

my questions are:

  1. how setup , teardown functions being called underlying test runner?
  2. is required functions used set test cases should start setup , functions used tear down test cases should start teardown? or can named valid identifier?

thank you.

  1. setup() , teardown() methods automatically used when available in classes inheriting unittest.testcase.
  2. they should named setup() , teardown(), used when test methods executed.

example:

class mytestcase(unittest.testcase):   def setup(self):      self.setupmystuff()    def teardown(self):      self.teardownmystuff()  class testspam(mytestcase):   def setupmystuff(self):     # called before execution of every method named test_...     self.cnx = # ... connect database    def teardownmystuff(self):     # called after execution of every method named test_...     self.cnx.close()    def test_get_data(self):     cur = self.cnx.cursor()     ... 

Comments