Fixtures
fixtures can vividly see it as the outer layer of the two biscuit sandwich crackers, two biscuits is setUp / tearDown, the middle of the heart is the test case. In addition, the unittest also provides a greater range of fixtures, for example, Test classes and modules fixtures.
#test.py
#coding:utf-8
import unittest
def setUpModule():
print ("test module start >>>>>>>>>>")
def tearDownMoudle():
print ("test module end >>>>>>>>>>")
class Test(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("test class start >>>>>>>>>>")
@classmethod
def tearDownClass(cls):
print("test class end >>>>>>>>>>")
def setUp(self):
print ("test case start >>>>>>>>>")
def tearDown(self):
print ("test case end >>>>>>>>>>")
def testcase(self):
print ("test case1")
def testcase2(self):
print ("test case2")
if __name__=="__main__":
unittest.main()
Execution results as shown below:
setUpMoudule / tearDownMoudule: to be performed at the beginning and end of the whole module.
setUpClass / tearDownClass: to be performed at the beginning and the end of the test class.
setUp / tearDown: to be performed at the beginning and end of the test case.
Note that setUpClass / tearDownClass need to be decorated by @classmethod parameters, the method is followed by cls. In fact, cls with self and nothing special, only represents a parameter class method.