1. Download htmltestrunner.py
Htmltestrunner is an extension of the UnitTest module of the Python Standard library. It generates easy-to-use HTML test reports. Htmltestrunner is issued under the BSD license.
Download Address: http://tungwaiyip.info/software/HTMLTestRunner.html
Windows: Put the downloaded files into the PYTHON34 installation directory ... \python34\lib directory
2. Import the Htmltestrunner module package
1 fromHtmltestrunnerImportHtmltestrunner2 ImportUnitTest3 fromTimeImportstrftime or import time,datetime4 ImportOS5 fromEmail.mime.textImportMimetext6 7 fromEmail.mime.multipartImportMimemultipart#Message Format8 9 fromEmail.mime.imageImportMimeimage#send a message with picture
3. Defining test Case paths
' ./testcase/ '
Or
' test_case\\testcase '
4. Find Elements (TestCase)
Discover = Unittest.defaultTestLoader.discover (Test_dir,'*case.py')
5. Define the test report file name and take the previous time to add to the test report file name
# define report Storage path, support relative path
' ./report/ ' + strftime ('%y_%m_%d_%h_%m_%s'. html' )
6. Create a file
fp = open (file_name,'wb')
Htmltestrunner (stream=fp,description=' description ', title=' title '). Run (Discover)
7. Close the file
Fp.close ()
Attachment:
#-*-coding:utf-8-*-ImportUnitTest fromHtmltestrunnerImportHtmltestrunnerImportTime,os,datetimeImportSmtplib fromEmail.mime.textImportMimetext fromEmail.mime.multipartImportMimemultipart fromEmail.mime.imageImportMimeimage#fetch all use case files under the Test_case folderdefCreatsuitel (lists): Testunit=UnitTest. TestSuite ()#Discover method DefinitionDiscover = Unittest.defaultTestLoader.discover (lists, pattern='start_*.py', top_level_dir=None)#the Discover method filters out the use cases, loops to add to the test suite forTest_suiteinchDiscover: forTest_caseinchtest_suite:testunit.addTests (test_case)Print(Testunit)returntestunitlist_1='Test_case\\test_case_1'Alltestnames=Creatsuitel (list_1)#take the previous time to add to the test report file namenow = Time.strftime ("%y-%m-%m-%h_%m_%s", Time.localtime (Time.time ())) filename="report\\"+now+'result.html' #defines a report storage path that supports relative paths. fp = open (filename,'WB') Runner= Htmltestrunner (STREAM=FP, title='Report_title', description='report_description')if __name__=="__main__": #executing test case sets and generating reportsRunner = UnitTest. Texttestrunner ()
Generating a test report file in HTML format using the Htmltestrunner module