HTML报告
- 直接把HTMLTestRunner.py放入工程目录即可
- 报告脚本封装
代码语言:python
代码运行次数:0
复制#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
- 直接引用即可
代码语言:python
代码运行次数:0
复制import logging
- log等级
- logging.basicConfig()函数包含参数说明
- logging模块中定义好的可以用于format格式字符串说明
- 生成log脚本封装
代码语言:python
代码运行次数:0
复制#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函数入口集成报告和日志
代码语言:python
代码运行次数:0
复制# 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)