Previous reports generated test code, if repeated run test report with the same name will be kept covered, before the test reports will be lost, before the problem can not be traced. So how to solve this problem then?
First thought is to use random function to take a random name, once generated more reports, the order can not distinguish between reports, so you can take advantage of built-in python function of time to generate the current time to name the test report, then the above problem is completely solved a. Of course, you can also be used
Solve similar things, such as reptiles crawling down the file name, file type can be added to the time named. . . We did not talk much on the code
1 >>> import time 2 >>> time.time() 3 1577015630.9973366 4 >>> time.ctime() 5 'Sun Dec 22 19:53:55 2019' 6 >>> time.localtime() 7 time.struct_time(tm_year=2019, tm_mon=12, tm_mday=22, tm_hour=19, tm_min=54, tm_sec=3, tm_wday=6, tm_yday=356, tm_isdst=0) 8 >>> time.strftime("%Y/%m/%d %H:%M:%S") 9 '2019/12/22 19:55:03'
time.time () # Get the current time twist
time.ctime () # get the current time string
time.localtime () # the current time struct_time form
time.strftime () # current time, and converted to a string format
The use of the above function of time before the code to add to, change the name of the test report.
. 1 Import the unittest 2 Import Time . 3 from HTMLTestRunner Import HTMLTestRunner . 4 . 5 now The time.strftime = ( " % D%% Y-M-% H_% of M_% S " ) # must not prohibit the use of a file name including special symbols . 6 test_dir = ' ./ ' # current path . 7 Discover = unittest.defaultTestLoader.discover (test_dir, pattern = " the IOT of _ *. py ' ) # the IOT of _ *. name py containing the test case . 8 filename = now + + test_dir ' iot.html ' . 9 fp = open (filename," WB " ) # Paths report storage 10 Runner = HTMLTestRunner (Stream = FP, title = ' Kexin cloud test report ' , Description = ' test case: ' ) . 11 runner.run (Discover) 12 is fp.close ()
Here encountered a pit, not the name of the generated test report with / and: and some special characters, otherwise it will report the following error