Pytest+Allure生成HTML图形化测试报告

本文详细介绍了如何使用Allure生成自动化测试报告的过程,包括环境配置、依赖库安装、生成xml报告及转换为html报告的方法。此外,还深入讲解了如何通过Feature、Story、Severity等标签定制报告,提升测试报告的可读性和实用性。

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

一、环境配置

1、安装Python依赖库

pip install pytest
pip install pytest-allure-adaptor
# pytest-allure-adaptor库可以替换为pytest-allure-adaptor2

2、安装allure-commandline工具

下载的allure-commandline为ZIP压缩包,需要将解压文件中bin目录设置为环境变量。

注意:不要将allure-commandline解压在Program Files (x86)目录中,否则运行会报错

allure-commandline百度云盘下载:

链接:https://pan.baidu.com/s/1o2ptbdOKq2qOQYkQ0z2pKg
提取码:wevw

官网下载地址:

https://www.mvnjar.com/ru.yandex.qatools.allure/allure-commandline/1.5.3/detail.html

 

3、验证安装是否成功与问题解决方法

在命令行输入pytest和allure,若提示如下错误信息,是因为我们没有将python目录下的Scripts目录配置到环境变量中

将python目录下的Scripts目录配置到环境变量后,再次输入pytest,返回如下信息,则表示pytest安装成功。

 

但是当我们在命令行输入allure,却返回错误信息“此时不应有 \Python36-32\allure-commandline\bin\..”,产生问题的原因是我们把allure-commandline解压到“Program Files (x86)”中,需要更换安装路径为“Program Files”。

当更换allure-commandline目录后,再次输入allure,可能还会返回错误信息“此时不应有\java\jdk1.8.0_17\lib”,此时可能需要安装64位的JDK(我没试过更换目录,直接重新安装的64位JDK,所以不清楚直接更换目录是否有用)

当输入allure返回如下界面,则表示allure安装成功

 

二、生成html报告命令

1、使用pytest生成xml报告

pytest --alluredir [xml_report_path]

# [xml_report_path]根据自己需要定义文件夹,作者定义为:/report/xml

用例执行完成之后会在[xml_report_path]目录下生成了一堆xml的report文件,当然这不是我们最终想要的美观报告。

运行结果如下:

2、使用 CommandTool 生成我们需要的美观报告。

allure generate [xml_report_path] -o [html_report_path]
# [xml_report_path]为第一步生成xml报告的路径
# [html_report_path]为输出html报告的路径,作者定义为:/report/html

打开 index.html,测试报告就会呈现在你面前

注⚠️:直接用chrome浏览器打开报告,报告可能会是空白页面。

解决办法:
1、在pycharm中右击index.html选择打开方式Open in Browser就可以了。
2、使用Firefox直接打开index.html。

打开后界面如下

三、定制报告

  • Feature: 功能块,feature功能分块时比story大,即同时存在feature和story时,feature为父节点
  • Story: 功能块,具有相同feature或story的用例将规整到相同模块下,执行时可用于筛选
  • Severity: 标注测试用例的重要级别,包含blocker, critical, normal, minor, trivial 几个不同的等级
  • Step: 标注测试用例步骤
  • Issue:问题标识,关联标识已有BUG的问题,可为一个url链接地址
  • TestCase: # 用例标识,关联标识用例,可为一个url链接地址
  • attach: 标注一些附加内容到测试报告中
  • Environment: 标注环境Environment字段

1、Features定制详解

  • Feature: 功能块,feature功能分块时比story大,即同时存在feature和story时,feature为父节点
# coding=utf-8

import os
import allure

fileName = os.path.split(os.path.realpath(__file__))[1]
@allure.feature(f"{fileName}文件feature标注的第一个用例")
def test_case1():
    a = 1
    b = 1
    c = a + b
    assert c == 2

@allure.feature(f"{fileName}文件feature标注的第二个用例")
def test_case2():
    a = "aaaaaaaaa"
    assert len(a) == 10

2、Story定制详解

  • Story: 功能块,具有相同feature或story的用例将规整到相同模块下,执行时可用于筛选
import os
import allure

fileName = os.path.split(os.path.realpath(__file__))[1]
@allure.feature(f"{fileName}文件feature标注的用例")
@allure.story(f"{fileName}文件story标注的第一个用例")
def test_case1():
    a = 1
    b = 1
    c = a + b
    assert c == 2

@allure.feature(f"{fileName}文件feature标注的用例")
@allure.story(f"{fileName}文件story标注的第二个用例")
def test_case2():
    a = "aaaaaaaaa"
    assert len(a) == 10

3、用例标题和用例描述定制详解

def的方法名称为用例名称,3引号注释为用例描述

# coding=utf-8

import os
import allure

fileName = os.path.split(os.path.realpath(__file__))[1]
@allure.feature(f"{fileName}")
def test_caseName1():
    # test_caseName1为用例title
    """
    用例描述:这是test_caseName1的用例描述
    """
    # 3引号注释为用例描述
    a = 1
    b = 1
    c = a + b
    assert c == 2

@allure.feature(f"{fileName}")
def test_caseName2():
    a = "aaaaaaaaa"
    assert len(a) == 10

4 、Severity定制详解

  • Severity: 标注测试用例的重要级别,包含blocker, critical, normal, minor, trivial 几个不同的等级
Allure中对严重级别的定义:
1、 Blocker级别:中断缺陷(客户端程序无响应,无法执行下一步操作)
2、 Critical级别:临界缺陷( 功能点缺失)
3、 Normal级别:普通缺陷(数值计算错误)
4、 Minor级别:次要缺陷(界面错误与UI需求不符)
5、 Trivial级别:轻微缺陷(必输项无提示,或者提示不规范)
# coding=utf-8

