『德不孤』Pytest框架 — 9、Pytest测试报告
1、pytest-html插件
Pytest可以通过命令行方式,生成xml/html
格式的测试报告,并存储于用户指定路径。
需要用到pytest-html
插件。
安装方式:执行命令pip install pytest-html
。
(1)插件使用方式:
命令格式:--html=用户路径/report.html
运行方式:
main()
函数方式:
pytest.main(['--html=./report/report_01.html'])
(不好使,可能配置了pytest.ini
文件)- 命令行方式:
在report
目录中生成report.html
测试报告。
pytest ./pytest_demo/test_pytest_01.py --html=./report/report.html
- 使用
pytest.ini
文件方式:
在addopts
属性后追加--html
参数配置,在report
目录中生成report.html
测试报告。
addopts = -s --html=../report/report.html
(2)执行结果:
在指定目录中会生成assets
文件夹(css
文件)和report.html
文件。
如下图所示:
提示:若要生成
xml
文件,可将--html=./report.html
改成--junitxml= report/report.xml
2、Allure测试报告
(1)Allure框架说明
Allure
生成的测试报告与上面pytest-html
插件生成的测试报告对比,简直完美!
Allure
是一个Report
框架,是一种灵活的轻量级,支持多语言的测试报告工具,它不仅能够以简洁的WEB报告形式显示已测试的内容,并带有失败用例截图、测试步骤和测试说明信息,也可以集成到Jenkins
上展示高大上的报告界面。
而且允许参与开发过程的每个人从测试的日常执行中提取最大限度的有用信息。
Allure
框架支持的语言包括:
Java
Python
JavaScript
Ruby
Groovy
PHP
.Net
Scala
Allure帮助文档:
(2)Allure框架的使用
步骤1:下载Allure
框架,并配置到环境变量中。
Allure
框架下载地址:https://github.com/allure-framework/allure2/releases
点击下图位置,进行下载。
然后解压Allure
框架文件,放到自己指定的目录中。
把Allure
框架的bin
目录配置到Path
环境变量中。
步骤2:验证Allure
框架是否安装成功。
使用命令:allure --version
需要在CMD
命令行和PyCharm的Terminal
中,都需要验证一下。
因为CMD
可以验证通过,但是PyCharm中验证失败,如下:
J:\PyCharmWorkSpace\Pytest_d>allure --version
'allure' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
解决方式:需要重启PyCharm。
步骤3:下载allure-pytest
库(插件)。
执行安装命令:pip install allure-pytest
步骤4:设置生成的Json
格式临时报告的存放位置。
配置pytest.ini
文件,在pytest.ini
全局配置文件中的addopts
属性中添加:
--alluredir ../report/temp_jsonreport
例如:addopts = -vs --alluredir ../report/temp_jsonreport
然后我们执行测试用例就可以了,当然--alluredir
参数也可以不配置在pytest.ini
文件,比如在执行测试的命令行或者mian()
函数中填写都可以。(主要是生成Json
格式的测试报告,是多个Json
文件)
提示:
- 命令行参数:
pytest --alluredir report
,是在执行命令目录生成report
文件夹,文件夹下包含xml
文件。- 将
pytest.ini
文件中的生成报告的命令替换成--alluredir report
,在命令行中运行pytest
即可生成报告格式为Json
格式,保存在项目文件的report
文件夹中。
步骤5:生成Allure
测试报告。
原理是:使用第一步下载的Allure
框架把Json
格式的测试报告,转换成精美的HTML测试报告。
将上面/report/temp_jsonreport
文件夹中的Json
格式的测试报告转化为HTML格式的测试报告。
执行命令:allure generate ./report/temp_jsonreport -o ./report/html --clean
注意:以执行命令的目录为相对路径。
说明:
allure generate
: 固定命令。./report/temp_jsonreport
:生成的Json
格式的临时报告的路径。-o
:输出output
。./report/html
:生成的Allure
报告的路径。--clean
:清空./report/html
路径中原来的Allure
测试报告。
提示:main()
函数中执行如上命令。
if __name__ == '__main__':
pytest.main()
os.system("allure generate ./report/temp_jsonreport -o ./report/html --clean")
# 或者直接用main函数调用,哪种方式都可以。
# (直接执行测试文件, 而不用pytest的方式执行,就可以执行)
pytest.main(["testCase_demo1.py","-sv","--alluredir","../report/temp_jsonreport"])
os.system("allure generate ./report/temp_jsonreport -o ./report/html --clean")
说明:找不到路径的话,可以在Python Console
窗口调试。
最后,生成的Allure
测试报告如下图:
提示:
Allure
测试报告支持自定义修改。
『德不孤』Pytest框架 — 9、Pytest测试报告的更多相关文章
- 『德不孤』Pytest框架 — 1、Pytest测试框架介绍
目录 1.什么是单元测试框架 2.单元测试框架主要做什么 3.单元测试框架和自动化测试框架有什么关系 4.Pytest测试框架说明 5.Pytest框架和Unittest框架区别 (1)Unittes ...
- 『德不孤』Pytest框架 — 2、Pytest的基本使用
目录 1.Pytest安装 2.Pytest常用插件 3.Pytest运行的第一个例子 4.Pytest框架的运行方式 5.在PyCharm中以Pytest的方式运行测试用例 1.Pytest安装 C ...
- 『德不孤』Pytest框架 — 3、Pytest的基础说明
目录 1.Pytest参数介绍 2.Pytest框架用例命名规则 3.Pytest Exit Code说明 4.pytest.ini全局配置文件 5.Pytest执行测试用例的顺序 1.Pytest参 ...
- 『德不孤』Pytest框架 — 10、setUp()和tearDown()函数
目录 1.setUp()和tearDown()函数介绍 2.setUp()和tearDown()函数作用 3.setUp()和tearDown()函数说明 4.示例 (1)方法级 (2)类级 (3)函 ...
- 『德不孤』Pytest框架 — 11、Pytest中Fixture装饰器(一)
目录 1.Fixture装饰器的用途 2.Fixture参数说明 3.Fixture装饰器简单应用 4.yield执行后置函数 1.Fixture装饰器的用途 做测试前后的初始化设置,如测试数据准备, ...
- 『德不孤』Pytest框架 — 14、Pytest中的conftest.py文件
目录 1.conftest.py文件介绍 2.conftest.py的注意事项 3.conftest.py的使用 4.不同位置conftest.py文件的优先级 5.conftest.py中Fixtu ...
- 『德不孤』Pytest框架 — 15、Pytest参数化
目录 1.Pytest参数化说明 2.Pytest参数化方式 3.parametrize装饰器参数说明 4.Pytest参数化(单个参数) 5.Pytest参数化(多个参数) 6.ids参数说明 1. ...
- 『德不孤』Pytest框架 — 6、Mark分组执行测试用例
目录 1.Pytest中的Mark介绍 2.Mark的使用 3.Mark的注册和使用 4.使用Mark完成失败重试 5.扩展 1.Pytest中的Mark介绍 Mark主要用于在测试用例/测试类中给用 ...
- 『德不孤』Pytest框架 — 12、Pytest中Fixture装饰器(二)
目录 5.addfinalizer关键字 6.带返回值的Fixture 7.Fixture实现参数化 (1)params参数的使用 (2)进阶使用 8.@pytest.mark.usefixtures ...
随机推荐
- curl: (6) Could not resolve host: mirrors.163.com; Unknown error 服务器上解析不了域名,换成ip可以
原因是DNS域名解析问题: 添加nameserver即可解决 echo nameserver 8.8.8.8 > /etc/resolv.conf 解释一下DNS服务 DNS(Domain Na ...
- 微信h5下拉隐藏网页,还有取消页面滑动
需求: 网页下拉太丑了,如下 度娘了一下, 发现一篇相关文档 基本解决了问题 https://juejin.cn/post/6844903940190896135#heading-2 加入如下代码即可 ...
- spring 事务的传播级别和隔离级别
1.事务的传播级别 1)@Transactional(propagation=Propagation.REQUIRED):默认的spring事务传播级别,使用该级别的特点是,如果上下文中已经存在事务, ...
- linux计划任务之at
at是单次的计划任务 1.首先安装at yum -y install at 2.开启atd服务 systemctl start atd systemctl enabled atd 3.常用命令 -m ...
- Java线程--Semaphore使用
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11872132.html Java线程--Semaphore使用 Semaphore是信号量, ...
- C++中的常见错误
1.变量定义位置错误 1 int sum = 0; 2 3 do 4 { 5 int i = 1; 6 sum += i; 7 i++; 8 }while(i <= 100);//错误:i没有定 ...
- Xcode 插件推荐
1. Alcatraz(建议安装,以下插件都可以在Alcatraz下载安装) 使用Alcatraz来下载管理Xcode插件, 2.下载安装注释插件VVDocumenter-Xcode. 3.使用代码对 ...
- LNMP架构的源码编译以及yum安装
LNMP架构的源码编译以及yum安装 目录 LNMP架构的源码编译以及yum安装 一.LNMP架构的编译安装 1. 安装nginx服务 (1)关闭防火墙 (2)安装依赖包 (3)创建运行用户 (4)编 ...
- SpringBoot自动配置的魔法
Spring自动配置 从@SpringBootApplication注解说起 SpringBoot会根据类路径下的类自动配置,省去了编写繁琐的xml配置文件.原本基于xml配置bean的方式编程基于J ...
- Solution -「多校联训」自动机
\(\mathcal{Description}\) Link. 有一个状态集为 \(V\) 的自动机,状态接收 (, ) 和 _(空格) 三种字符,分别编号为 \(0,1,2\),状态 \(u ...