BeautifulReport测试报告
获取BeautifulReport模块
BeautifulReport 源码Clone地址为 BeautifulReport ,其中BeautifulReport.py和其template是我们需要的
BeautifulReport
如下代码是BeautifulReport.py的源码,其中几个注释的地方需要注意,将其集成进自己的自动化框架时需要做相应的修改
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/2022010612381251710.png"><img src="https://img-blog.csdnimg.cn/2022010612381251710.png" width="800px" height="500px"/></a><br></br>
"""class OutputRedirector(object):""" Wrapper to redirect stdout or stderr """def __init__(self, fp):self.fp = fpdef write(self, s):self.fp.write(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()FIELDS = {"testPass": 0,"testResult": [],"testName": "","testAll": 0,"testFail": 0,"beginTime": "","totalTime": "","testSkip": 0
}class PATH:""" all file PATH meta """config_tmp_path = 'D:\\Programs\\Python\\PythonUnittest\\Template\\template'class MakeResultJson:""" make html table tags """def __init__(self, datas: tuple):"""init self object:param datas: 拿到所有返回数据结构"""self.datas = datasself.result_schema = {}def __setitem__(self, key, value):""":param key: self[key]:param value: value:return:"""self[key] = valuedef __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 = 0self.stream = streamself.end_time = 0self.failure_count = 0self.error_count = 0self.success_count = 0self.skipped = 0self.verbosity = 1self.success_case_info = []self.skipped_case_info = []self.failures_case_info = []self.errors_case_info = []self.all_case_counter = 0self.suite = suiteself.status = ''self.result_list = []self.case_log = ''self.default_report_name = '自动化测试报告'self.FIELDS = Noneself.sys_stdout = Noneself.sys_stderr = Noneself.outputBuffer = None@propertydef success_counter(self) -> int:""" set success counter """return self.success_count@success_counter.setterdef success_counter(self, value) -> None:"""success_counter函数的setter方法, 用于改变成功的case数量:param value: 当前传递进来的成功次数的int数值:return:"""self.success_count = valuedef startTest(self, test) -> None:"""当测试用例测试即将运行时调用:return:"""unittest.TestResult.startTest(self, test)self.outputBuffer = StringIO()stdout_redirector.fp = self.outputBufferstderr_redirector.fp = self.outputBufferself.sys_stdout = sys.stdoutself.sys_stdout = sys.stderrsys.stdout = stdout_redirectorsys.stderr = stderr_redirectorself.start_time = time.time()def stopTest(self, test) -> None:"""当测试用力执行完成后进行调用:return:"""self.end_time = '{0:.3} s'.format((time.time() - self.start_time))self.result_list.append(self.get_all_result_info_tuple(test))self.complete_output()def complete_output(self):"""Disconnect output redirection and return buffer.Safe to call multiple times."""if self.sys_stdout:sys.stdout = self.sys_stdoutsys.stderr = self.sys_stdoutself.sys_stdout = Noneself.sys_stdout = Nonereturn self.outputBuffer.getvalue()def stopTestRun(self, title=None) -> dict:"""所有测试执行完成后, 执行该方法:param title::return:"""FIELDS['testPass'] = self.success_counterfor item in self.result_list:item = json.loads(str(MakeResultJson(item)))FIELDS.get('testResult').append(item)FIELDS['testAll'] = len(self.result_list)FIELDS['testName'] = title if title else self.default_report_nameFIELDS['testFail'] = self.failure_countFIELDS['beginTime'] = self.begin_timeend_time = int(time.time())start_time = int(time.mktime(time.strptime(self.begin_time, '%Y-%m-%d %H:%M:%S')))FIELDS['totalTime'] = str(end_time - start_time) + 's'FIELDS['testError'] = self.error_countFIELDS['testSkip'] = self.skippedself.FIELDS = FIELDSreturn FIELDSdef get_all_result_info_tuple(self, test) -> tuple:"""接受test 相关信息, 并拼接成一个完成的tuple结构返回:param test::return:"""return tuple([*self.get_testcase_property(test), self.end_time, self.status, self.case_log])@staticmethoddef error_or_failure_text(err) -> str:"""获取sys.exc_info()的参数并返回字符串类型的数据, 去掉t6 error:param err::return:"""return traceback.format_exception(*err)def addSuccess(self, test) -> None:"""pass:param test::return:"""logs = []output = self.complete_output()logs.append(output)if self.verbosity > 1:sys.stderr.write('ok ')sys.stderr.write(str(test))sys.stderr.write('\n')else:sys.stderr.write('.')self.success_counter += 1self.status = '成功'self.case_log = output.split('\n')self._mirrorOutput = True # print(class_name, method_name, method_doc)def addError(self, test, err):"""add Some Error Result and infos:param test::param err::return:"""logs = []output = self.complete_output()logs.append(output)logs.extend(self.error_or_failure_text(err))self.failure_count += 1self.add_test_type('失败', logs)if self.verbosity > 1:sys.stderr.write('F ')sys.stderr.write(str(test))sys.stderr.write('\n')else:sys.stderr.write('F')self._mirrorOutput = Truedef addFailure(self, test, err):"""add Some Failures Result and infos:param test::param err::return:"""logs = []output = self.complete_output()logs.append(output)logs.extend(self.error_or_failure_text(err))self.failure_count += 1self.add_test_type('失败', logs)if self.verbosity > 1:sys.stderr.write('F ')sys.stderr.write(str(test))sys.stderr.write('\n')else:sys.stderr.write('F')self._mirrorOutput = Truedef addSkip(self, test, reason) -> None:"""获取全部的跳过的case信息:param test::param reason::return: None"""logs = [reason]self.complete_output()self.skipped += 1self.add_test_type('跳过', logs)if self.verbosity > 1:sys.stderr.write('S ')sys.stderr.write(str(test))sys.stderr.write('\n')else:sys.stderr.write('S')self._mirrorOutput = Truedef add_test_type(self, status: str, case_log: list) -> None:"""abstruct add test type and return tuple:param status::param case_log::return:"""self.status = statusself.case_log = case_log@staticmethoddef get_testcase_property(test) -> tuple:"""接受一个test, 并返回一个test的class_name, method_name, method_doc属性:param test::return: (class_name, method_name, method_doc) -> tuple"""class_name = test.__class__.__qualname__method_name = test.__dict__['_testMethodName']method_doc = test.__dict__['_testMethodDoc']return class_name, method_name, method_docclass BeautifulReport(ReportTestResult, PATH):img_path = 'img/' if platform.system() != 'Windows' else 'img\\'def __init__(self, suites):super(BeautifulReport, self).__init__(suites)self.suites = suitesself.log_path = Noneself.title = '自动化测试报告'self.filename = 'report.html'def report(self, description, filename: str = None, log_path='.'):"""生成测试报告,并放在当前运行路径下:param log_path: 生成report的文件存储路径:param filename: 生成文件的filename:param description: 生成文件的注释:return:"""if filename:self.filename = filename if filename.endswith('.html') else filename + '.html'if description:self.title = descriptionself.log_path = os.path.abspath(log_path)self.suites.run(result=self)self.stopTestRun(self.title)self.output_report()text = '\n测试已全部完成, 可前往{}查询测试报告'.format(self.log_path)print(text)def output_report(self):"""生成测试报告到指定路径下:return:"""template_path = self.config_tmp_path# template_path = "D:\\PythonUnittest\\Template\\template"override_path = os.path.abspath(self.log_path) if \os.path.abspath(self.log_path).endswith('/') else \os.path.abspath(self.log_path) + '/'with open(template_path, 'rb') as file:body = file.readlines()with open(override_path + self.filename, 'wb') as write_file:for item in body:if item.strip().startswith(b'var resultData'):head = ' var resultData = 'item = item.decode().split(head)item[1] = head + json.dumps(self.FIELDS, ensure_ascii=False, indent=4)item = ''.join(item).encode()item = bytes(item) + b';\n'write_file.write(item)@staticmethoddef img2base(img_path: str, file_name: str) -> str:"""接受传递进函数的filename 并找到文件转换为base64格式:param img_path: 通过文件名及默认路径找到的img绝对路径:param file_name: 用户在装饰器中传递进来的问价匿名:return:"""pattern = '/' if platform != 'Windows' else '\\'with open(img_path + pattern + file_name, 'rb') as file:data = file.read()return base64.b64encode(data).decode()def add_test_img(*pargs):"""接受若干个图片元素, 并展示在测试报告中:param pargs::return:"""def _wrap(func):@wraps(func)def __wrap(*args, **kwargs):img_path = os.path.abspath('{}'.format(BeautifulReport.img_path))try:result = func(*args, **kwargs)except Exception:if 'save_img' in dir(args[0]):save_img = getattr(args[0], 'save_img')save_img(func.__name__)data = BeautifulReport.img2base(img_path, pargs[0] + '.png')print(HTML_IMG_TEMPLATE.format(data, data))sys.exit(0)print('<br></br>')if len(pargs) > 1:for parg in pargs:print(parg + ':')data = BeautifulReport.img2base(img_path, parg + '.png')print(HTML_IMG_TEMPLATE.format(data, data))return resultif not os.path.exists(img_path + pargs[0] + '.png'):return resultdata = BeautifulReport.img2base(img_path, pargs[0] + '.png')print(HTML_IMG_TEMPLATE.format(data, data))return resultreturn __wrapreturn _wrap
自带HTML模板template
template文件是和BeautifulReport.py一起使用的,他将unittest的测试结果按照template的样式转换成HTML格式的报告
调用BeautifulReport
import unittest
from Run.BeautifulReport import BeautifulReportif __name__ == '__main__':test_suite = unittest.defaultTestLoader.discover('TestScripts', pattern='test*.py')result = BeautifulReport(test_suite)result.report(filename='测试报告', description='测试报告', log_path='D:\\Programs\\Python\\PythonUnittest\\Reports')
测试报告
html-testRunner测试报告
安装
pip install html-testRunner
测试代码
# encoding = utf-8
'''
@Time : 2018/8/14 20:41
@Author : davieyang
@File : test_AjaxElement.py
@Software: PyCharm
'''
import tracebackfrom selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import unittest
import time
import HtmlTestRunnerclass TestAjaxElement(unittest.TestCase):def setUp(self):self.driver = webdriver.Chrome()def test_AjaxDivOptionByKeys(self):"""simulate keyboadr keydonw and enter and by circulating"""url = 'http://www.sogou.com'self.driver.get(url)searchBox = self.driver.find_element_by_id('query')searchBox.send_keys("selenium")time.sleep(3)for i in range(3):searchBox.send_keys(Keys.DOWN)time.sleep(3)searchBox.send_keys(Keys.ENTER)time.sleep(3)def test_AjaxDivOptionByWords(self):"""using xpath to fuzzy match"""url = 'http://www.sogou.com'self.driver.get(url)try:searchBox = self.driver.find_element_by_id('query')searchBox.send_keys(u'光荣之路')time.sleep(3)suggetion_option = self.driver.find_element_by_xpath("//ul/li[contains(.,'@#')]")suggetion_option.click()time.sleep(3)except NoSuchElementException as e:print(traceback.print_exc())def test_error(self):""" This test should be marked as error one. """raise ValueErrordef test_fail(self):""" This test should fail. """self.assertEqual(1, 2)@unittest.skip("This is a skipped test.")def test_AjaxDivOptionByIndex(self):"""using xpath to locate"""url = 'http://www.sogou.com'self.driver.get(url)try:searchBox = self.driver.find_element_by_id('query')searchBox.send_keys(u'光荣之路')time.sleep(3)suggetion_option = self.driver.find_element_by_xpath("//*[@id='vl']/div[1]/ul/li[3]")suggetion_option.click()time.sleep(3)except NoSuchElementException as e:print(traceback.print_exc())def tearDown(self):self.driver.quit()if __name__ == '__main__':unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='D:\\Programs\\Python\\PythonUnittest\\Reports\\'))
测试报告
HTMLTestRunner测试报告
测试代码
# encoding = utf-8
"""
__title__ = ''
__author__ = 'davieyang'
__mtime__ = '2018/4/21'
"""
from selenium import webdriver
import unittest
import time
import logging
import traceback
import ddt
from selenium.common.exceptions import NoSuchElementException# 初始化日志对象
logging.basicConfig(# 日志级别level=logging.INFO,# 时间、代码所在文件名、代码行号、日志级别名字、日志信息format='%(asctime)s %(filename)s[line: %(lineno)d] %(levelname)s %(message)s',# 打印日志的时间datefmt='%a, %d %b %Y %H:%M:%S',# 日志文件存放的目录及日志文件名filename='F:\\DataDriven\\TestResults\TestResults.TestResults',# 打开日志的方式filemode='w'
)@ddt.ddt
class DataDrivenTestByDDTHTR(unittest.TestCase):def setUp(self):self.driver = webdriver.Chrome(executable_path="F:\\automation\\webdriver\\chromedriver.exe")@ddt.file_data("F:\\DataDriven\\testData\\test_data_list.json")def test_dataDrivenHTRByFile(self, value):url = "http://www.baidu.com"self.driver.get(url)self.driver.maximize_window()print(value)# 将从.json文件中读取出的数据用“||”分割成测试数据和期望的数据testdata, execptdata = tuple(value.strip().split("||"))# 设置隐式等待时间self.driver.implicitly_wait(10)try:self.driver.find_element_by_id("kw").send_keys(testdata)self.driver.find_element_by_id("su").click()time.sleep(3)# 断言期望结果是否出现在页面中self.assertTrue(execptdata in self.driver.page_source)except NoSuchElementException as e:logging.error(u"查找的页面元素不存在,异常堆栈信息为:" + str(traceback.format_exc()))except AssertionError as e:logging.info(u"搜索 '%s',期望 '%s' ,失败" % (testdata, execptdata))except Exception as e:logging.error(u"未知错误,错误信息:" + str(traceback.format_exc()))else:logging.info(u"搜索 '%s',期望 '%s' ,通过" % (testdata, execptdata))def tearDown(self):self.driver.quit()if __name__ == '__main__':unittest.main()
执行测试
import unittest
from Run.HTMLTestRunner import HTMLTestRunner
from DataDrivenTest.DataDrivenTestByDDTHTR import DataDrivenTestByDDTHTR
import timesuit = unittest.TestSuite()
suit.addTest(unittest.makeSuite(DataDrivenTestByDDTHTR))
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
filename = "F:\\DataDriven\\TestResults\\Results-" + now + "result.html"
print(filename)
fp = open(filename, 'wb')
runner = HTMLTestRunner(stream=fp, title='Result', description='TestReport')
runner.run(suit)
print("test_DrivenByFile finished")
方法封装
# encoding = utf-8
from Run import HTMLTestRunner
import unittest
import timeclass StartTest(object):def __init__(self):print("generate test reports...")@staticmethoddef starttest():test_suite = unittest.defaultTestLoader.discover('TestScripts', pattern='test*.py')now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))filename = "D:\\Programs\\Python\\PythonUnittest\\Reports\\Results-" + now + "result.html"print(filename)fp = open("D:\\Programs\Python\\PythonUnittest\\Reports\\Results-" + now + "result.html", 'wb')runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title='Result', description='Test_Report')runner.run(test_suite)print('Test reports generate finished')if __name__ == '__main__':StartTest.starttest()
测试报告
Allure测试报告
环境配置
首先python环境中安装pytest和pytest_allure_adaptor模块,命令如下
pip install pytest
pip install pytest_allure_adaptor
执行测试
pytest -s -q --alluredir [path_to_report_dir]
- [path_to_report_dir]:为执行测试后生成测试结果的路径,是个自定义路径
- 测试完成后,在命令中指定的[path_to_report_dir]的自定义路径下,会生成类似如下xml文件,该文件中存下了执行测试后的测试结果,文件名类似为(自动生成):95060033-2085-40bb-b85a-64e43dc1d04b-testsuite.xml
<ns0:test-suite xmlns:ns0="urn:model.allure.qatools.yandex.ru" start="1544418468984" stop="1544418469065"><name>TestScripts</name><labels/><test-cases><test-case start="1544418468984" status="passed" stop="1544418468987"><name>test_asserts.test_add</name><attachments/><labels><label name="severity" value="normal"/><label name="thread" value="9944-MainThread"/><label name="host" value="DESKTOP-BE70S9D"/><label name="framework" value="pytest"/><label name="language" value="cpython3"/></labels><steps><step start="1544418468987" status="passed" stop="1544418468987"><name>test_add</name><title>test_add</title><attachments/><steps/></step></steps></test-case><test-case start="1544418468990" status="passed" stop="1544418468991"><name>test_asserts.test_add2</name><attachments/><labels><label name="severity" value="normal"/><label name="thread" value="9944-MainThread"/><label name="host" value="DESKTOP-BE70S9D"/><label name="framework" value="pytest"/><label name="language" value="cpython3"/></labels><steps><step start="1544418468991" status="passed" stop="1544418468991"><name>test_add2</name><title>test_add2</title><attachments/><steps/></step></steps></test-case><test-case start="1544418468992" status="passed" stop="1544418468993"><name>test_asserts.test_add3</name><attachments/><labels><label name="severity" value="normal"/><label name="thread" value="9944-MainThread"/><label name="host" value="DESKTOP-BE70S9D"/><label name="framework" value="pytest"/><label name="language" value="cpython3"/></labels><steps><step start="1544418468993" status="passed" stop="1544418468993"><name>test_add3</name><title>test_add3</title><attachments/><steps/></step></steps></test-case><test-case start="1544418468994" status="failed" stop="1544418469045"><name>test_asserts.test_add4</name><failure><message>AssertionError: assert 39 >= 50+ where 39 = add(17, 22)</message><stack-trace>def test_add4():
> assert add(17,22) >= 50
E assert 39 >= 50
E + where 39 = add(17, 22)test_asserts.py:34: AssertionError</stack-trace></failure><attachments/><labels><label name="severity" value="normal"/><label name="thread" value="9944-MainThread"/><label name="host" value="DESKTOP-BE70S9D"/><label name="framework" value="pytest"/><label name="language" value="cpython3"/></labels><steps/></test-case><test-case start="1544418469046" status="passed" stop="1544418469047"><name>test_asserts.test_in</name><attachments/><labels><label name="severity" value="normal"/><label name="thread" value="9944-MainThread"/><label name="host" value="DESKTOP-BE70S9D"/><label name="framework" value="pytest"/><label name="language" value="cpython3"/></labels><steps/></test-case><test-case start="1544418469048" status="passed" stop="1544418469049"><name>test_asserts.test_not_in</name><attachments/><labels><label name="severity" value="normal"/><label name="thread" value="9944-MainThread"/><label name="host" value="DESKTOP-BE70S9D"/><label name="framework" value="pytest"/><label name="language" value="cpython3"/></labels><steps/></test-case><test-case start="1544418469050" status="failed" stop="1544418469054"><name>test_asserts.test_true</name><failure><message>AssertionError: assert not True+ where True = is_prime(7)</message><stack-trace>def test_true():
> assert not is_prime(7)
E assert not True
E + where True = is_prime(7)test_asserts.py:65: AssertionError</stack-trace></failure><attachments/><labels><label name="severity" value="normal"/><label name="thread" value="9944-MainThread"/><label name="host" value="DESKTOP-BE70S9D"/><label name="framework" value="pytest"/><label name="language" value="cpython3"/></labels><steps/></test-case><test-case start="1544418469055" status="passed" stop="1544418469057"><name>test_fixture1.test_numbers_3_4</name><attachments/><labels><label name="severity" value="normal"/><label name="thread" value="9944-MainThread"/><label name="host" value="DESKTOP-BE70S9D"/><label name="framework" value="pytest"/><label name="language" value="cpython3"/></labels><steps/></test-case><test-case start="1544418469058" status="passed" stop="1544418469059"><name>test_fixture1.test_strings_a_3</name><attachments/><labels><label name="severity" value="normal"/><label name="thread" value="9944-MainThread"/><label name="host" value="DESKTOP-BE70S9D"/><label name="framework" value="pytest"/><label name="language" value="cpython3"/></labels><steps/></test-case><test-case start="1544418469060" status="passed" stop="1544418469062"><name>test_fixture2.TestUM.test_numbers_5_6</name><attachments/><labels><label name="severity" value="normal"/><label name="thread" value="9944-MainThread"/><label name="host" value="DESKTOP-BE70S9D"/><label name="framework" value="pytest"/><label name="language" value="cpython3"/></labels><steps/></test-case><test-case start="1544418469064" status="passed" stop="1544418469065"><name>test_fixture2.TestUM.test_strings_b_2</name><attachments/><labels><label name="severity" value="normal"/><label name="thread" value="9944-MainThread"/><label name="host" value="DESKTOP-BE70S9D"/><label name="framework" value="pytest"/><label name="language" value="cpython3"/></labels><steps/></test-case></test-cases>
</ns0:test-suite>
使用allure-commandline将xml制作成html报告
- 下载:allure2 allure1
- 解压到allure-commandline目录
- 进入bin目录,运行allure.bat
- 添加allure到环境变量PATH(\安装路径\allure-commandline\bin)
执行命令:
allure generate directory-with-results/ -o directory-with-report
- 【directory-with-results】: 为第三部中的路径,第三步中已经指定
- 【directory-with-report】: 自定义html生成到哪个路径下,如下图所示
查看报告
报告样式
生成报告方法封装
import os
from time import sleepclass GenerateAllureReport:def __init__(self, case_path, xml_path, report_path):self.case_path = case_pathself.xml_path = xml_pathself.report_path = report_pathdef run(self):generate_xml = "pytest " + self.case_path + " -s -q --alluredir " + self.xml_pathos.system(generate_xml)sleep(10)generate_report = "allure generate " + self.xml_path + " -o " + self.report_path + " --clean"os.system(generate_report)sleep(10)if __name__ == '__main__':GenerateAllureReport("D://PythonPrograms/Python_Pytest/TestScripts", "D://PythonPrograms/Python_Pytest//TestResult","D://PythonPrograms/Python_Pytest//TestResult//Report").run()
Jenkins集成Allure
安装插件
安装过程
安装成功
Global Tool Configuration
配置Allure Command Line
Add Post-Build Action
配置测试结果生成路径
配置测试报告生成路径
重新Build项目
生成测试结果
查看Allure报告