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

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

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

一、下载HTMLTestRuner

首先,下载HTMLTestRuner.py文件。

下载地址:Python2.7版本:HTMLTestRunner - tungwaiyip's software

Python3.0版本:GitHub - huilansame/HTMLTestRunner_PY3: 针对PY3做了修改,增加对subTest的支持,用Echarts加了执行统计表

二、生成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)

感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接免费拿走:

① 2000多本软件测试电子书(主流和经典的书籍应该都有了)

② 软件测试/自动化测试标准库资料(最全中文版)

③ 项目源码(四五十个有趣且经典的练手项目及源码)

④ Python编程语言、API接口自动化测试、web自动化测试、App自动化测试(适合小白学习)


⑤ Python学习路线图(告别不入流的学习)

 上图的资料 在我的QQ技术交流群里(技术交流和资源共享,广告进来腿给你打断)

可以自助拿走,群号953306497(备注“csdn111”)群里的免费资料都是笔者十多年测试生涯的精华。还有同行大神一起交流技术哦。

猜你喜欢

转载自blog.csdn.net/ZangKang1/article/details/122393607
今日推荐