pytese支持allure报告的使用,allure报告美观实用。
安装Allure报告
依赖环境:
java version “1.8.0_212”(依赖java环境需要安装jdk)
pytest == 6.1.0
allure-pytest == 2.8.18
Mac下安装allure环境
allure解压包下载
配置allure环境
#命令行输入:
open -t ~/.bash_profile #打开配置文件
export PATH=${PATH}:/Users/songpeilun/Downloads/allure-2.13.0/bin #输入allure bin目录下的全路径
source ~/.bash_profile #保存使命令生效
#查看是否安装成功
allure --version 或 allure
pip3 list
allure使用
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/7/21 16:13
# @Name : peilun
# @File : pytest_allure.py
# @Software: PyCharm
import pytest
import allure
import os
@pytest.fixture(scope='function')
def login():
print("登录")
yield
print("登录完成")
@allure.feature('加入购物车')
def test_1(login):
'''将苹果加入购物车'''
print("测试用例1")
@allure.feature('加入购物车')
def test_2():
'''将橘子加入购物车'''
print("测试用例2")
命令行执行:
pytest pytest_allure.py --alluredir ./reports // 当前目录下生成report报告目录文件
allure serve reports // 本地执行并打开报告
代码中运行 后面加上以下代码
if __name__ == '__main__':
os.system("rm -rf ./reports") # 运行之前删除上次的报告目录
pytest.main(["-s", "run.py",'--alluredir', './reports'])
#os.system('allure generate ../reports -o ./allure-report --clean-alluredir') #该步骤主要是生成本地的报告 然后手动打开报告(获取报告成功数量)
os.system("allure serve ./reports") # 启动本地服务,自动打开报告
自定义Allure报告样式
修改报告输出格式
# 将allure报告结果json化
allure.attach(con.host+data['Url'], '请求URL', allure.attachment_type.JSON)
allure.attach(json.dumps(dicher), '请求头-Header', allure.attachment_type.JSON)
allure.attach(json.dumps(diccase),'请求体-Body',allure.attachment_type.JSON)
修改报告格式排版乱的问题
需要在Python的安装目录修改
- python安装目录:E:\Python3.7\Lib\site-packages\allure_pytest
- 打开文件找到第95行,然后将内容清空
自定义修改logo和名称
需要在allure解压目录下安装
- allure解压目录:E:\allure-2.13.0\plugins\custom-logo-plugin\static
- 替换掉之前的logo文件
- 编辑styles.css文件
.side-nav__brand {
background: url('custom-logo.png') no-repeat left center !important;
margin-left: 10px;
height: 50px;
background-size: contain !important;
}
.side-nav__brand span{
display: none;
}
.side-nav__brand:after {
content: "管家2.0";
margin-left: 20px;
font-size: 20px;
height: 50px;
line-height: 50px;
}
- 最后在allure.yml文件添加 - custom-logo-plugin