Automatic generation of automated reporting interfaces
To pre-import HTMLTestrunner module, if not HTMLTestrunner module, you can Baidu search download.
# ! / Usr / bin / the env to python3
# - * - Coding: UTF-. 8 - * -
# __author__: Hunter
Import Time
from HTMLTestRunner Import HTMLTestRunner introduced HTMLTestRunner module #
Import the unittest
# loading test procedure to discover encapsulated into a function
DEF allCase ():
case_dir = ' D: / hunter_ / interfaceTest / interface / Case ' # define the path test
Suite unittest.TestSuite = () # define a test suite
Discover = unittest.defaultTestLoader.discover (case_dir,
pattern = '. Test _ * Py ' ,
top_level_dir = None)
"" "
. 1, case_dir i.e. test directory
2, patten =' test _ * py ':. represented by the matching rule cases file name," * "means any number of characters, where means match all files that start with test_
3, top_level_dir = None:. test module top-level directory if not the top-level directory (that is not a test case on the multi-level directory), defaults to None
"" "
# the Discover method of screening out of use cases, added to the circulating test suite
for test_suit in Discover:
for test_case in test_suit:
suite.addTests (test_case)
Print (Suite)
'' ' directly added Discover ' ''
return suite
if __name__== ' __main__ ' :
allsuit = allCase () # instantiated
now = The time.strftime ( " % D%% Y-H-M-%%% M-S " ) # display the current time
filename = ' D: / hunter_ / interfaceTest / interface / report / ' + now + ' _result.html 'path # test report + time + time of the test report name
fp = open (filename, ' wb ' ) # open a file, create a test report HTML file, At this time, empty file; W is a write mode, b represents the binary mode (default mode of the pickle is stored in binary mode)
Runner = HTMLTestRunner (Stream = FP,
title= ' Interface test report ' ,
the Description = ' Implementation Example with ' ) #初始化一个HTMLTestRunner实力对象,用来生成报告
runner.run (allsuit) # run the test suite
Generated test report
Excellent ability of code you can modify to optimize HTMLTestrunner module allows test report more perfect; but also can increase the error screenshot function