unittest Core to be Vegetarian
1.TestCase
a An instance of TestCase is a test case. What is a test case? is a complete test process, including pre-test preparation Environment setup, execution of test code (run), and post-test environment restore (tearDown) . the nature of the meta-Test (unit test) is here, a test case is a complete test unit, which can be verified by running the test unit.
2.TestSuite
The combination of multiple test cases isTestSuite, andTestSuiteYou can also nest nestedTestSuite. Testloaderis used to loadTestCaseto theTestSuite, of which there are severalloadtestsfrom__ ()method is to find from every placeTestCase, create an instance of them, and thenAddto theTestSuite, and then returns aTestSuiteinstance.
3.TextTestRunner
Texttestrunner is to execute the test case where run (test) executes the testsuite/testcase in the Run (Result) method. The results of the test are saved to the texttestresult instance, including information about how many test cases were run, how many succeeded, and how many failed.
4.Fixture
The construction and destruction of a test case environment is a fixture.
calculator.pyclass Math: def__init__(self,a,b): = Int (a) = int (b) def Add (self): return SELF.A + self.b def Jian (self): return self.a-self.b
Unit Test
1 fromCalculatorImportMath2 ImportUnitTest3 4 classTestmath (unittest. TestCase):5 defsetUp (self):6 Print("Start Test")7 8 defTest_add (self):9j = Math (5,10)TenSelf.assertequal (J.add (), 15) One #self.assertequal (J.add (), A - defTearDown (self): - Print("Test End") the - if __name__=='__main__': - #Constructing test Sets -Suite =UnitTest. TestSuite () +Suite.addtest (Testmath ("Test_add")) - + #Perform tests ARunner =UnitTest. Texttestrunner () atRunner.run (Suite)
PYTHON-ZX note 9-unit test