Pytest生成测试报告

本文详细介绍了如何在Python中使用pytest和allure生成详细的测试报告,包括下载并配置allure,安装allure-pytest插件,生成json格式报告,以及最终生成Allure报告的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、pytest+allure生成测试报告

1.1、allure下载安装

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

下载zip包

在这里插入图片描述
配置环境变量

vim ~/.bash_profile		# centOS
或者
vim ~/.zshrc 			# macOS

# allure
export PATH="/Users/xxx/Downloads/allure-2.14.0/bin:$PATH"

# 配置结束后保存文件
source ~/.bash_profile

验证安装是否成功:

% allure --version
2.14.0

注意:iTerm2可以验证,但pycharm验证失败,此时需要重启pycharm

1.2、安装allure-pytest插件
% pip install allure-pytest
Collecting allure-pytest
  Using cached allure_pytest-2.9.43-py3-none-any.whl (9.6 kB)
Requirement already satisfied: pytest>=4.5.0 in /Users/xxx/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from allure-pytest) (6.2.4)
Collecting allure-python-commons==2.9.43
  Downloading allure_python_commons-2.9.43-py3-none-any.whl (15 kB)
Requirement already satisfied: six>=1.9.0 in /Users/xxx/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from allure-pytest) (1.15.0)
Requirement already satisfied: pluggy>=0.4.0 in /Users/xxx/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from allure-python-commons==2.9.43->allure-pytest) (0.13.1)
Requirement already satisfied: attrs>=16.0.0 in /Users/xxx/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from allure-python-commons==2.9.43->allure-pytest) (21.2.0)
Requirement already satisfied: importlib-metadata>=0.12 in /Users/xxx/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from pluggy>=0.4.0->allure-python-commons==2.9.43->allure-pytest) (3.10.0)
Requirement already satisfied: typing-extensions>=3.6.4 in /Users/xxx/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from importlib-metadata>=0.12->pluggy>=0.4.0->allure-python-commons==2.9.43->allure-pytest) (3.10.0.0)
Requirement already satisfied: zipp>=0.5 in /Users/xxx/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from importlib-metadata>=0.12->pluggy>=0.4.0->allure-python-commons==2.9.43->allure-pytest) (3.5.0)
Requirement already satisfied: iniconfig in /Users/xxx/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from pytest>=4.5.0->allure-pytest) (1.1.1)
Requirement already satisfied: packaging in /Users/xxx/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from pytest>=4.5.0->allure-pytest) (21.0)
Requirement already satisfied: py>=1.8.2 in /Users/xxx/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from pytest>=4.5.0->allure-pytest) (1.10.0)
Requirement already satisfied: toml in /Users/xxx/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from pytest>=4.5.0->allure-pytest) (0.10.2)
Requirement already satisfied: pyparsing>=2.0.2 in /Users/xxx/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from packaging->pytest>=4.5.0->allure-pytest) (2.4.7)
Installing collected packages: allure-python-commons, allure-pytest
Successfully installed allure-pytest-2.9.43 allure-python-commons-2.9.43
1.3、生成json格式的测试报告

添加选项--alluredir ./temp生成json测试报告

pytest.ini

[pytest]
addopts = -vs --alluredir ./temp
testpaths = testcase/
python_files = test_*.py
python_classes = Test*
python_functions = test

执行命令行操作:

% pytest

生成json格式的测试报告
在这里插入图片描述

1.4、生成allure报告

执行命令:

allure generate ./temp -o ./report --clean

# 说明
allure generate 	命令,固定的
./temp				临时的json格式报告的路径
-o 					输出output
./report			生成的allure报告的路径
--clean				清空./report路径原来的报告

测试实例:

配置好pytest.ini

[pytest]
addopts = -vs --alluredir ./temp
testpaths = testcase/
python_files = test_*.py
python_classes = Test*
python_functions = test

在项目根目录下创建一个主py文件:

all.py

import os
import pytest

if __name__ == '__main__':
    pytest.main()
    os.system('allure generate ./temp -o ./report --clean')

运行all.py文件,打印日志:

============================= test session starts ==============================
collecting ... collected 2 items

testcase/product/test_product.py::TestProduct::test_product 
全局的前置

product的前置
我是一个product~
PASSED
product的后置

全局的后置

testcase/user/test_user.py::TestUser::test_user 
user的前置
我是一个user~
PASSED
user的后置

============================== 2 passed in 0.03s ===============================
Report successfully generated to ./report

Process finished with exit code 0

打开report文件夹

在这里插入图片描述
打开index.html

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值