首页 > 其他分享 >高颜值测试报告- XTestRunner

高颜值测试报告- XTestRunner

时间:2022-09-30 11:03:27浏览次数:46  
标签:__ 高颜值 测试报告 self import test XTestRunner unittest

 

基于unittest框架现代风格测试报告。

特点

  • 漂亮测试报告让你更愿意编写测试。
  • 支持单元Web UIAPI 各种类型的测试。
  • 支持Selenium运行失败/错误自动截图。
  • 支持失败重跑。
  • 支持标签黑、白名单。
  • 支持发邮件功能。
  • 支持多语言enzh-CN 等。
  • 支持HTML/XML不同格式的报告。

安装

  • pip安装
> pip install XTestRunner

使用文档

单元测试

XTestRunner基本用法,用于生成 HTML测试报告。

测试用例

# test_unit.py
import unittest
from XTestRunner import HTMLTestRunner


class TestDemo(unittest.TestCase):
    """测试用例说明"""
    
    def test_success(self):
        """执行成功"""
        self.assertEqual(2 + 3, 5)
    
    @unittest.skip("skip case")
    def test_skip(self):
        """跳过用例"""
        pass
    
    def test_fail(self):
        """失败用例"""
        self.assertEqual(5, 6)
    
    def test_error(self):
        """错误用例"""
        self.assertEqual(a, 6)

if __name__ == '__main__':
    suit = unittest.TestSuite()
    suit.addTests([
        TestDemo("test_success"),
        TestDemo("test_skip"),
        TestDemo("test_fail"),
        TestDemo("test_error")
    ])
    
    with(open('./result.html', 'wb')) as fp:
        runner = HTMLTestRunner(
            stream=fp,
            title='<project name>test report',
            description='describe: ... ',
            language='en',
        )
        runner.run(
            testlist=suit,
            rerun=2,
            save_last_run=False
        )

HTMLTestRunner类说明

  • stream: 指定报告的路径。
  • title: 报告的标题。
  • description: 报告的描述, 支持strlist两种类型。
  • language: 支持中文zh-CN, 默认en

run()方法说明

  • testlist: 运行的测试套件。
  • rerun: 重跑次数。
  • save_last_run: 是否保存最后一个重跑结果。

运行测试

> python test_unit.py

Selenium Web测试

针对Selenium Web自动化测试提供了失败/错误 自动截图功能。

注意

1.安装selenium

> pip install selenium

2.注意:驱动必须定义为 driver, 否则无法生成截图。

测试用例

import unittest
from XTestRunner import HTMLTestRunner
from selenium import webdriver
from selenium.webdriver.common.by import By


class SeTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.driver = webdriver.Chrome()
        cls.base_url = "https://cn.bing.com/"

    @classmethod
    def tearDownClass(cls) -> None:
        cls.driver.quit()

    def test_success(self):
        """测试bing搜索:XTestRunner """
        self.driver.get(self.base_url)
        search = self.driver.find_element(By.ID, "sb_form_q")
        search.send_keys("XTestRunner")
        search.submit()

    def test_error(self):
        """测试bing搜索,定位失败 """
        self.driver.get(self.base_url)
        self.driver.find_element(By.ID, "sb_form_qxxx").send_keys("python")

    def test_fail(self):
        """测试bing搜索,断言失败 """
        self.driver.get(self.base_url)
        self.driver.find_element(By.ID, "sb_form_q").send_keys("unittest")
        self.assertEqual(self.driver.title, "unittest")

    def test_screenshots(self):
        """测试截图"""
        self.driver.get(self.base_url)
        # 元素截图
        elem = self.driver.find_element(By.ID, "sb_form_q")
        self.images.append(elem.screenshot_as_base64)
        # 竖屏截图
        self.images.append(self.driver.get_screenshot_as_base64())
        # 最大化截图
        self.driver.maximize_window()
        self.images.append(self.driver.get_screenshot_as_base64())


if __name__ == '__main__':
    report = "./selenium_result.html"
    with(open(report, 'wb')) as fp:
         unittest.main(testRunner=HTMLTestRunner(
            stream=fp,
            title='Selenium自动化测试报告',
            description=['类型:selenium', '操作系统:Windows', '浏览器:Chrome', '执行人:虫师']
        ))

测试报告

一个用例支持多张截图,不同的截图自动轮播,而且优化之后,不管是页面元素截图,横向、纵向图片都可以很好的展示。

API 接口测试

XTestRunner 当然也支持HTTP接口测试了。

  • 安装requests
> pip install requests

测试用例

import requests
import unittest
from XTestRunner import HTMLTestRunner

class YouTest(unittest.TestCase):

    def test_get(self):
        """测试get接口 """
        r = requests.get("https://httpbin.org/get", params={"key":"value"})
        print(r.json())

    def test_post(self):
        """测试post接口 """
        r = requests.post("https://httpbin.org/post", data={"key":"value"})
        print(r.json())

    def test_put(self):
        """测试put接口 """
        r = requests.put("https://httpbin.org/put", data={"key":"value"})
        print(r.json())

    def test_delete(self):
        """测试delete接口 """
        r = requests.delete("https://httpbin.org/delete", data={"key":"value"})
        print(r.json())


if __name__ == '__main__':
    report = "./reports/api_result.html"
    with(open(report, 'wb')) as fp:
        unittest.main(testRunner=HTMLTestRunner(
            stream=fp,
            title='Seldom自动化测试报告',
            description=['类型:API', '地址:https://httpbin.org/', '执行人:虫师']
        ))

