Automated test = unittest and requests interface test cases, test express query api (2), unittestrequests
Generate a test report based on the original one:
First, generate a report file using unittest of HTMLTestRunner. py.
Then slightly modify the code:
Import requestsimport jsonimport unittestimport timefrom HTMLTestRunner import HTMLTestRunner class MyTest (unittest. testCase): def setUp (self): print ("[+] start") def tearDown (self): print ("[+] end") def zhongtong (self, type = "zhongtong", id = "719857434111"): self. url = "http://www.kuaidi100.com/query" self. params = {"type": type, "postid": id} self. headers = {'user-agent': 'My-app/0.0.1 '} r = requests. get (url = self. url, params = self. params, headers = self. headers) print (r. status_code) return r class ExpressInquiry (MyTest): def test001_type_valid (self): print ("00001") zhongtong = self. zhongtong (type = "shentong") self. assertIn ("courier company parameter exception", zhongtong. text) def test002_type_invalid (self): print ("00002") zhongtong = self. zhongtong (type = "sssssssssssssss") self. assertIn ("parameter error", zhongtong. text) def test003_id_valid (self): print ("00003") id = self. zhongtong (id = "719857434155") self. assertIn ("cainiao station of Traffic Engineering College", id. text) def test004_id_invalid (self): print ("00004") id = self. zhongtong (id = "123") self. assertIn ("parameter error", id. text) def test005_type_id_invalid (self): print ("00005") type_and_id = self. zhongtong (type = "dads", id = "7777") # print (type_and_id.url) # print (type_and_id.text) self. assertIn ("parameter error", type_and_id.text) def test006_type_id_null (self): print ("00006") null = self. zhongtong (type = "", id = "") # print (null. url) # print (null. text) self. assertIn ("parameter error", null. text) def suite (): now = time. strftime ("% Y-% m-% d % H _ % M _ % S") filename = '. /'+ now + 'test_result.html' fp = open (filename, 'wb') runner = HTMLTestRunner (stream = fp, title = "express query interface test report ", description = "test case execution:") suite = unittest. testSuite () suite. addTest (ExpressInquiry ("test001_type_valid") suite. addTest (ExpressInquiry ("test002_type_invalid") suite. addTest (ExpressInquiry ("test003_id_valid") suite. addTest (ExpressInquiry ("test004_id_invalid") suite. addTest (ExpressInquiry ("test005_type_id_invalid") suite. addTest (ExpressInquiry ("test006_type_id_null") # unittest. textTestRunner (). run (suite) runner. run (suite) fp. close () if _ name _ = '_ main _': # unittest. main (exit = False, verbosity = 2) # It is a global method. After blocking it, the use cases that are not in the suite will not run, exit = False indicates that the intermediate useful instance fails and execution continues; there are also common verbosity = 2, indicating that the def name is displayed in suite ()
Test results after running: