Pytest(三、Allure使用)
1.Pytest配置文件
2.Pytest测试报告
3.Allure生成报告
4.pytest-allure代码
1.Pytest配置文件
配置文件的名称必须为: pytest.ini (是ini后缀,不是init后缀)
配置文件跟脚本同处一个目录下
# 配置pytest命令行运行参数
[pytest] # 这个需要填写
addopts = -s ... # 空格分隔,可添加多个命令行参数 -所有参数均为插件包的参数
# 配置测试搜索的路径
testpaths = ./scripts # 当前目录下的scrip文件夹 可自定义
# 配置测试搜索的文件名
python_files = test_*.py
# 当前目录下的scripts文件夹下,以test_开头,以.py结尾的所有文件 -可自定义
# 配置测试搜索的测试类名
python_classes = Test_*
# 当前目录下的scripts文件夹下,以test_开头,以.py结尾的所有文件中,以Test_开头的类
# 配置测试搜索的测试函数名
python_functions = test_*
# 当前目录下的scripts文件夹下,以test_开头,以.py结尾的所有文件中,以Test_开头的类内,以test_开头的⽅法 -可自定义
2.Pytest测试报告.
# 安装命令
pip install pytest-html
# 修改pytest.ini 配置文件,添加报告参数,即:addopts = -s --html=./report.html # 可自定义生成的路径
# 执行方法: 来到 pytest.ini 目录下, 输入:pytest
3.Allure生成报告
1.Allure是一款非常轻量级并且非常灵活的开源测试报告生成框架, 能够生成美观易读的报告,目前支持语言:Java, PHP, Ruby, Python, Scala, C#。它支持绝大多数测试框架, 例如TestNG、Pytest、JUint等。它简单易用,易于集成.
2. 安装Allure Pytest Adaptor
Allure Pytest Adaptor是Pytest的一个插件,通过它我们可以生成Allure所需要的用于生成测试报告的数据。安装pytest-allure-adaptor插件方法:
pip install pytest-allure-adaptor
3.使用方式:
在pytest.ini文件中添加: addopts = -s --alluredir ./report # 会在当前目录下的report文件夹下生成 一个 xxx.json的文件
4.需要使用工具 将json文件转成html文件
需要下载一个allure工具,可以参考这个教程 https://www.cnblogs.com/huny/p/13752406.html
5. 进入report上级目录执行命令:allure generate report/ -o report/html # 会将报告生成在 report/html下,名称为index.html
ps: 用谷歌浏览器打开报告 需要开启allure服务器,否则无法显示数据: allure serve report/allure_raw
ps: 如果报告中文乱码,可参考这个教程:https://blog.csdn.net/qq_36917144/article/details/108740806
4.pytest-allure代码
import os
import allure
import pytest
class TestAllure:
def setup(self):
print('---> setup')
def teardown(self):
print('teardown')
@allure.step("我是测试步骤001")
def test_a(self):
print('test_a')
# 添加描述信息
allure.attach("描述", "我是测试步骤001的描述~~")
assert 2 != 2 # 当条件不成立时,会报错
if __name__ == '__main__':
pytest.main(["-s", "test_01_fixture.py"])
os.system("allure generate report/ -o report/html --clean") # 自动生成报告
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。
文章标题:Pytest(三、Allure使用)
本文作者:伟生
发布时间:2021-01-16, 17:01:25
最后更新:2021-01-24, 19:57:35
原始链接:http://yoursite.com/2021/01/16/ceshi_14_Pytest(3)/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。