自动化测试基础篇--Selenium unittest生成测试报告(HTMLTestRunner)

如何生成HTMLTestRunner测试报告。接上篇文章,对于unittest框架,运行后,测试结果不便于查看,同时多个case存在的时候,可能会导致case result记录不正确的情况。

为此,引入了HTMLTestRunner.py,它是Python标准库unittest模块的一个扩展。它可以生成直观的HTML测试报告。

一、下载HTMLTestRuner

首先,下载HTMLTestRuner.py文件。

下载地址:Python2.7版本:http://tungwaiyip.info/software/HTMLTestRunner.html

Python3.0版本:https://github.com/huilansame/HTMLTestRunner_PY3

二、生成HTMLTestRuner报告

三、测试报告详情

四、参考代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : chen
# @File    : run_main.py
# @Software: PyCharm
import os
import unittest
import time
import HTMLTestRunner
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

cur_path = os.path.dirname(os.path.realpath(__file__))

def add_case(caseName='case',rule='test*.py'):
    case_path = os.path.join(cur_path, caseName)
    if not os.path.exists(case_path):
        os.mkdir(case_path)
    print('test case path:%s' %case_path)
    discover = unittest.defaultTestLoader.discover(case_path,
                                                   pattern=rule,
                                                   top_level_dir=None)
    print(discover)
    return discover

def run_case(all_case,reportName='report'):
    now = time.strftime('%Y_%m_%d_%H_%M_%S')
    report_path = os.path.join(cur_path, reportName)
    if not os.path.exists(report_path):
        os.mkdir(report_path)
    report_abspath = os.path.join(report_path, now + 'result.html')
    print('test case path:%s' %report_abspath)
    fp = open(report_abspath, 'wb')
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp,
                                           title='自动化测试报告:',
                                           description='用例执行情况')
    runner.run(all_case)
    fp.close()

def get_report_file(report_path):
    lists = os.listdir(report_path)
    lists.sort(key=lambda fn: os.path.getmtime(os.path.join(report_path, fn)))
    print('最新测试生成报告:' + lists[-1])
    report_file = os.path.join(report_path, lists[-1])
    return report_file

def send_mail(sender,password,receiver,smptserver,report_file,port):
    with open(report_file, 'rb') as f:
        mail_body = f.read()

    msg = MIMEMultipart()
    body = MIMEText(mail_body,_subtype='html',_charset='utf-8')
    msg["subject"] = "自动化测试报告"
    msg["from"] = sender
    msg["to"] = receiver
    msg.attach(body)

    att = MIMEText(open(report_file,'rb').read(), 'base64', 'utf-8')
    att['Content-Type'] = 'application/octet-stream'
    att['Content-Disposition'] = 'attachment; filename="report.html"'
    msg.attach(att)
    try:
        smtp = smtplib.SMTP_SSL(smptserver,port)
    except:
        smtp = smtplib.SMTP()
        smtp.connect(smptserver,port)

    smtp.login(sender,password)
    smtp.sendmail(sender,receiver,msg.as_string())
    smtp.quit()
    print('test report email has send out !')

if __name__ == "__main__":
    all_case = add_case()
    run_case(all_case)
    report_path = os.path.join(cur_path,'report')
    report_file = get_report_file(report_path)

    from jiekou_test.config import readConfig
    sender = readConfig.sender
    password = readConfig.password
    smtp_server = readConfig.smtp_server
    port = readConfig.port
    receiver = readConfig.receiver
    send_mail(sender,password,receiver,smtp_server,report_file,port)
写在最后的话:这些都是小编自己一个字一个字敲上去的,原创算不上,可能很多类似的资料,小编写这个的目的是为了激励自己在学习道路上养成良好的习惯,所以转载请注明出处,谢谢!
posted @ 2017-09-21 15:04  叁藏法师  阅读(842)  评论(0编辑  收藏  举报