Allure生成测试报告这样生成

Allure是一个开源的测试报告生成框架,提供了测试报告定制化功能,相较于我们之前使用过pytest-html插件生成的html格式的测试报告,通过Allure生成的报告更加规范、清晰、美观。

pytest框架支持使用Allure生成测试报告,接下来让介绍pytest怎样结合Allure生成测试报告。

环境搭建
安装allure-pytest

步骤1需要先安装插件allure-pytest,可以理解为用于连接pytestallure,使它们可以结合使用。

安装命令:pip install allure-pytest

安装Allure

步骤2中需要安装Allure,需要去github下载,地址为:Releases · allure-framework/allure2 · GitHub

根据操作系统在最新版本中选择对应格式的安装文件进行下载,Windows系统选择allure-2.xx.x.zip下载,如下图所示:

下载后解压文件,并bin文件所在的路径加入系统环境变量,再重启电脑,怎样加入环境变量这里不啰嗦,不知道的同学可以百度。

至此,环境搭建完成。

定制报告

Allure提供了很多特性用于定制生成测试报告,脚本中加入这些特性可以对测试步骤进行详细的说明,且不会对测试代码逻辑产生影响。

接下来以在线购物平台的购物车功能模块下单模块简单举例说明,测试模块test_case.py代码如下:

 

import allure

import pytest

import os


@allure.step("登录获取token")

def get_token():

print("请求登录接口获取token")


@allure.step("加入购物车")

def add_to_shopping_trolley():

print("请求加入购物车接口")


@allure.step("查询我的购物车")

def get_shopping_trolley_goods():

print("请求查询我的购物车接口")


@allure.step("清空购物车")

def empty_shopping_trolley():

print("请求清空购物车接口")


@allure.step("下单")

def place_order():

print("请求下单接口")



@allure.epic("xx在线购物平台接口测试")

@allure.feature("购物车功能模块")

class TestShoppingTrolley:


@allure.story("商品加入购物车")

@allure.title("正向用例--将库存数>0的商品加入购物车")

@allure.description("校验库存数不为0的商品加入购物车是否正常")

@allure.severity("critical")

def test_add_goods(self):

get_token()

add_to_shopping_trolley()


@allure.story("商品加入购物车")

@allure.title("异常用例--将库存数=0的商品加入购物车")

@allure.description("校验库存数为0的商品加入购物车是否提示正确的错误信息")

@allure.severity("normal")

def test_add_goods_error(self):

get_token()

add_to_shopping_trolley()


@allure.story("查询购物车商品数量")

@allure.title("查询购物车所有商品的总数量")

@allure.description("校验查询购物车所有商品的总数量是否正常")

@allure.severity("critical")

def test_get_goods_quantity(self):

get_token()

add_to_shopping_trolley()

get_shopping_trolley_goods()


@allure.story("查询购物车商品数量")

@allure.title("查询购物车单个商品的数量")

@allure.description("校验查询购物车单个商品的数量是否正常")

@allure.severity("critical")

def test_get_goods_quantity(self):

get_token()

add_to_shopping_trolley()

get_shopping_trolley_goods()


@allure.story("清空购物车")

@allure.title("加入商品后再清空购物车")

@allure.description("校验清空购物车接口功能是否正常")

@allure.severity("normal")

def test_empty_shopping_trolley(self):

get_token()

add_to_shopping_trolley()

empty_shopping_trolley()



@allure.epic("xx在线购物平台接口测试")

@allure.feature("下单模块")

class TestPlaceOrder:


@allure.story("购物车下单")

@allure.title("商品加入购物车再下单")

@allure.description("校验清购物车下单功能是否正常")

@allure.severity("critical")

def test_place_order(self):

get_token()

add_to_shopping_trolley()

place_order()


@allure.story("立即购买下单")

@allure.title("选择商品不加入购物车立即购买下单")

@allure.description("校验立即购买下单功能是否正常")

@allure.severity("critical")

def test_order(self):

get_token()

place_order()

上面测试代码中使用了Allure的一些特性,为了更好的理解这些特性的使用,我们可以将测试脚本由上至下进行分层:

  1. 被测系统,即被测系统的描述,如在线购物商城
  2. 功能模块,一个被测软件系统包含一个或多个功能模块,如在线购物商城包含登录、购物车、下单、支付、发货等模块
  3. 使用场景,一个功能模块中包含一个或多个用户使用场景,如购物车模块包含加入购物车、修改数量、清空购物车的场景
  4. 测试用例,一个场景包含一条或多条测试用例,如加入购物车包含库存数>0 或 <0等测试用例
  5. 测试步骤,一条测试用例由一个或多个测试步骤构成,如将库存数>0商品加入购物车,测试步骤为:登录-->商品加入购物车

对照以上分层,我们再来理解代码中使用的这些Allure特性,如下:

  • @allure.epic(),用于描述被测软件系统

  • @allure.feature(),用于描述被测软件的某个功能模块

  • @allure.story(),用于描述功能模块下的功能点或功能场景,也即测试需求

  • @allure.title(),用于定义测试用例标题

  • @allure.description(),用于测试用例的说明描述

  • @allure.severity(),标记测试用例级别,由高到低分为 blocker、critical、normal、minor、trivial 五级

  • @pytest.allure.step(),标记通用函数使之成为测试步骤,测试方法/测试函数中调用此通用函数的地方会向报告中输出步骤描述

