class take_screen_shot():  #这个类将在下面作为装饰器使用


    def __init__(self, func):
        self.func = func
        self.name = func.__name__ + ' (error).png'  #拼接截图文件名

    def __call__(self, *args):   #对每次调用的函数都做截图操作
        try:
            self.func(self, *args)
        except Exception as msg:
            pic=self.driver.get_screenshot_as_file('C:\\Users\\zhengtao\\Desktop\\pic\\'+self.name) 
            return pic
            raise msg




class HotelBook(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        desired_caps={}
        desired_caps['platformName']='Android'
        desired_caps['platformVersion']='6.0.1'
        desired_caps['deviceName']='MI NOTE LTE'
        desired_caps['appPackage']='com.tuniu.app.ui'
        desired_caps['appActivity']='.homepage.LaunchActivity'

        desired_caps['unicodeKeyboard']=True
        desired_caps['resetKeyboard']=True

        self.driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)

        self.driver.implicitly_wait(10)



    @take_screen_shot
    def simpletest(self):

        self.driver.find_element_by_id('com.tuniu.app.ui:id/iv_style3_card_1').click()
        self.driver.find_element_by_id('com.tuniu.hotel:id/tv_query222').click()


if __name__ == '__main__':

    suite = unittest.TestSuite()

    suite.addTest(HotelBook('simpletest'))

    timestr = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
    filename = 'D:\\report\\HotelBook_result_'+timestr+'.html'
    fp = open(filename,'wb')
    runner = HTMLTestReportCN.HTMLTestRunner(
        stream=fp,
        title='HotelBook测试报告',
        description='HotelBook测试报告',
        tester='测试'
    )
    runner.run(suite)
    fp.close()

下面是我的 HTML 测试报告输出的部分代码

<!-- 默认展开错误信息 -Findyou -->
    <button id='btn_%(tid)s' type="button"  class="btn btn-danger btn-xs" data-toggle="collapse" data-target='#div_%(tid)s'>%(status)s</button>
    <div id='div_%(tid)s' class="collapse in"> 
    <pre>
    <img src="C:\Users\zhengtao\Desktop\pic\%(pic)s" width="500" height="auto">
    %(script)s
    </pre>

代码执错误信息:

ETraceback (most recent call last):
File "D:\appium\workspace\newtest\AppRegressionTest\ForTest\pic.py", line 83, in
runner.run(suite)
File "C:\Users\zhengtao\AppData\Local\Programs\Python\Python35\lib\HTMLTestReportCN.py", line 574, in run
self.generateReport(test, result)
File "C:\Users\zhengtao\AppData\Local\Programs\Python\Python35\lib\HTMLTestReportCN.py", line 624, in generateReport
report = self._generate_report(result)
File "C:\Users\zhengtao\AppData\Local\Programs\Python\Python35\lib\HTMLTestReportCN.py", line 689, in _generate_report
self._generate_report_test(rows, cid, tid, n, t, o, e)
File "C:\Users\zhengtao\AppData\Local\Programs\Python\Python35\lib\HTMLTestReportCN.py", line 740, in _generate_report_test
status = self.STATUS[n],
KeyError: 'pic'

我是 python 初学者,请问我这样写的输出截图的代码问题原因在哪里?
或者各位前辈有比较好的测试报告带截图的方法,可以借小弟借鉴一下呢?


↙↙↙阅读原文可查看相关链接,并与作者交流