共计 7544 个字符,预计需要花费 19 分钟才能阅读完成。
HTML 报告
- 间接把 HTMLTestRunner.py 放入工程目录即可
- 报告脚本封装
#HTNL 格局报告
now = datetime.datetime.now().strftime('%Y-%m-%d_%H_%M_%S')
htmlreport = reportpath + "/" + now + r"result.html"
print("测试报告生成地址:%s"% htmlreport)
fp = open(htmlreport, "wb")
runner = HTMLTestRunner.HTMLTestRunner(stream=fp,
verbosity=2,
title="xxxx 接口自动化测试报告",
description="用例执行状况")
runner.run(case) # case 为所有的测试用例
fp.close()
LOG 日志
- 应用 Python 自带的 logging
- 间接援用即可
import logging
- log 等级
<span class=”td-span”><span> 级别 </span></span> | <span class=”td-span”><span> 何时应用 </span></span> |
---|---|
<span class=”td-span”><span>DEBUG</span></span> | <span class=”td-span”><span> 详细信息,典型地调试问题时会感兴趣。具体的 debug 信息。</span></span> |
<span class=”td-span”><span>INFO</span></span> | <span class=”td-span”><span> 证实事件按预期工作。要害事件。</span></span> |
<span class=”td-span”><span>WARNING</span></span> | <span class=”td-span”><span> 表明产生了一些意外,或者不久的未来会产生问题(如‘磁盘满了’)。软件还是在失常工作。</span></span> |
<span class=”td-span”><span>ERROR</span></span> | <span class=”td-span”><span> 因为更重大的问题,软件已不能执行一些性能了。个别谬误音讯。</span></span> |
<span class=”td-span”><span>CRITICAL</span></span> | <span class=”td-span”><span> 严重错误,表明软件已不能持续运行了。</span></span> |
<span class=”td-span”><span>NOTICE</span></span> | <span class=”td-span”><span> 不是谬误,然而可能须要解决。一般然而重要的事件。</span></span> |
<span class=”td-span”><span>ALERT</span></span> | <span class=”td-span”><span> 须要立刻修复,例如零碎数据库损坏。</span></span> |
<span class=”td-span”><span>EMERGENCY</span></span> | <span class=”td-span”><span> 紧急情况,零碎不可用(例如零碎解体),个别会告诉所有用户。</span></span> |
- logging.basicConfig() 函数蕴含参数阐明
<span class=”td-span”><span> 参数名称 </span></span> | <span class=”td-span”><span> 形容 </span></span> |
---|---|
<span class=”td-span”><span>filename</span></span> | <span class=”td-span”><span> 指定日志输入指标文件的文件名(能够写文件名也能够写文件的残缺的绝对路径,写文件名日志放执行文件目录下,写残缺门路依照残缺门路生成日志文件),指定该设置项后日志信念就不会被输入到控制台了 </span></span> |
<span class=”td-span”><span>filemode</span></span> | <span class=”td-span”><span> 指定日志文件的关上模式,默认为 ’a’。须要留神的是,该选项要在 filename 指定时才无效 </span></span> |
<span class=”td-span”><span>format</span></span> | <span class=”td-span”><span> 指定日志格局字符串,即指定日志输入时所蕴含的字段信息以及它们的程序。logging 模块定义的格局字段上面会列出。</span></span> |
<span class=”td-span”><span>datefmt</span></span> | <span class=”td-span”><span> 指定日期 / 工夫格局。须要留神的是,该选项要在 format 中蕴含工夫字段 %(asctime)s 时才无效 </span></span> |
<span class=”td-span”><span>level</span></span> | <span class=”td-span”><span> 指定日志器的日志级别 </span></span> |
<span class=”td-span”><span>stream</span></span> | <span class=”td-span”><span> 指定日志输入指标 stream,如 sys.stdout、sys.stderr 以及网络 stream。须要阐明的是,stream 和 filename 不能同时提供,否则会引发 <span>ValueError <span> 异样 </span></span></span></span> |
<span class=”td-span”><span>style</span></span> | <span class=”td-span”><span>Python 3.2 中新增加的配置项。指定 format 格局字符串的格调,可取值为 ’%’、'{‘ 和 ’$’,默认为 ’%'</span></span> |
<span class=”td-span”><span>handlers</span></span> | <span class=”td-span”><span>Python 3.3 中新增加的配置项。该选项如果被指定,它应该是一个创立了多个 Handler 的可迭代对象,这些 handler 将会被增加到 root logger。须要阐明的是:filename、stream 和 handlers 这三个配置项只能有一个存在,不能同时呈现 2 个或 3 个,否则会引发 ValueError 异样。</span></span> |
- logging 模块中定义好的能够用于 format 格局字符串阐明
<span class=”td-span”><span> 字段 / 属性名称 </span></span> | <span class=”td-span”><span> 应用格局 </span></span> | <span class=”td-span”><span> 形容 </span></span> |
---|---|---|
<span class=”td-span”><span>asctime</span></span> | <span class=”td-span”><span>%(asctime)s</span></span> | <span class=”td-span”><span> 将日志的工夫结构成可读的模式,默认状况下是‘2016-02-08 12:00:00,123’准确到毫秒 </span></span> |
<span class=”td-span”><span>name</span></span> | <span class=”td-span”><span>%(name)s</span></span> | <span class=”td-span”><span> 所应用的日志器名称,默认是 ’root’,因为默认应用的是 rootLogger</span></span> |
<span class=”td-span”><span>filename</span></span> | <span class=”td-span”><span>%(filename)s</span></span> | <span class=”td-span”><span> 调用日志输入函数的模块的文件名;pathname 的文件名局部,蕴含文件后缀 </span></span> |
<span class=”td-span”><span>funcName</span></span> | <span class=”td-span”><span>%(funcName)s</span></span> | <span class=”td-span”><span> 由哪个 function 收回的 log,调用日志输入函数的函数名 </span></span> |
<span class=”td-span”><span>levelname</span></span> | <span class=”td-span”><span>%(levelname)s</span></span> | <span class=”td-span”><span> 日志的最终等级(被 filter 批改后的)</span></span> |
<span class=”td-span”><span>message</span></span> | <span class=”td-span”><span>%(message)s</span></span> | <span class=”td-span”><span> 日志信息,日志记录的文本内容 </span></span> |
<span class=”td-span”><span>lineno</span></span> | <span class=”td-span”><span>%(lineno)d</span></span> | <span class=”td-span”><span> 以后日志的行号,调用日志输入函数的语句所在的代码行 </span></span> |
<span class=”td-span”><span>levelno</span></span> | <span class=”td-span”><span>%(levelno)s</span></span> | <span class=”td-span”><span> 该日志记录的数字模式的日志级别(10, 20, 30, 40, 50)</span></span> |
<span class=”td-span”><span>pathname</span></span> | <span class=”td-span”><span>%(pathname)s</span></span> | <span class=”td-span”><span> 残缺门路,调用日志输入函数的模块的残缺路径名,可能没有 </span></span> |
<span class=”td-span”><span>process</span></span> | <span class=”td-span”><span>%(process)s</span></span> | <span class=”td-span”><span> 以后过程,过程 ID。可能没有 </span></span> |
<span class=”td-span”><span>processName</span></span> | <span class=”td-span”><span>%(processName)s</span></span> | <span class=”td-span”><span> 过程名称,Python 3.1 新增 </span></span> |
<span class=”td-span”><span>thread</span></span> | <span class=”td-span”><span>%(thread)s</span></span> | <span class=”td-span”><span> 以后线程,线程 ID。可能没有 </span></span> |
<span class=”td-span”><span>threadName</span></span> | <span class=”td-span”><span>%(thread)s</span></span> | <span class=”td-span”><span> 线程名称 </span></span> |
<span class=”td-span”><span>module</span></span> | <span class=”td-span”><span>%(module)s</span></span> | <span class=”td-span”><span> 调用日志输入函数的模块名,filename 的名称局部,不蕴含后缀即不蕴含文件后缀的文件名 </span></span> |
<span class=”td-span”><span>created</span></span> | <span class=”td-span”><span>%(created)f</span></span> | <span class=”td-span”><span> 以后工夫,用 UNIX 规范的示意工夫的浮点数示意;日志事件产生的工夫 – 工夫戳,就是过后调用 time.time() 函数返回的值 </span></span> |
<span class=”td-span”><span>relativeCreated</span></span> | <span class=”td-span”><span>%(relativeCreated)d</span></span> | <span class=”td-span”><span> 输入日志信息时的,自 Logger 创立以 来的毫秒数;日志事件产生的工夫绝对于 logging 模块加载工夫的绝对毫秒数 </span></span> |
<span class=”td-span”><span>msecs</span></span> | <span class=”td-span”><span>%(msecs)d</span></span> | <span class=”td-span”><span> 日志事件产生事件的毫秒局部。logging.basicConfig() 中用了参数 datefmt,将会去掉 asctime 中产生的毫秒局部,能够用这个加上 </span></span> |
- 生成 log 脚本封装
#LOG 日志记录
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename=log_path + '/' + now + r"result.log",
filemode='w')
logger = logging.getLogger()
logger.info(case)
Unittest 函数入口集成报告和日志
# coding=utf-8
import unittest
import time,datetime
from common import HTMLTestRunner
from common.send_mail import sendmain
import os
import logging
now_path = os.path.dirname(os.path.realpath(__file__)) # 获取以后门路
report_path = os.path.join(now_path , "../report") # HTML 报告存储门路
log_path = os.path.join(now_path , "../log") # LOG 日志存储门路
if not os.path.exists(report_path): os.mkdir(report_path)
case_path = os.path.join(now_path , "../case") # 测试用例门路
def load_case(casepath=case_path, rule="test*.py"):
'''加载所有的测试用例'''
discover = unittest.defaultTestLoader.discover(casepath, pattern=rule,) # 定义 discover 办法的参数
return discover
def run_case(test_case, reportpath=report_path):
'''执行所有的用例, 并把后果写入测试报告'''
#HTNL 格局报告
now = datetime.datetime.now().strftime('%Y-%m-%d_%H_%M_%S')
report = reportpath + "/" + now + r"result.html"
print("测试报告生成地址:%s"% report)
fp = open(report, "wb")
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, verbosity=2, title="xxxx 接口自动化测试报告", description="用例执行状况")
#LOG 日志记录
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename=log_path + '/' + now + r"result.log",
filemode='w')
logger = logging.getLogger()
logger.info(test_case)
# 调用 load_case 函数返回值
runner.run(test_case)
fp.close()
time.sleep(2)
sendmain(report, mail_to=['yyyyyy@qq.com'])
print("发送测试报告邮件 OK")
if __name__ == "__main__":
my_cases = load_case()
run_case(my_cases)
正文完