python+allure生成测试报告

发布时间 2023-08-21 09:03:44作者: Raul2018
# 一、allure安装
#
# 下载地址:Central Repository: io/qameta/allure/allure-commandline (apache.org)
#
# 下载需要安装的版本,这里我选择的是2.9的版本
#
# 图片
#
# 下载解压完成后,将bin文件夹所在的目录放入环境变量中,同时需要安装JDK。
#
# 完成后,命令行pip install allure-pytest安装allure插件,安装完成后可以验证一下,看看是否安装成功。PS:首次安装后需要重启下pycharm。
#
# 二、生成测试报告
#
# 生成测试报告有多种方式,结合自动化测试需求,将生成命令写在run.py文件中,执行测试用例后自动生成测试报告。

#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time : 2023/6/11 11:22
# @Author : 凌雪
# @File : run.py
# @Software: PyCharm
# 功能说明:全局运行文件


import pytest
import os
import sys

if __name__ == "__main__":
testpath = os.path.dirname(os.getcwd()) + '/testcase/'
# --clean-alluredir 如果temps目录下存在内容则先清除,即不保留历史记录
pytest.main(['-s', testpath, '--alluredir', '../temps', '--clean-alluredir'])
os.system('allure generate ../temps -o ../reports --clean')
# pytest.main(['-s', testpath+'homepage/test_custom.py'])
# 生成报告路径可以根据需要自己修改,这边我设置的是生成的json文件和html报告分别在两个文件夹下。temps文件夹中是json格式的,reports文件中的则是转化成html的,比较美观。如果是多次测试需要保留历史记录以备比对,则无需加--clean-alluredir参数。
#
# 生成测试报告后,在reports文件夹中找到index.html文件,右键选择用浏览器打开,即可查看到测试报告。
#
# 图片
#
# 注意,如果直接在文件夹中用浏览器打开这个文件,是渲染不出来的。
#
# 图片
#
# 三、测试报告优化
#
# 因为用例是通过yaml文件管理的,所以先封装了一个测试用例标题的方法,然后将在yaml用例中的名称显示到测试报告中。

#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time : 2023/6/11 17:53
# @Author : 凌雪
# @File : allure_step.py
# @Software: PyCharm
# 功能说明:生成测试报告中的用例封装


import allure
import json


def allure_title(title: str) -> None:
"""
allure中显示的用例标题
:param title:
:return:
"""
allure.dynamic.title(title)


def allure_step(step_title: str, content: str) -> None:
"""

:param step_title: 步骤及附加名称
:param content: 附件内容
:return:
"""
with allure.step(step_title):
allure.attach(json.dumps(content, ensure_ascii=False, indent=4), step_title, allure.attachment_type.TEXT)
# 图片
#
# 在py文件中,还可以添加模块名称、用例名称、描述等。

#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time : 2023/6/11 11:47
# @Author : 凌雪
# @File : test_get_headinfo.py
# @Software: PyCharm
# 功能说明:首页涉及到的接口测试用例,这些接口无需登录


import pytest
from common.send_request import Send_Request
from common.yaml_util import *
from common.parameterize_util import ddt, read_testcase
import allure


@allure.feature("首页接口测试")
class TestHomepage:

@allure.story("不需要登录的接口")
@pytest.mark.parametrize('testdata', read_testcase('homepage\\get_headinfo.yaml'))
def test_getheadinfo(self, testdata):
res = Send_Request("trainurl").standard_yaml(testdata)
# 图片