如何生成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报告

三、测试报告详情

四、参考代码

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @Author : chen
  4. # @File : run_main.py
  5. # @Software: PyCharm
  6. import os
  7. import unittest
  8. import time
  9. import HTMLTestRunner
  10. import smtplib
  11. from email.mime.text import MIMEText
  12. from email.mime.multipart import MIMEMultipart
  13.  
  14. cur_path = os.path.dirname(os.path.realpath(__file__))
  15.  
  16. def add_case(caseName='case',rule='test*.py'):
  17. case_path = os.path.join(cur_path, caseName)
  18. if not os.path.exists(case_path):
  19. os.mkdir(case_path)
  20. print('test case path:%s' %case_path)
  21. discover = unittest.defaultTestLoader.discover(case_path,
  22. pattern=rule,
  23. top_level_dir=None)
  24. print(discover)
  25. return discover
  26.  
  27. def run_case(all_case,reportName='report'):
  28. now = time.strftime('%Y_%m_%d_%H_%M_%S')
  29. report_path = os.path.join(cur_path, reportName)
  30. if not os.path.exists(report_path):
  31. os.mkdir(report_path)
  32. report_abspath = os.path.join(report_path, now + 'result.html')
  33. print('test case path:%s' %report_abspath)
  34. fp = open(report_abspath, 'wb')
  35. runner = HTMLTestRunner.HTMLTestRunner(stream=fp,
  36. title='自动化测试报告:',
  37. description='用例执行情况')
  38. runner.run(all_case)
  39. fp.close()
  40.  
  41. def get_report_file(report_path):
  42. lists = os.listdir(report_path)
  43. lists.sort(key=lambda fn: os.path.getmtime(os.path.join(report_path, fn)))
  44. print('最新测试生成报告:' + lists[-1])
  45. report_file = os.path.join(report_path, lists[-1])
  46. return report_file
  47.  
  48. def send_mail(sender,password,receiver,smptserver,report_file,port):
  49. with open(report_file, 'rb') as f:
  50. mail_body = f.read()
  51.  
  52. msg = MIMEMultipart()
  53. body = MIMEText(mail_body,_subtype='html',_charset='utf-8')
  54. msg["subject"] = "自动化测试报告"
  55. msg["from"] = sender
  56. msg["to"] = receiver
  57. msg.attach(body)
  58.  
  59. att = MIMEText(open(report_file,'rb').read(), 'base64', 'utf-8')
  60. att['Content-Type'] = 'application/octet-stream'
  61. att['Content-Disposition'] = 'attachment; filename="report.html"'
  62. msg.attach(att)
  63. try:
  64. smtp = smtplib.SMTP_SSL(smptserver,port)
  65. except:
  66. smtp = smtplib.SMTP()
  67. smtp.connect(smptserver,port)
  68.  
  69. smtp.login(sender,password)
  70. smtp.sendmail(sender,receiver,msg.as_string())
  71. smtp.quit()
  72. print('test report email has send out !')
  73.  
  74. if __name__ == "__main__":
  75. all_case = add_case()
  76. run_case(all_case)
  77. report_path = os.path.join(cur_path,'report')
  78. report_file = get_report_file(report_path)
  79.  
  80. from jiekou_test.config import readConfig
  81. sender = readConfig.sender
  82. password = readConfig.password
  83. smtp_server = readConfig.smtp_server
  84. port = readConfig.port
  85. receiver = readConfig.receiver
  86. send_mail(sender,password,receiver,smtp_server,report_file,port)
写在最后的话:这些都是小编自己一个字一个字敲上去的,原创算不上,可能很多类似的资料,小编写这个的目的是为了激励自己在学习道路上养成良好的习惯,所以转载请注明出处,谢谢!

