单元测试界的高富帅,Pytest框架,手把手教学,以后测试报告就这么做~

一、生成 HTML 报告

pytest 生成 HTML 的插件有很多,比如 pytest-html,pytest-testreport 等等,下面就给大家介绍如何使用 pytest-testreport 这个插件来生成 HTML 测试报告。

1.1、安装插件
pip install pytest-testreport

注意点:如果安装了 pytest-html 这个插件请先卸载,不然有可能会有冲突

1.2、插件的使用介绍

在使用 pytest 运行测试时,如果要使用 pytest-testreport 生成测试报告, 运行测试时加上参数 --report  指定报告文件名,即可实现。

其他运行参数:

--title :指定报告标题

--tester :指定报告中的测试者

--desc :指定报告中的项目描述

--template :指定报告模板样式(1 or 2)

  • 命令行执行:
pytest --report=musen.html --title=测试报告 --tester=木森 --desc=项目描述  --template=2

如果你想学习自动化测试,我这边给你推荐一套视频,这个视频可以说是B站播放全网第一的接口自动化测试教程,同时在线人数到达1000人,并且还有笔记可以领取及各路大神技术交流:798478386     

【已更新】B站讲的最详细的Python接口自动化测试实战教程全集(实战最新版)_哔哩哔哩_bilibili【已更新】B站讲的最详细的Python接口自动化测试实战教程全集(实战最新版)共计200条视频,包括:1.【接口自动化】目前软件测试的市场行情以及测试人员能力标准。、2.【接口自动化】全面熟练Requests库以及底层方法调用逻辑、3.【接口自动化】接口自动化实战及正则和JsonPath提取器的应用等,UP主更多精彩视频,请关注UP账号。https://www.bilibili.com/video/BV17p4y1B77x/?spm_id_from=333.337&vd_source=488d25e59e6c5b111f7a1a1a16ecbe9a 

  • 代码中使用 pytest.main 执行
import pytest

pytest.main(['--report=musen.html',
             '--title=柠檬班上课报告',
             '--tester=测试员',
             '--desc=报告描述信息',
             '--template=2'])
  • 生成的报告样式如下

样式一

图片

样式二

图片

关于 HTML 格式的报告生成就给大家介绍到这里,接下来和大家讲讲怎么集成 allure 报告

二、集成 Allure 报告

如果要在 pytest 中集成 allure 报告,首先得下载 allure,并配置好环境

2.1、allure 环境配置
一、下载 allure:

地址:https://github.com/allure-framework/allure2/releases

下载之后并解压

二、环境变量配置

将 allure 解压之后的 allure 路径的 bin 目录路径放到环境变量当中

图片

三、安装 allure 的 pytest 插件

pip install allure-pytest 
2.2、生成 allure 报告

安装配置好 allure 环境之后,在使用 pytest 执行用例时,就可以通过 allure 插件的参数来指定生成 allure 来报告了。

运行参数:

--alluredir :指定 allure 报告保存的路径

  • 命令行运行
pytest --alluredir=reports

  • 代码中使用 pytest.main 执行

import pytest
pytest.main(['--alluredir=reports'])
2.3、启动 allure 服务

在命令终端输入如下命令,启动 allure 服务

# 命令:allure serve  生成的报告路径allure serve reports

执行上述命令,allure 服务启动之后会自动在打开浏览器,显示 allure 的服务页面

图片

2.4、allure 内置常用的方法
添加错误截图
  • allure.attach
 def error_save_screenshot(driver,file_path, desc):        # 对当前页web页面进行截图        driver.save_screenshot(file_path)        # 将截图保存到allure报告中        with open(file_path, "rb") as f:            file = f.read()            allure.attach(file, "失败截图", allure.attachment_type.PNG)

添加报告中的用例名称
  • allure.title
import allure
class TestLogin:
    @allure.title('登录用例')    def test_login(self):        pass

参数化的用例中动态设置用例名称
  • allure.dynamic.title
# 用例数据casedatas = [    {'title': '反向用例1','data':"xxx"},    {'title': '反向用例2','data':"xxx"},    {'title': '反向用例3','data':"xxx"}]
class TestLogin:    @pytest.mark.parametrize('item',casedatas )    def test_demo(self, item):      # 动态设置报告中的用例名称        allure.dynamic.title(item['title'])

添加报告中的功能描述
  • allure.story
@allure.story('登录功能')class TestLogin:
    @allure.title('登录用例')    def test_login(self):        pass
添加报告中套件名称
  • allure.suite​​​​​​​
@allure.suite('登录测试套件')class TestLogin:    @allure.title('登录用例')    def test_login(self):        pass
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值