import os
import allure

fileName = os.path.split(os.path.realpath(__file__))[1]
@allure.feature(f"{fileName}")
@allure.story("正向用力")
@allure.severity('blocker')
def test_case1():
    a = "aaaaaaaaa"
    assert len(a) == 10

@allure.feature(f"{fileName}")
@allure.story("正向用力")
@allure.severity('critical')
def test_case2():
    a = "aaaaaaaaa"
    assert len(a) == 9

@allure.feature(f"{fileName}")
@allure.story("反向用力")
@allure.severity('normal')
def test_case3():
    a = "aaaaaaaaa"
    assert len(a) == 10

@allure.feature(f"{fileName}")
@allure.story("反向用力")
@allure.severity('minor')
def test_case4():
    a = "aaaaaaaaa"
    assert len(a) == 9

@allure.feature(f"{fileName}")
@allure.story("反向用力")
@allure.severity('trivial')
def test_case5():
    a = "aaaaaaaaa"
    assert len(a) == 9

5、Step定制详解

  • 标注测试用例步骤
# coding=utf-8

import os
import allure

fileName = os.path.split(os.path.realpath(__file__))[1]

@allure.feature(f"{fileName}")
@allure.story("正向用力")
@allure.step('用例步骤')
def test_case1():
    """
    用例描述:计算两步相加之和
    """
    with allure.step('step1:返回a的值'):
        a = 1
    with allure.step('step2:返回b的值'):
        b = 2
    with allure.step('step3:预期a+b=3'):
        assert a + b == 4

6、Issue和TestCase定制详解

  • Issue:问题标识,关联标识已有BUG的问题,可为一个url链接地址
  • TestCase: # 用例标识,关联标识用例,可为一个url链接地址
# coding=utf-8

import os
import allure

fileName = os.path.split(os.path.realpath(__file__))[1]

@allure.feature(f"{fileName}")
@allure.story("正向用力")
@allure.step('用例步骤')
@allure.issue("http://www.baidu.com")
@allure.testcase("http://www.testlink.com")
def test_case1():
    """
    用例描述:计算两步相加之和
    """
    with allure.step('step1:返回a的值'):
        a = 1
    with allure.step('step2:返回b的值'):
        b = 2
    with allure.step('step3:预期a+b=3'):
        assert a + b == 4

7、Environment定制详解

  • Environment: 标注环境配置Environment字段
# coding=utf-8

import allure

allure.environment(app_package='com.mobile.fm')
allure.environment(test="hhhhhh")
allure.environment(域名="127.0.0.1")

8、attach定制详解

在报告中增加附件:allure.attach(’arg1’,’arg2’,’arg3’):
arg1:是在报告中显示的附件名称
arg2:表示添加附件的内容
arg3:表示添加的类型(支持:HTML,JPG,PNG,JSON,OTHER,TEXTXML)

# coding=utf-8

import os
import allure

fileName = os.path.split(os.path.realpath(__file__))[1]

@allure.feature(f"{fileName}")
@allure.story("正向用力")
@allure.step('用例步骤')
def test_case1():
    """
    用例描述:计算两步相加之和
    """
    with allure.step('step1:返回a的值'):
        allure.attach('背景1', '没有背景')  # attach可以打印一些附加信息
        allure.attach('背景2', '没有背景')
        a = 1
    with allure.step('step2:返回b的值'):
        with open(r"C:\Users\Administrator\Desktop\qwe.jpg","rb") as f:
            f = f.read()
            allure.attach('图片',f,allure.attach_type.JPG)
        b = 2
    with allure.step('step3:预期a+b=3'):
        assert a + b == 4

 

 

转载于:https://www.cnblogs.com/testlearn/p/11069664.html

Airtest, Poco, Python, Pytest, Allure是一些用于实现Android App自动化测试框架的工具。这些工具结合使用可以实现对应用元素的定位(使用Airtest和Poco),测试用例的管理和执行(使用Pytest),以及生成测试报告(使用Pytest-HTMLAllure)。 Airtest是一个基于Python的UI自动化测试框架,它可以用于Android、iOS等平台的应用自动化测试。它提供了丰富的API来进行界面元素的定位和操作,以及对应用的各种操作进行模拟和验证。 Poco是Airtest的一个扩展库,它提供了一套更简单易用的API来进行应用元素的定位和操作。Poco可以与Airtest结合使用,使得测试用例编写更加简单和高效。 Python是一种流行的编程语言,对于测试框架的编写和测试脚本的编写都非常适用。通过使用Python,可以方便地编写和管理测试用例,以及进行各种数据处理和操作。 Pytest是Python的一个测试框架,它提供了丰富的功能和插件来管理和执行测试用例。Pytest可以与Airtest和Poco结合使用,以便更好地管理和执行自动化测试用例。 Allure是一个用于生成漂亮且可视化的测试报告的工具。它可以将测试结果以图表和图形的形式展示,能够清晰地展示测试用例的执行情况和结果。 综上所述,Airtest、Poco、Python、PytestAllure都是在Android App自动化测试中常用的工具和框架。它们的结合使用可以帮助开发人员和测试人员更方便地进行自动化测试的编写、管理和执行,并生成美观的测试报告来展示测试结果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python + Airtest + poco + pytest + pytest-html 实现Android App自动化测试框架](https://blog.youkuaiyun.com/weixin_38813807/article/details/129179944)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [airtest/poco+pytest+allure+jenkins框架来了](https://blog.youkuaiyun.com/George513/article/details/119034783)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值