pytest中的allure报告

一、 使用步骤

第一步、安装allure: 使用命令行 pip install allure-pytest

    注意: allure的生效范围,是在当前目录还是别的目录; 有没有在venv环境下; 可以通过在当前命令行下输入 pip list 查看是否有对应的allure插件

第二步、 1. 生成报告路径: 使用命令pytest --alluredir=./report/tmp --clean-alluredir   # --alluredir表示指定测试报告数据的生成路径 

    命令执行完成后,会在当前目录下report目录中生成一个tmp目录文件, 如下图的tmp文件夹

      

 

 

     2. 生成报告数据, 生成allure-report文件夹,如上图显示

    注意:这里只是将tmp目录中的测试报告原始数据在指定的文件夹生成allure测试报告,并未在本机开启一个allure服务

    allure generate report\tmp -c -o report\allure-report # 最好使用该命令生成测试报告(与Jenkins上allure插件生成测试报告一致)

    3. 查看生成的报告,命令如下  

    (1)本地查看报告, 可以加参数指定. 必须步骤2、3一起结合使用

      allure open ./report/allure-report

    •   参数:-h, (--host):指定域名地址;
    •   参数:-p, (--port):指定端口号;

 

 

     (2)查看报告,对外在线展示, 可以在步骤1后直接使用。 可以通过-h  -p指定域名和端口号。 对外用户直接使用域名+端口访问报告

 

 

     注意: 每次更新了报告内容,都要重新生成报告,并且重新查看,否则不生效

 二、allure使用规则

第一、allure标记描述

注意: 1 . allure.step有两种使用方式来注明步骤:pytest系列——allure(二)之添加测试用例步骤(@allure.step())

      1) 在测试函数内,使用 with allure.step("test step"):    

      2)  在测试函数定义前,使用 @allure.step("test step")

    

 

  2.  allure.attach pytest系列——allure(三)之在测试报告中为测试用例添加附件(@allure.attach())_allure.attach()函数

       3. allure.title  pytest系列——allure(五)之在测试用例添加标题(@allure.title())

  4. allure.description pytest系列——allure(四)之在测试用例添加描述(@allure.description())_pytest 用例描述

 

 第二、 allure层级总览

 

 

 

 

 

 

第三、 测试用例详情

 

    ①parameters:如果用了 @pytest.mark.parametrize ,在右侧的parameters是可以看到传了什么参数以及对应的值。

    ②set up:调用fixture的前置操作。

    ③tear down:调用fixture的后置操作。

 

#!/usr/bin/env python
"""
 -*- coding: utf-8 -*-
 @file   : test_module_02.py
 @Date   : 2023/3/14
 @Author : Lin YK
 """
import pytest
import allure


# 测试函数
@allure.step("字符串相加:{0},{1}")  # 测试步骤,可通过format机制自动获取函数参数
def str_add(str1, str2):
    print("hello")
    if not isinstance(str1, str):
        return "%s is not a string" % str1
    if not isinstance(str2, str):
        return "%s is not a string" % str2
    return str1 + str2


# @allure.environment(host="172.6.12.27", test_vars=paras)
@allure.severity("critical")  # 优先级,包含blocker, critical, normal, minor, trivial 几个不同的等级
@allure.feature("测试模块_demo1")  # 功能块,feature功能分块时比story大,即同时存在feature和story时,feature为父节点
@allure.story("测试模块_demo2")  # 功能块,具有相同feature或story的用例将规整到相同模块下,执行时可用于筛选
@allure.issue("BUG号:123")  # 问题表识,关联标识已有的问题,可为一个url链接地址
@allure.testcase("用例名:测试字符串相等")  # 用例标识,关联标识用例,可为一个url链接地址
@pytest.mark.parametrize("para_one, para_two",  # 用例参数
                         [("hello world", "hello world"),  # 用例参数的参数化数据
                          (4, 4),
                          ("中文", "中文")],
                         ids=["test ASCII string",  # 对应用例参数化数据的用例名
                              "test digital string",
                              "test unicode string"])
def test_case_example(para_one, para_two):
    """用例描述:测试字符串相等
    :param para_one: 参数1
    :param para_two: 参数2
    """

    # 获取参数
    paras = vars()
    # 报告中的环境参数,可用于必要环境参数的说明,相同的参数以后者为准
    # allure.environment(host="172.6.12.27", test_vars=paras)
    # 关联的资料信息, 可在报告中记录保存必要的相关信息
    allure.attach("用例参数", "{0}".format(paras))
    # 调用测试函数
    res = str_add(para_one, para_two)
    # 对必要的测试中间结果数据做备份
    allure.attach("str_add返回结果", "{0}".format(res))
    # 测试步骤,对必要的测试过程加以说明
    with allure.step("测试步骤2,结果校验 {0} == {1}".format(res, para_one + para_two)):
        assert res == para_one + para_two, res


if __name__ == '__main__':
    # 执行,指定执行测试模块_demo1, 测试模块_demo2两个模块,同时指定执行的用例优先级为critical,blocker
    pytest.main(['--allure_stories=测试模块_demo1, 测试模块_demo2', '--allure_severities=critical, blocker'])

  

三、扩展

第一、 添加ENVIRONMENT, 用来显示此次测试用例执行的环境内容,可以自己手动配置,也可以通过代码实时获取代码运行时的内容。

文件中不能有中文,会乱码

#第一步执行测试【这里会删除原来的environment文件】
pytest test01.py -v -s --alluredir=./report/allure  --clean-alluredir 
 
#第二步复制环境变量文件【从项目根目录复制到测试报告原始数据目录,放到tmp下】
copy environment.properties report\tmp\
 
#第三步生成报告
allure generate report\tmp -o report\allure-report -c report\allure-report
 
#第四步本地查看报告
allure open report\allure-report

 

第二、添加CATEGORIES

。。。。

     

           

posted @ 2023-03-14 10:50  Aedline  阅读(697)  评论(0编辑  收藏  举报