HTMLTestRunner+uiautomator+unittest生成带截图链接的测试报告

1、更改HTMLTestRunner.py文件里的内容

<tr id='header_row'>
    <td>Test Group/Test casetd>
    <td>Counttd>
    <td>Passtd>
    <td>Failtd>
    <td>Errortd>
    <td>Viewtd>
    <td>Picturetd>
tr>
%(test_list)s
<tr id='total_row'>
    <td>Totaltd>
    <td>%(count)std>
    <td>%(Pass)std>
    <td>%(fail)std>
    <td>%(error)std>
    <td> td>
    <td><a href="" target="_blank">a>td>
tr>

<tr class='%(style)s'>
    <td>%(desc)std>
    <td>%(count)std>
    <td>%(Pass)std>
    <td>%(fail)std>
    <td>%(error)std>
    <td><a href="javascript:showClassDetail('%(cid)s',%(count)s)">Detaila>td>
    <td><a href="" target="_blank">a>td>
tr>

<tr id='%(tid)s' class='%(Class)s'>
    <td class='%(style)s'><div class='testcase'>%(desc)sdiv>td>
    <td colspan='5' align='center'>

    
    <a class="popup_link" onfocus='this.blur();' href="javascript:showTestDetail('div_%(tid)s')" >
        %(status)sa>

    <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>
    

    td>
    %(html)s
tr>

2、封装的手机截屏方法中加入分别将截图名称和截图存放路径插入对应的数组中(也可以使用二维数组),以下分别为打印日志,截屏,组装html的方法,在组装完成的html代码开头和结尾分别叫上htmlbegin和htmlend作为之后截取使用的关键字

def screenshot():
    print u"截图执行"
    dirname = PATH('E:\\code\\auto' + "/screenshot")
    if not os.path.isdir(dirname):
        os.makedirs(dirname)
    pic = time.strftime("%y%m%d%H%M%S")+str(random.randint(1,100))+".png"
    path = dirname + "/" + pic
    d.screenshot(path)
    pics.append(pic)
    picpath.append(path)


def creathtml(path, pic):
    html = ''
    if range(len(path)) > 0:
        for i in range(len(path)):
            if i == 0:
                html = '' + pic[i] + ''
            else:
                html = html + '
'
+ pic[i] + '' else: html = '' htmls = 'htmlbegin' + html +'htmlend' return htmls

3、在用例中使用截图和打印html就ok了

def test_case_01(self):
        print u"用例执行"
        d(text=u"百度新闻").click()
        time.sleep(3)
        screenshot()
        time.sleep(2)
        d(text=u"我的").click()
        time.sleep(2)
        screenshot()
        print creathtml(picpath, pics)
        self.assertTrue(d(text=u"请输入账号").exists)



if __name__ == '__main__':

    suite = unittest.TestSuite()
    suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestEle))
    now = time.strftime('%Y-%m-%d', time.localtime(time.time()))
    test_dir = r'E:\code\auto'
    filename = test_dir + '/' + now + 'test_result.html'
    fp = file(filename,'wb')
    runner = HTMLTestRunner(stream=fp, title=u'自动化测试', description= u'AI淘客测试结果',verbosity=2)
    runner.run(suite)

你可能感兴趣的:(学习)