"""
@Project: BeautifulReport
@Author: Raymond Mocobk
@Data: 2019/03/20
@File: __init__.py.py
@License: MIT
"""
import os
import sys
from io import StringIO as StringIO
import time
import json
import unittest
import platform
import base64
from distutils.sysconfig import get_python_lib
import traceback
from functools import wraps
__all__ = ['BeautifulReport']
HTML_IMG_TEMPLATE = """
<a href="https://img-blog.csdnimg.cn/2022010701293692640.png">
<img src="https://img-blog.csdnimg.cn/2022010701293692640.png" width="800px" height="500px"/>
</a>
<br></br>
"""
origin_stdout = sys.stdout
def output2console(s):
"""将stdout内容输出到console"""
tmp_stdout = sys.stdout
sys.stdout = origin_stdout
print(s, end='')
sys.stdout = tmp_stdout
class OutputRedirector(object):
""" Wrapper to redirect stdout or stderr """
def __init__(self, fp):
self.fp = fp
def write(self, s):
self.fp.write(s)
output2console(s)
def writelines(self, lines):
self.fp.writelines(lines)
def flush(self):
self.fp.flush()
stdout_redirector = OutputRedirector(sys.stdout)
stderr_redirector = OutputRedirector(sys.stderr)
SYSSTR = platform.system()
SITE_PAKAGE_PATH = get_python_lib()
class PATH:
""" all file PATH meta """
template_path = os.path.join(os.path.dirname(__file__), 'template')
config_tmp_path = os.path.join(template_path, 'template.html')
class MakeResultJson:
""" make html table tags """
def __init__(self, datas: tuple):
"""
init self object
:param datas: 拿到所有返回数据结构
"""
self.datas = datas
self.result_schema = {}
def __setitem__(self, key, value):
"""
:param key: self[key]
:param value: value
:return:
"""
self[key] = value
def __repr__(self) -> str:
"""
返回对象的html结构体
:rtype: dict
:return: self的repr对象, 返回一个构造完成的tr表单
"""
keys = (
'className',
'methodName',
'description',
'spendTime',
'status',
'log',
)
for key, data in zip(keys, self.datas):
self.result_schema.setdefault(key, data)
return json.dumps(self.result_schema)
class ReportTestResult(unittest.TestResult):
""" override"""
def __init__(self, suite, stream=sys.stdout):
""" pass """
super(ReportTestResult, self).__init__()
self.begin_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
self.start_time = 0
self.stream = stream
self.end_time = 0
self.failure_count = 0
self.error_count = 0
self.success_count = 0
self.skipped = 0
self.verbosity = 1
self.success_case_info = []
self.skipped_case_info = []
self.failures_case_info = []
self.errors_case_info = []
self.all_case_counter = 0
self.suite = suite
self.status = ''
self.result_list = []
self.case_log = ''
self.default_report_name = '自动化测试报告'
self.sys_stdout = None
self.sys_stderr = None
self.outpu
BeautifulReport(自己用得很水)
最新推荐文章于 2024-04-19 09:53:15 发布
本文分享了如何利用Python创建一份精美的报告,包括数据处理、图表制作和报告布局等关键步骤,适合初学者和进阶者提升技能。
摘要由CSDN通过智能技术生成