1、pytest+allure生成测试报告
1.1、allure下载安装
https://github.com/allure-framework/allure2/releases
下载zip包
配置环境变量
vim ~/.bash_profile
或者
vim ~/.zshrc
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
|