自动化测试基础篇--Selenium unittest生成测试报告(HTMLTestRunner)的更多相关文章

  1. 自动化测试基础篇--Selenium unittest简介

    一.什么是unittest unittest是Python单元测试框架,类似于JUnit框架. unittest中有4个重要的概念:test fixture, test case, test suit ...

  2. 3.5 unittest生成测试报告HTMLTestRunner

    3.5 unittest生成测试报告HTMLTestRunner 前言批量执行完用例后,生成的测试报告是文本形式的,不够直观,为了更好的展示测试报告,最好是生成HTML格式的.unittest里面是不 ...

  3. python接口自动化测试(七)unittest 生成测试报告

    用例的管理问题解决了后,接下来要考虑的就是报告我问题了,这里生成测试报告主要用到 HTMLTestRunner.py 这个模块,下面简单介绍一下如何使用: 一.下载HTMLTestRunner下载: ...

  4. 自动化测试基础篇--Selenium发送测试报告邮件

    来自:https://www.cnblogs.com/sanzangTst/p/8377870.html 发邮件需要用到python两个模块,smtplib和email,这俩模块是python自带的, ...

  5. 自动化测试基础篇--Selenium框架设计(POM)

    一.自动化测试框架 感谢木棉花的漂泊分享,内容转自链接:http://www.cnblogs.com/fengyiru6369/p/8053035.html 1.什么是自动化测试框架 简单来说,自动化 ...

  6. 自动化测试基础篇--Selenium简单的163邮箱登录实例

    摘自https://www.cnblogs.com/sanzangTst/p/7472556.html 前面几篇内容一直讲解Selenium Python的基本使用方法.学习了什么是selenium: ...

  7. 自动化测试基础篇--Selenium元素定位

    摘自https://www.cnblogs.com/sanzangTst/p/7457111.html 一.Selenium元素定位的重要性: Web自动化测试的操作:获取UI页面的元素,对元素进行操 ...

  8. 自动化测试基础篇--Selenium文件上传send_keys

    摘自https://www.cnblogs.com/sanzangTst/p/8358165.html 文件上传是web页面上很常见的一个功能,自动化成功中操作起来却不是那么简单. 一般分两个场景:一 ...

  9. 自动化测试基础篇--Selenium鼠标键盘事件

    摘自https://www.cnblogs.com/sanzangTst/p/7477382.html 前面几篇文章我们学习了怎么定位元素,同时通过实例也展示了怎么切换到iframe,怎么输入用户名和 ...

随机推荐

  1. 【Kafka】Consumer配置

    从0.9.0.0开始,下面是消费者的配置. 名称 描述 类型 默认值 bootstrap.servers 消费者初始连接kafka集群时的地址列表.不管这边配置的什么地址,消费者会使用所有的kafka ...

  2. php 使用 rabbitmq

    1,配置好rabbitmq 服务器 (参照 http://www.cnblogs.com/spicy/p/7017603.html)(我是linux) 2,新增了一个用户 并点击该用户 增加权限如下

  3. 《LeetBook》LeetCode题解(2):Add Two Numbers [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  4. 用通俗的语言解释 Spring 中的 DI 、IOC 和AOP概念

    DI 所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B,反正A要用到B,则A依赖于B.所谓倒置,你必须理解如果不倒置,会怎么着,因为A必须要有B,才可以调用B,如果不倒置,意思就是 ...

  5. Vue 中 export default 和 module.exports

    export default 服从 ES6 的规范,补充:default 其实是别名 module.exports 服从CommonJS 规范 一般导出一个属性或者对象用 export default ...

  6. 设置spacevim字体显示乱码问题

    https://github.com/powerline/fonts clone powerline fonts 仓库 执行项目中的 install.sh 安装字体 修改终端配置中使用的字体为 xxx ...

  7. 深入redis内部--字典实现

    redis的字典定义和实现在dict.h和dict.c文件中. 1.字典结构 typedef struct dict { dictType *type; //定义了字典需要的函数 void *priv ...

  8. Hadoop学习笔记(10) ——搭建源码学习环境

    Hadoop学习笔记(10) ——搭建源码学习环境 上一章中,我们对整个hadoop的目录及源码目录有了一个初步的了解,接下来计划深入学习一下这头神象作品了.但是看代码用什么,难不成gedit?,单步 ...

  9. js只允许输入数字和两位小数

    一.js只允许输入数字和两位小数 //只允许输入数字和两位小数 function clearNoNum(obj) { obj.value = obj.value.replace(/[^\d.]/g, ...

  10. 二、hdfs单节点安装

    一.准备环境 在配置hdfs之前,我们需要先安装好hadoop的配置,本文主要讲述hdfs单节点的安装配置. hadoop的单节点安装配置请参考:https://www.cnblogs.com/lay ...