The first five sections mainly introduce the environment construction and the use of the requests library, which can be used to send the interface request. But how to manage the interface case? Returns how the results are automatically verified? This is not possible with the above five sections, so from this section we have introduced the Python Unit Test framework unittest, which handles batch use case management, verifying return results, initialization work, and environmental resiliency after testing.
One, a single use case management is relatively simple, for example, a single use case is generally more useful when debugging:
Second, the code is as follows:
#-*-coding:utf-8-*-#single use case execution#1. Import ModuleImportUnitTest#2, inherit from UnitTest. TestCase classclassTestone (unittest. TestCase):#3, configure the environment: Before testing the initialization of the work defsetUp (self):Print '\ncases before' Pass #4. Define test case with name beginning with "Test" defTest_add (self):" "Test Add Method" " Print 'Add ...'a= 3 + 4b= 7#5, define the Assert assertion, judge the test resultsSelf.assertequal (A, b)deftest_sub (self):" "Test Sub Method" " Print 'Sub ...'a= 10-5b= 4Self.assertequal (A, b)#6. Clean up the environment defTearDown (self):Print ' Case After' Pass#7. This method searches all test case methods that start with test in the module and executes them automaticallyif __name__=='__main__': Unittest.main ()
Output:
inch 0.001sOKcases beforeadd...case aftercases beforesub...case afterprocess finished with exit code 0
Python Interface Automation Test (vi)-unittest-single use case management