首页 > 编程语言 >Python-BeautifulReport的简单使用

Python-BeautifulReport的简单使用

时间:2023-02-16 15:22:06浏览次数:51  
标签:img Python self driver test 简单 BeautifulReport import

一、简介

  • BeautifulReport.report

  report ( filename -> 测试报告名称, 如果不指定默认文件名为report.html description -> 测试报告用例名称展示 log_path='.' -> log文件写入路径 )

  • BeautifulReport.add_test_img

  如果使用报告过程中需要把测试报告的截图放在报告中, 可以使用add_test_img方法

  • add_test_img ( *pargs )

  可以在测试用例上挂载一个装饰器

 ps:
  • 默认存放的图片路径是img, 需要在当前测试项目的启动路径下, 创建一个img文件夹
  • 传递给装饰器的图片,在运行测试前可以不存在, 运行测试之后生成即可.
  • 当文件在报告中展示后, 想要看到原图, 可以点击报告中的缩略图查看完整的截图

 

二、工具包下载

1、https://github.com/TesterlifeRaymond/BeautifulReport(出自此大神)

 

2、下载后把BeautifulReport整个包放到python的/Lib/site-packages/目录下

 

三、使用方式

 1、项目结构

  • test_case:存放测试用例
  • test_report:存放生成的测试报告
  • run.py:运行测试脚本
  • img:存放测试截图

 

2、测试脚本

测试脚本test_a_bd.py参考代码

# -*- coding:UTF-8 -*-
import unittest
from selenium import webdriver
from time import sleep

class Test_a(unittest.TestCase):
    """测试类A"""

    @classmethod
    def setUpClass(self):
        self.driver = webdriver.Chrome()

        self.driver.get("https://www.baidu.com/")

    def test_b_dubai(self):
        """用例1"""
        print("1", self.driver.title)
        sleep(2)
        self.driver.close()

测试脚本test_b_db.py参考代码

# -*- coding:UTF-8 -*-
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from BeautifulReport import BeautifulReport
import os
from time import sleep

class Test_b(unittest.TestCase):
    """测试类B"""

    img_path = 'img'

    def save_img(self, img_name):
        """
            传入一个img_name, 并存储到默认的文件路径下
        :param img_name:
        :return:
        """
        self.driver.get_screenshot_as_file('{}/{}.png'.format(os.path.abspath(self.img_path), img_name))

    @classmethod
    def setUpClass(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://www.baidu.com/")

    @BeautifulReport.add_test_img('搜索前', '搜索后')
    def test_a_baidu(self):
        """用例2"""
        self.save_img('搜索前')
        self.driver.find_element(By.ID, "kw").send_keys("博客园")
        self.driver.find_element(By.ID, "su").click()
        self.save_img('搜索后')
        print("2", self.driver.title)
        self.driver.close()

run.py参考脚本

# -*- coding:UTF-8 -*-
import unittest
from BeautifulReport import BeautifulReport
import os
from tomorrow import threads
import datetime

# 定义目录
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
TEST_DIR = os.path.join(BASE_DIR, "test_case")
REPORT_DIR = os.path.join(BASE_DIR, "test_report")

# kill 所有chromedriver进程
os.system("taskkill /f /im chromedriver.exe")

def add_case():
    '''加载所有的测试用例'''
    discover = unittest.defaultTestLoader.discover(
        TEST_DIR,
        pattern="test_*.py"
    )
    return discover

@threads(3)
def run(test_suit):

    # 定义测试报告名称
    now_time = str(datetime.datetime.now().strftime('%Y%m%d%H%M'))
    report_name = "report_" + now_time

    result = BeautifulReport(test_suit)
    result.report(filename=f'{report_name}.html', description='平台报告', log_path='test_report')

if __name__ == "__main__":
    # 用例集合
    cases = add_case()

    print(cases)
    for i in cases:
        print(i)
        run(i)

 

3、报告效果

 

标签:img,Python,self,driver,test,简单,BeautifulReport,import
From: https://www.cnblogs.com/TSmagic/p/17125523.html

相关文章

  • python的学习之路Day5
    2023.2.16Day5今日内容概要逻辑运算符成员运算符身份运算符流程控制(重点)if判断while循环今日内容详细逻辑运算符and与 ##可以用and连接多个条......
  • Python Boa-constructor的使用
    上篇介绍了Python的环境搭建,主要是开发环境的介绍,本篇主要介绍Python的GUI开发工具Boa-constructor的使用和我们的程序开发完毕后的打包问题,本篇还会介绍在Python中我们如......
  • 什么是python的设计模式
    一.什么是python的设计模式?软件工程中,设计模式是指软件设计问题的推荐方案。设计模式一般是描述如何组织代码和使用最佳实践来解决常见的设计问题。需谨记一点:设计模......
  • python代码打包exe文件(不带模块)
    一、安装第三方模块pipinstallpyinstaller二、在控制台输入对应命令pyinstaller-Dtas.py#tas.py为py文件名称,自行匹配更改成功后生成三个新文件.spec文件配置......
  • js逆向中常见加密/解密算法实现(js、python)
    常见加密、解密算法实现(JS、python)base64NodejsconstCryptoJS=require('crypto-js')//加密letpwd='密码'letb64_pwd=Buffer.from(pwd).toString('b......
  • python配置包
    配置python环境在python官网(https://www.python.org)下载python3.7版本   选择 download   进行安装  installnow选择addpython3.7topath......
  • jupyter notebook(python 3.7)网页版安装
    参考链接:https://blog.csdn.net/NickHan_cs/article/details/108204297 #1.进入python的Scripts目录下 >activate.bat    ------->#激活Scripts >cdd:\Ope......
  • 使用docker部署Python-Flask实现ChatGPT的使用
    首先说下背景:自从申请ChatGPT到现在也有个把月了,余额一直还很多,想想三月下旬就过期了,还是计划把里面的18美刀用掉,于是结合着之前做的Django简单cv一个"简易网站"。简......
  • 【Python】字母表的循环迭代
     字母循环迭代器defforward(letter,jump):"""字母表上的循环迭代器"""ifletter.islower():start_character=ord('a')......
  • python读取json 生成Excel表格
    importjsonimportxlwtbiaotou='[{"name_cn":"企业名称","name_en":"QYMC"},{"name_cn":"类别","name_en":"LB"},{"name_cn":"地市","name_en":"DS&q......