我们都知道HTMLTESTRunner自动化测试报告,是Unittest单元测试框架报告,那么在做ui测试的时候就有点不适用了。
我们需要出错截图功能。
以下是我改的,增加了截图功能,先展示界面,再展示代码。
概要界面:
展开界面:
代码展示:
以下代码python2.7 和 python3.6两个版本
截图方式:
python2: print 'screenshot:', timestrmap, '.png' 在要截图的位置print就可以了。
python3: print('screenshot:', timestrmap, '.png' )
原理:展示时通过捕获unittest print中查找字符串“screenshot:”;找到就说明有截图,然后截取图片地址,写到html模版src=中。
截图代码:
@property def getImage(self): ''' 截取图片,并保存在images文件夹 :return: 无 ''' timestrmap = time.strftime('%Y%m%d_%H.%M.%S') imgPath = os.path.join(gl.imgPath, '%s.png' % str(timestrmap)) self.driver.save_screenshot(imgPath) print 'screenshot:', timestrmap, '.png'
以上代码需要注意的是gl.imgPath这个是存储图片的images文件夹的绝对路径.
另外需要注意的是images要与产生的report.html平级
HTMLTESTRunner.py 点击加号,展开代码
#coding=utf-8"""A TestRunner for use with the Python unit testing framework. Itgenerates a HTML report to show the result at a glance.The simplest way to use this is to invoke its main method. E.g. import unittest import HTMLTestRunner ... define your tests ... if __name__ == '__main__': HTMLTestRunner.main()For more customization options, instantiates a HTMLTestRunner object.HTMLTestRunner is a counterpart to unittest's TextTestRunner. E.g. # output to a file fp = file('my_report.html', 'wb') runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title='My unit test', description='This demonstrates the report output by HTMLTestRunner.' ) # Use an external stylesheet. # See the Template_mixin class for more customizable options runner.STYLESHEET_TMPL = ' ' # run the test runner.run(my_test_suite)------------------------------------------------------------------------Copyright (c) 2004-2007, Wai Yip TungAll rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.* Neither the name Wai Yip Tung nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "ASIS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITEDTO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR APARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNEROR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, ORPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."""# URL: http://tungwaiyip.info/software/HTMLTestRunner.html__author__ = "Wai Yip Tung, Findyou"__version__ = "0.8.2.1""""Change HistoryVersion 0.8.2.1 -Findyou* 支持中文,汉化* 调整样式,美化(需要连入网络,使用的百度的Bootstrap.js)* 增加 通过分类显示、测试人员、通过率的展示* 优化“详细”与“收起”状态的变换* 增加返回顶部的锚点Version 0.8.2* Show output inline instead of popup window (Viorel Lupu).Version in 0.8.1* Validated XHTML (Wolfgang Borgert).* Added description of test classes and test cases.Version in 0.8.0* Define Template_mixin class for customization.* Workaround a IE 6 bug that it does not treat %(stylesheet)s%(heading)s%(report)s%(ending)s""" # variables: (title, generator, stylesheet, heading, report, ending) # ------------------------------------------------------------------------ # Stylesheet # # alternatively use a for external style sheet, e.g. # STYLESHEET_TMPL = """""" # ------------------------------------------------------------------------ # Heading # HEADING_TMPL = """""" # variables: (title, parameters, description) HEADING_ATTRIBUTE_TMPL = """%(title)s
%(parameters)s%(description)s
%(name)s : %(value)s
""" # variables: (name, value) # ------------------------------------------------------------------------ # Report # # 汉化,加美化效果 --Findyou REPORT_TMPL = """概要{ %(passrate)s }错误{ %(error)s }失败{ %(fail)s }通过{ %(Pass)s }所有{ %(count)s }
用例集/测试用例 | 总计 | 通过 | 失败 | 错误 | 详细 | 截图 |
总计 | %(count)s | %(Pass)s | %(fail)s | %(error)s | 通过率:%(passrate)s |
#coding=utf-8"""A TestRunner for use with the Python unit testing framework. Itgenerates a HTML report to show the result at a glance.The simplest way to use this is to invoke its main method. E.g. import unittest import HTMLTestRunner ... define your tests ... if __name__ == '__main__': HTMLTestRunner.main()For more customization options, instantiates a HTMLTestRunner object.HTMLTestRunner is a counterpart to unittest's TextTestRunner. E.g. # output to a file fp = file('my_report.html', 'wb') runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title='My unit test', description='This demonstrates the report output by HTMLTestRunner.' ) # Use an external stylesheet. # See the Template_mixin class for more customizable options runner.STYLESHEET_TMPL = ' ' # run the test runner.run(my_test_suite)------------------------------------------------------------------------Copyright (c) 2004-2007, Wai Yip TungAll rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.* Neither the name Wai Yip Tung nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "ASIS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITEDTO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR APARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNEROR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, ORPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."""# URL: http://tungwaiyip.info/software/HTMLTestRunner.html__author__ = "Wai Yip Tung, Findyou"__version__ = "0.8.2.1""""Change HistoryVersion 0.8.2.1 -Findyou* 支持中文,汉化* 调整样式,美化(需要连入网络,使用的百度的Bootstrap.js)* 增加 通过分类显示、测试人员、通过率的展示* 优化“详细”与“收起”状态的变换* 增加返回顶部的锚点Version 0.8.2* Show output inline instead of popup window (Viorel Lupu).Version in 0.8.1* Validated XHTML (Wolfgang Borgert).* Added description of test classes and test cases.Version in 0.8.0* Define Template_mixin class for customization.* Workaround a IE 6 bug that it does not treat %(stylesheet)s%(heading)s%(report)s%(ending)s""" # variables: (title, generator, stylesheet, heading, report, ending) # ------------------------------------------------------------------------ # Stylesheet # # alternatively use a for external style sheet, e.g. # STYLESHEET_TMPL = """""" # ------------------------------------------------------------------------ # Heading # HEADING_TMPL = """""" # variables: (title, parameters, description) HEADING_ATTRIBUTE_TMPL = """%(title)s
%(parameters)s%(description)s
%(name)s : %(value)s
""" # variables: (name, value) # ------------------------------------------------------------------------ # Report # # 汉化,加美化效果 --Findyou REPORT_TMPL = """概要{ %(passrate)s }错误{ %(error)s }失败{ %(fail)s }通过{ %(Pass)s }所有{ %(count)s }
用例集/测试用例 | 总计 | 通过 | 失败 | 错误 | 详细 | 截图 |
总计 | %(count)s | %(Pass)s | %(fail)s | %(error)s | 通过率:%(passrate)s |