测试报告

通过print() 可以讲接口信息打印到报告中展示。

邮件功能

XTestRunner 支持邮件功能。

import unittest
from XTestRunner import HTMLTestRunner
from XTestRunner import SMTP
...

if __name__ == '__main__':
    report = "./reports/send_email_result.html"
    with(open(report, 'wb')) as fp:
        unittest.main(testRunner=HTMLTestRunner(
            stream=fp,
            title='测试发送邮件',
            description=['类型:测试发送邮件', '执行人:虫师']
        ))
    # 发邮件功能
    # 使用126邮箱发送时password应为授权码而非用户密码,须在邮箱客户端设置开启授权码
    # 使用gmail邮箱发送时password为用户密码,须在gmail客户端开启安全性较低的应用的访问权限
    smtp = SMTP(user="[email protected]", password="xxx", host="smtp.qq.com")
    smtp.sender(to="[email protected]", subject="XTestRunner测试邮件", attachments=report)

邮件展示

支持黑白名单

可以通过黑白名单选择要执行(或跳过)的用例。

  • 支持白黑名单
    • 白名单:whitelist=["base"] 只有使用@label("base")装饰的用例执行
    • 黑名单:blacklist=["slow"] 只有使用@label("slow")装饰的用例不被执行

测试用例

import unittest
from XTestRunner import label
from XTestRunner import HTMLTestRunner


class LabelTest(unittest.TestCase):

    @label("base")
    def test_label_base(self):
        self.assertEqual(1+1, 2)

    @label("slow")
    def test_label_slow(self):
        self.assertEqual(1, 2)

    def test_no_label(self):
        self.assertEqual(2+3, 5)


if __name__ == '__main__':
    report = './reports/label_result.html'
    with(open(report, 'wb')) as fp:
        unittest.main(testRunner=HTMLTestRunner(
            stream=fp,
            title='<project name>test report',
            description='describe: ... ',
            whitelist=["base"],  # 设置白名单
            # blacklist=["slow"],  # 设置黑名单
        ))

注意:

白名单和黑名单不要同时用,以免产生冲突。

XML格式报告

虽然,HTML报告的颜值很高,但有时需要提取测试数据,比如保存到数据库,这个时候从HTML报告中提取数据是非常麻烦的,所以,XTestRunner 支持XML格式的报告。

import unittest
from XTestRunner import XMLTestRunner

#...

if __name__ == '__main__':
    # 定义报告
    report = "./xml_result.xml"
    with(open(report, 'wb')) as fp:
        unittest.main(testRunner=XMLTestRunner(output=fp))

报告展示

标签:__,高颜值,测试报告,self,import,test,XTestRunner,unittest
From: https://www.cnblogs.com/mashuqi/p/16744165.html

相关文章

  • UI测试报告jenkens整合
    (一)在本地整合出报告1.在cmd分别安装pytest和allure-pytestpip3installpytestpipinstallallure-pytest需要allure结合pytest才可以生成很漂亮的测试报告2.进入需要......
  • WEB自动化-09-Cypress 测试报告
    9测试报告  一份好的测试报告,可以很直观的看出整个测试过程的各种数据。而Cypress的测试报告是基于Mocha,因此任何支持Mocha的测试报告都可以应用于Cypress。但实际上......
  • allure介绍——生成完美的测试报告
    一、allure简介Allure是输出网页测试报告的一种框架1、该框架是基于Java写的,所以安装该框架需要先安装JDK;2、下载allure命令行工具,路径:https://github.com/allure-frame......
  • cypress无头模式运行,生成测试报告
    1.内置测试报告 npxcypressrun--reporter=spec2.指定运行用例  npxcypressrun--reporter=spec--speccypress\integration\cypress-study\web\page\should_de......
  • pytest+allure 之测试报告
     'allure'不是内部或外部命令,也不是可运行的程序或批处理文件。如果运行生成allure报告时,报此错误,大多是没有配置allure环境变量;具体如何配置,见下面文章详情;  if......
  • 开学课前测试报告
    对于本次开学测试分数未达到12分我认为有以下几点原因:1.对eclipse不太熟悉,考试时出现一些错误,导致考试前30分钟都在调试eclipse.2.暑假里对Java的学习不够深入,停留在一些......
  • 高颜值,类似Fliqlo的翻页时钟-BdTab新标签页插件组件
    起因:很多用户在使用BdTab插件时,反馈说希望添加一个时钟的功能,而BdTab又是组件模块化的插件,于是在空余时间就用html+js+css写了一款高颜值的分页时钟源码如下:需要其......
  • Python 自动化测试(五): Pytest 结合 Allure 生成测试报告
    ⬇️点击“下方链接”,提升测试核心竞争力!>>更多技术文章分享和免费资料领取霍格沃兹出品《测试开发实战进阶》课程,资深测试架构师、开源项目作者亲授BAT大厂前沿......
  • Pytest框架 — 16、Pytest的测试报告(pytest-html插件和Allure框架)
    目录1、前言2、pytest-html生成测试报告(1)pytest-html插件安装(2)pytest-html的使用(3)报告独立显示3、Allure框架生成测试报告(1)说明(2)环境准备步骤1:安装Allure框架步骤2:下载allu......
  • 如何修改Allure自动化测试报告名称?
    1.从GitHub上面获取allure代码https://github.com/allure-framework/allure22.下载gradle,用于打包jarhttps://gradle.org/install/#manually 3.配置gradle环境......