Uinittest use UnitTest. TestCase
Instance:
import unittestclass Test1(unittest.TestCase): def test_x1(self): self.a=1 def test_x2(self): print("a:",self.a)class Test2(): def x1(self): self.b=1 def x2(self): print("b:",self.b)#测试调用1unittest.main()#测试调用2A=Test2()A.x1()A.x2()
- First UnitTest. The test case method in TestCase needs to start with test, and subsequent calls to Unittest.main () will automatically execute these use-case methods
- Unittest.main () is a function that executes the functions in the Test1 class sequentially, but it differs from the normal class instance by invoking the method individually.
The obvious test call 1 executes the test_x1 and the Test_x2 method one by one, when executing to the TEST_X2, the error does not define the variable a
The Test call 2 does not report that the variable B is undefined because the variable b is initialized when the a.x1 () is executed. So there's no error.
TestSuite
- Use the above class Test1 (UnitTest. TestCase) class definition
suite=unittest.TestSuite()suite.addTest(Test1('test_x1'))suite.addTest(Test1('test_x2'))#或者使用#suite.addTests([Test1('test_x1'),Test1('test_x2')]) 相当于上面两句runner = unittest.TextTestRunner()runner.run(suite)
Python unittest use