生成报告
生成Allure报告步骤

pytestAllure生成测试报告需要经过如下两步操作:

  1. 首先,生成测试结果数据:

    
    # python代码执行
    
    pytest.main(['testcase/test_case.py', '-s', '-q', '--alluredir', './result'])
    
    # 命令行形式
    
    pytest testcase/test_case.py --alluredir ./result

    即运行testcase/目录下的测试用例,将测试结果以json文件的形式保存至当前目录下的result文件夹中。

    参数--alluredir用于指定测试结果保存路径。

  2. 然后,生成HTML格式的测试报告:

    
    # python代码执行
    
    os.system('allure generate ./result -o ./report --clean')
    
    # 命令行形式
    
    allure generate ./result -o ./report --clean

    即将当前目录下的result文件夹中的json数据,生成测试报告结果及index.html,并保存至当前目录下的report文件夹中。

    --clean表示先清除之前的测试报告,使用与否视情况自行选择。

执行代码

因此,执行模块run.py代码编写如下:

run.py


if __name__ == '__main__':

pytest.main(['testcase/test_case.py', '-s', '-q', '--alluredir', './result'])

os.system('allure generate ./result -o ./report --clean')

运行run.py,结果如下:

报告结果展示

运行run.py后,在run.py同级目录下新增了result文件夹,以及文件夹下的json文件,有多少条测试用例就生成多少个名称为xxxx-result.json的结果文件。

同样在run.py同级目录下新增了report文件夹,report文件夹中生成了一些文件,包括index.html,如下:

在浏览器中打开index.html,打开后首页如下:

选择点击Behaviors后,结果如下:

Allure报告默认语言为英文,可以选择中文,如下:

总结

可以把epic、feature、story理解为将测试用例按照功能模块进行分类,epic为一级类目,feature为二级类目,story为三级类目。

而title、description、severity、step等则用于测试用例自身相关的描述定义。

当然,Allure还有其他的常用特性,下篇文章我们再继续学习。

最后作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,坚持几天便放弃的感受的话,在这里我给大家分享一些软件测试的学习资源,这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,希望能给你前进的路上带来帮助。如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你! 

### Allure 生成测试报告 示例代码 以下是基于 Python 和 Pytest 的 Allure 测试报告生成示例代码: #### 安装依赖库 首先需要安装 `pytest` 和 `pytest-allure-adaptor` 或者 `allure-pytest` 插件。可以通过以下命令完成安装: ```bash pip install pytest allure-python-commons allure-pytest ``` #### 编写测试用例 创建一个简单的测试脚本,例如命名为 `test_example.py`。 ```python import pytest @pytest.mark.parametrize("x, y, expected", [ (1, 2, 3), (5, 7, 12), (-1, -1, -2) ]) def test_addition(x, y, expected): assert x + y == expected, f"{x} + {y} should equal {expected}" def test_string_comparison(): string1 = "hello" string2 = "world" assert string1 != string2, "Strings are not the same" @pytest.mark.skip(reason="This is an example of a skipped test") def test_skipped_test(): pass ``` #### 执行测试并生成 Allure 报告 运行以下命令以执行测试并将结果保存到指定目录中: ```bash pytest --alluredir=./results ``` 此命令会将测试结果存储在当前路径下的 `./results` 文件夹中[^3]。 #### 启动本地 Allure 报告服务 使用以下命令启动 Allure 并查看生成测试报告: ```bash allure serve ./results/ ``` 该命令会在浏览器中打开交互式的 HTML 报告页面[^1]。 --- ### 进一步优化报表展示 为了使 Allure 报告更加直观和详细,可以添加一些装饰器来增强描述信息。例如,在测试函数前加入 `@allure.description` 和 `@allure.severity` 装饰器。 修改后的测试用例如下所示: ```python import pytest import allure @allure.description("Test addition operation with multiple inputs.") @allure.severity(allure.severity_level.NORMAL) @pytest.mark.parametrize("x, y, expected", [ (1, 2, 3), (5, 7, 12), (-1, -1, -2) ]) def test_addition(x, y, expected): with allure.step(f"Verify that {x} + {y} equals {expected}"): assert x + y == expected, f"{x} + {y} should equal {expected}" @allure.description("Compare two strings to ensure they are different.") @allure.severity(allure.severity_level.CRITICAL) def test_string_comparison(): string1 = "hello" string2 = "world" with allure.step(f"Ensure '{string1}' and '{string2}' are distinct."): assert string1 != string2, "Strings are not the same" @pytest.mark.skip(reason="This is an example of a skipped test") @allure.description("Example of a skipped test case.") def test_skipped_test(): pass ``` 上述代码通过引入 `allure.step()` 方法增强了每一步操作的日志记录功能,从而让最终生成的报告更清晰易读[^4]。 --- ### 总结 以上即为完整的 Allure 测试报告生成过程及其对应的代码实现方式。它涵盖了从编写测试用例、执行测试到最后生成可视化报告的所有环节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值