前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >测试报告-BeautifulReport报告

测试报告-BeautifulReport报告

作者头像
wangmcn
发布2022-07-25 13:48:04
7130
发布2022-07-25 13:48:04
举报
文章被收录于专栏:AllTests软件测试

BeautifulReport报告

这是一个基于unittest.TestReport模块实现的测试用例模板,可以把我们每次测试中的结果通过BeautifulReport整合成一个可视化的报表。

BeautifulReport报告下载地址:https://github.com/TesterlifeRaymond/BeautifulReport

下载完之后,把BeautifulReport整个包解压后放到Python安装目录里的/Lib/site-packages/目录下。

1、首先新建一个Python项目

例如:My_Report_BeautifulReport_Demo。

创建case包,用于存放测试用例(test_case1.py、test_case2.py)。

创建img文件夹,截图会存放到此文件夹里。

创建report文件夹,执行脚本指定测试报告生成在此文件夹里。

创建run_all.py为执行测试用例文件。

BeautifulReport.zip为BeautifulReport报告包。

2.1、test_case1.py(测试用例)

(1)定义截图方法:

代码语言:javascript
复制
def save_img(self, img_name):
    self.img_path = 'img'
    self.driver.get_screenshot_as_file('{}/{}.png'.format(os.path.abspath(self.img_path), img_name))

(2)@BeautifulReport.add_test_img,如果用例失败且已经截图,则将截图显示在测试报告里。

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
测试用例1
"""
from selenium import webdriver
import unittest
from BeautifulReport import BeautifulReport
import os

class Test1(unittest.TestCase):
    '''测试用例1测试'''
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

    @BeautifulReport.add_test_img('test_01')
    def test_01(self):
        '''失败用例'''
        self.driver.get('https://www.baidu.com')
        self.save_img('test_01')
        self.driver.find_element_by_id('kw1').send_keys('百度一下')
        self.assertTrue(True)

    @BeautifulReport.add_test_img('test_02')
    def test_02(self):
        '''失败用例'''
        self.driver.get('https://mail.126.com/')
        self.save_img('test_02')
        self.assertIn('163',self.driver.title)

    @BeautifulReport.add_test_img('test_03')
    def test_03(self):
        '''成功用例'''
        self.driver.get('https://cn.bing.com/')
        self.save_img('test_03')
        self.assertIn('Bing',self.driver.title)

    # 截图方法
    def save_img(self, img_name):
        self.img_path = 'img'
        self.driver.get_screenshot_as_file('{}/{}.png'.format(os.path.abspath(self.img_path), img_name))

if __name__ == '__main__':
    unittest.main(verbosity=2)

2.2、test_case2.py(测试用例)

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
测试用例2
"""
from selenium import webdriver
import unittest

class Test2(unittest.TestCase):
    '''测试用例2测试'''
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

    def test_01(self):
        '''成功用例'''
        self.driver.get('https://www.baidu.com')
        self.driver.find_element_by_id('kw').send_keys('百度一下')

    def test_02(self):
        '''成功用例'''
        self.driver.get('https://cn.bing.com/')

if __name__ == '__main__':
    unittest.main(verbosity=2)

2.3、run_all.py(执行用例)

引入BeautifulReport,设置报告文件名、描述、报告路径等。

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
执行用例
"""
import unittest
import os
# 将BeautifulReport.zip解压后放到Python安装目录里的/Lib/site-packages/文件夹下
from BeautifulReport import BeautifulReport

# 获取路径
cur_path = os.path.dirname(os.path.realpath(__file__))
# 测试用例路径
case_path = os.path.join(cur_path, 'case')
# 测试报告路径
report_path = os.path.join(cur_path, 'report')

if __name__ == "__main__":
    suite = unittest.defaultTestLoader.discover(case_path,'test*.py')
    result = BeautifulReport(suite)
    result.report(filename='HTMLReport', description='Demo示例', log_path='report')

3、执行run_all.py文件

运行结果:

在项目的img目录里存放截图。

在项目的report目录里自动生成测试报告。

双击打开测试报告,效果如下:

优化了:样式美化、报告汇总、饼图统计、测试结果筛选等。

失败用例截图显示在报告里。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-12-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AllTests软件测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云 BI
腾讯云 BI(Business Intelligence,BI)提供从数据源接入、数据建模到数据可视化分析全流程的BI能力,帮助经营者快速获取决策数据依据。系统采用敏捷自助式设计,使用者仅需通过简单拖拽即可完成原本复杂的报表开发过程,并支持报表的分享、推送等企业协作场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档