HTMLTestRunner实现报告中的截图

前一篇写到给unittest 通过装饰器增加截图功能。现在还想再HTMLTestRunner中,让截图出现在报告里。
结果如图:
这里写图片描述
点击可打开连接

实现步骤如下:

于是先在原来的装饰器中把图片的名字输出出来:

    def add(func):
        PATH = lambda p: os.path.abspath(p)
        def wrapper(self, first, second, msg=None):
            try:
                func(self, first, second, msg=None)
            except AssertionError:
                path = PATH(os.getcwd() + "/screenshot")
                timestamp = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
                os.popen("adb wait-for-device")
                os.popen("adb shell screencap -p /data/local/tmp/tmp.png")
                if not os.path.isdir(PATH(os.getcwd() + "/screenshot")):
                    os.makedirs(path)
                os.popen("adb pull /data/local/tmp/tmp.png " + PATH(path + "/" + timestamp + ".png"))
                os.popen("adb shell rm /data/local/tmp/tmp.png")
                #打印出路径
                print  timestamp , '.png'
                raise AssertionError,msg
        return wrapper

修改处如下:


    REPORT_TMPL = """
<p id='show_detail_line'>Show
<a href='javascript:showCase(0)'>Summary</a>
<a href='javascript:showCase(1)'>Failed</a>
<a href='javascript:showCase(2)'>All</a>
</p>
<table id='result_table'>
<colgroup>
<col align='left' />
<col align='right' />
<col align='right' />
<col align='right' />
<col align='right' />
<col align='right' />
</colgroup>
<tr id='header_row'>
    <td>Test Group/Test case</td>
    <td>Count</td>
    <td>Pass</td>
    <td>Fail</td>
    <td>Error</td>
    <td>Skip</td>
    <td>View</td>
    <td>Screenshot</td>#增加一列
</tr>
%(test_list)s
<tr id='total_row'>
    <td>Total</td>
    <td>%(count)s</td>
    <td>%(Pass)s</td>
    <td>%(fail)s</td>
    <td>%(error)s</td>
    <td>%(skip)s</td>
    <td> </td>
    <td> <a href="" target="_blank"></a></td>

</tr>
</table>
"""  # variables: (test_list, count, Pass, fail, error)

下两处增加了图片行

REPORT_TEST_WITH_OUTPUT_TMPL = r"""
<tr id='%(tid)s' class='%(Class)s'>
    <td class='%(style)s'><div class='testcase'>%(desc)s</div></td>
    <td colspan='6' align='center'>
    <!--css div popup start-->
    <a class="popup_link" onfocus='this.blur();' href="javascript:showTestDetail('div_%(tid)s')" >
        %(status)s</a>

    <div id='div_%(tid)s' class="popup_window" >
        <div style='text-align: right; color:red;cursor:pointer'>
        <a onfocus='this.blur();' onclick="document.getElementById('div_%(tid)s').style.display = 'none' " >
           [x]</a>
        </div>
        <pre>
        %(script)s
        </pre>
    </div>
    <!--css div popup end-->
    </td>
    <td align='center'>
    <a  %(hidde)s  href="%(image)s">picture_shot</a>
    </td>
</tr>
"""  # variables: (tid, Class, style, desc, status)
    REPORT_TEST_NO_OUTPUT_TMPL = r"""
<tr id='%(tid)s' class='%(Class)s'>
    <td class='%(style)s'><div class='testcase'>%(desc)s</div></td>
    <td colspan='6' align='center'>%(status)s</td>
    <td align='center'>
    <a  %(hidde)s  href="%(image)s">picture_shot
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值