Python unittest如何生成HTMLTestRunner模块
生成HTMLTestRunner模块
unittest里面是不能生成html格式报告的,需要导入一个第三方的模块:HTMLTestRunner
方法1.这个模块下载不能通过pip安装了,只能下载后手动导入,下载地址:
http://tungwaiyip.info/software/HTMLTestRunner.html
方法2.在python安装文件的Lib目录下新增文件HTMLTestRunner.py
两种模板如下,建议使用第一种(第一种模板更加美观)
文件内容如下:
(1)第一种模板
#-*-coding:utf-8-*-
"""
ATestRunnerforusewiththePythonunittestingframework.It
generatesaHTMLreporttoshowtheresultataglance.
Thesimplestwaytousethisistoinvokeitsmainmethod.E.g.
importunittest
importHTMLTestRunner
...defineyourtests...
if__name__=='__main__':
HTMLTestRunner.main()
Formorecustomizationoptions,instantiatesaHTMLTestRunnerobject.
HTMLTestRunnerisacounterparttounittest'sTextTestRunner.E.g.
#outputtoafile
fp=file('my_report.html','wb')
runner=HTMLTestRunner.HTMLTestRunner(
stream=fp,
title='Myunittest',
description='ThisdemonstratesthereportoutputbyHTMLTestRunner.'
)
#Useanexternalstylesheet.
#SeetheTemplate_mixinclassformorecustomizableoptions
runner.STYLESHEET_TMPL=''
#runthetest
runner.run(my_test_suite)
------------------------------------------------------------------------
Copyright(c)2004-2007,WaiYipTung
Allrightsreserved.
Redistributionanduseinsourceandbinaryforms,withorwithout
modification,arepermittedprovidedthatthefollowingconditionsare
met:
*Redistributionsofsourcecodemustretaintheabovecopyrightnotice,
thislistofconditionsandthefollowingdisclaimer.
*Redistributionsinbinaryformmustreproducetheabovecopyright
notice,thislistofconditionsandthefollowingdisclaimerinthe
documentationand/orothermaterialsprovidedwiththedistribution.
*NeitherthenameWaiYipTungnorthenamesofitscontributorsmaybe
usedtoendorseorpromoteproductsderivedfromthissoftwarewithout
specificpriorwrittenpermission.
THISSOFTWAREISPROVIDEDBYTHECOPYRIGHTHOLDERSANDCONTRIBUTORS"AS
IS"ANDANYEXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOTLIMITED
TO,THEIMPLIEDWARRANTIESOFMERCHANTABILITYANDFITNESSFORA
PARTICULARPURPOSEAREDISCLAIMED.INNOEVENTSHALLTHECOPYRIGHTOWNER
ORCONTRIBUTORSBELIABLEFORANYDIRECT,INDIRECT,INCIDENTAL,SPECIAL,
EXEMPLARY,ORCONSEQUENTIALDAMAGES(INCLUDING,BUTNOTLIMITEDTO,
PROCUREMENTOFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,DATA,OR
PROFITS;ORBUSINESSINTERRUPTION)HOWEVERCAUSEDANDONANYTHEORYOF
LIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,ORTORT(INCLUDING
NEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUTOFTHEUSEOFTHIS
SOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.
"""
#URL:http://tungwaiyip.info/software/HTMLTestRunner.html
__author__="WaiYipTung"
__version__="0.9.1"
"""
ChangeHistory
Version0.9.1
*用Echarts添加执行情况统计图(灰蓝)
Version0.9.0
*改成Python3.x(灰蓝)
Version0.8.3
*使用Bootstrap稍加美化(灰蓝)
*改为中文(灰蓝)
Version0.8.2
*Showoutputinlineinsteadofpopupwindow(ViorelLupu).
Versionin0.8.1
*ValidatedXHTML(WolfgangBorgert).
*Addeddescriptionoftestclassesandtestcases.
Versionin0.8.0
*DefineTemplate_mixinclassforcustomization.
*WorkaroundaIE6bugthatitdoesnottreat
%(stylesheet)s
%(heading)s
%(report)s
%(ending)s
%(chart_script)s