The methods setUp and tearDown are executed before and after any test method of a test case. This is necessary when the test methods change the data. They guarantee that the test data is restored before the next test is executed.
However, there is also often a situation where your tests do not change the test data and you want to save time by only once setting up the data. This is done by the class method setUpClass.
The following code block schematically illustrates how the method setUpClass is used. You might also want to check Section 8.4: Class attributes and class methods again.
Â
import unittest
class TestExample(unittest.Testcase):
@classmethod
def setUpClass(cls):
cls.A=....
def Test1(self):
A=self.A
# assert something
....
def Test2(self):
A=self.A
# assert something else