Pytest自动化测试框架完美结合Allure

🍅 视频学习:文末有免费的配套视频可观看

🍅 关注公众号【互联网杂货铺】,回复 1 ,免费获取软件测试全套资料,资料在手,涨薪更快

简介

Allure Framework是一种灵活的、轻量级、多语言测试报告工具。

不仅可以以简洁的网络报告形式非常简洁地显示已测试的内容,

而且还允许参与开发过程的每个人从日常执行中提取最大程度的有用信息和测试。

从开发/测试的角度来看:

Allure报告可以快速查看到缺陷点,可以将测试未通过划分为Bug和中断的测试。

还可以配置日志,步骤,固件,附件,时间,历史记录,以及与TMS的集成和Bug跟踪系统,以便掌握所有信息。

从管理者的角度来看:

Allure提供了一个清晰的全局,涵盖了所涵盖的功能,缺陷聚集的位置,执行时间表,以及许多其他方便的事情。

独特的模块化和可扩展性,确保你能够进行适当的微调,以使更适合你自己。

官方文档:Allure Framework

部署使用

Pytest作为一个高扩展性、功能强大的自动化测试框架,自身的测试结果是较为简单的,如果想要一份完整测试报告需要其他插件的支持。

如果你对测试报告要求没那么高,你可以使用 pytest-html插件,基本覆盖了测试报告的常规内容。

但是如果你想查看清晰的测试过程、多维度的测试报告、自定义一些输出,以及与用例和缺陷系统集成等,那allure-python将是你的"不二人选"。

注意:allure-pytest从1.7之后已弃用,从2.0版本开始迁移至allure-python项目(即使用allure2),另外要运行allure命令行也需要Java的支持。

1、安装:

1)allure-pytest插件:

pip install -U allure-pytest

这将安装allure-pytest和allure-python-commons程序包,以生成与allure2兼容的报告数据。

2)allure工具:

官方下载地址:Releases · allure-framework/allure2 · GitHub

解压软件包(建议直接放到Python文件夹下),然后添加bin目录到环境变量中,最后使用 allure --version 验证是否安装成功。

2、基本使用

>>> 要使allure侦听器能够在测试执行过程中收集结果,只需添加 --alluredir  选项并提供路径即可存储结果。

pytest --alluredir=<directory-with-results>

如果你运行后进行了用例更改,那么下次运行可能还是会查看到之前记录,可添加 --clean-alluredir 选项清除之前记录。

pytest --alluredir=<directory-with-results> --clean-alluredir

>>> 要在测试完成后查看实际报告,你需要使用allure命令行应用程序从结果生成报告。

1)在默认浏览器中显示生成的报告

allure serve <my-allure-results>

2)要从现有的Allure结果生成报告,可以使用以下命令:

allure generate <directory-with-results>

默认报告将生成到allure-report文件夹,你可以使用 -o 标志更改目标文件夹:

allure generate <directory-with-results> -o <directory-with-report>

3)生成报告后,可以在默认系统浏览器中将其打开,只需运行:

allure open <directory-with-report>

你也可以找到该目录,使用浏览器打开该目录下index.html。注意:有时打开会找不到数据或者乱码,如果你使用的是pycharm,请在pycharm中右击打开。

4)如果要删除生成的报告数据,只需运行:

allure report clean

默认情况下,报告命令将在 allure-results 文件夹中查找报告,如果要从其他位置使用报告,则可以使用 -o 选项。

5)你也可以使用 allure help 命令查看更多帮助。

测试报告

你可以在allure报告中看到所有默认的pytest状态:只有由于一个断言错误而未成功进行的测试将被标记为失败,其他任何异常都将导致测试的状态为坏。

示例:

# test_sample.py
import pytest
 
# 被测功能
def add(x, y):
    return x + y
 
# 测试类
class TestAdd:
 
    # 跳过用例
    def test_first(self):
        pytest.skip('跳过')
        assert add(3, 4) == 7
 
    # 异常用例
    def test_second(self):
        assert add(-3, 4) == 1
        raise Exception('异常')
 
    # 成功用例
    def test_three(self):
        assert add(3, -4) == -1
 
    # 失败用例
    def test_four(self):
        assert add(-3, -4) == 7

运行:

E:\workspace-py\Pytest>pytest test_sample.py --alluredir=report --clean-alluredir
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        
 
test_sample.py sF.F                                                                                                                                                [100%]
 
=============================================================================== FAILURES ================================================================================
__________________________________________________________________________ TestAdd.test_second __________________________________________________________________________
 
self = <test_sample.TestAdd object at 0x000000000464F278>
 
    def test_second(self):
        assert add(-3, 4) == 1
>       raise Exception('异常')
E       Exception: 异常
 
test_sample.py:21: Exception
___________________________________________________________________________ TestAdd.test_four ___________________________________________________________________________
 
self = <test_sample.TestAdd object at 0x000000000464FD30>
 
    def test_four(self):
>       assert add(-3, -4) == 7
E       assert -7 == 7
E        +  where -7 = add(-3, -4)
 
test_sample.py:29: AssertionError
======================================================================== short test summary info ========================================================================
FAILED test_sample.py::TestAdd::test_second - Exception: 异常
FAILED test_sample.py::TestAdd::test_four - assert -7 == 7
================================================================ 2 failed, 1 passed, 1 skipped in 0.14s =================================================================

生成报告:

E:\workspace-py\Pytest>allure generate --clean report
Report successfully generated to allure-report

查看目录:

E:\workspace-py\Pytest>tree
文件夹 PATH 列表
卷序列号为 B2C1-63D6
E:.
├─.idea
├─.pytest_cache
│  └─v
│      └─cache
├─allure-report
│  ├─data
│  │  ├─attachments
│  │  └─test-cases
│  ├─export
│  ├─history
│  ├─plugins
│  │  ├─behaviors
│  │  ├─jira
│  │  ├─junit
│  │  ├─packages
│  │  ├─screen-diff
│  │  ├─trx
│  │  ├─xctest
│  │  ├─xray
│  │  └─xunit-xml
│  └─widgets
├─report
└─__pycache__

查看报告:

Overview:总览,显示用例执行情况、严重程度分布、环境信息等。
Categories:分类,按用例执行结果分类,异常错误和失败错误。
Suites:套件,按测试用例套件分类,目录 ->测试文件 -> 测试类 ->测试方法。
Graphs:图表,显示用例执行分布情况,状态、严重程度、持续时间、持续时间趋势、重试趋势、类别趋势、整体趋势。
Timeline:时间线,显示用例耗时情况,具体到各个时间点用例执行情况
Behaviors:行为,按用例行为举止分类(以标记文字形式显示,需要用例添加allure相关装饰器)
Package:配套,按目录形式分类,显示不同的目录用例执行情况。

用例详情:

Allure报告不仅能显示pytest不同执行结果状态,错误情况,固件等,还能捕获参数化测试所有参数名称和值。

用例:

# test_sample.py
import pytest
import allure
 
# 被测功能
def add(x, y):
    return x + y
 
# 测试类
@allure.feature("测试练习")
class TestLearning:
    data = [
        [3, 4, 7],
        [-3, 4, 1],
        [3, -4, -1],
        [-3, -4, 7],
    ]
    @allure.story("测试用例")
    @allure.severity(allure.severity_level.NORMAL)
    @pytest.mark.parametrize("data", data)
    def test_add(self, data):
        assert add(data[0], data[1]) == data[2]

报告:

同时,在这我也准备了一份软件测试视频教程(含接口、自动化、性能等),需要的可以直接在下方观看就行,希望对你有所帮助!【公众号:互联网杂货铺】免费领取软件测试资料。

字节大佬,一周讲完,自动化测试项目实战,这套教程是怎么称霸B站的?【2024最新版】

  • 19
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是pytest接口自动化框架代码,并结合allure进行测试报告展示: ```python import pytest import requests import allure @allure.feature('API Test') class TestApi: @allure.story('Get Request') def test_get_request(self): url = "https://jsonplaceholder.typicode.com/posts/1" response = requests.get(url) assert response.status_code == 200 response_json = response.json() assert response_json["userId"] == 1 assert response_json["id"] == 1 assert response_json["title"] == "sunt aut facere repellat provident occaecati excepturi optio reprehenderit" assert response_json["body"] == "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" @allure.story('Post Request') def test_post_request(self): url = "https://jsonplaceholder.typicode.com/posts" payload = { "userId": 1, "title": "test title", "body": "test body" } headers = { "Content-Type": "application/json" } response = requests.post(url, json=payload, headers=headers) assert response.status_code == 201 response_json = response.json() assert response_json["userId"] == 1 assert response_json["title"] == "test title" assert response_json["body"] == "test body" if __name__ == '__main__': pytest.main(['-s', '-q', '--alluredir', './report']) ``` 以上代码中,使用了pytest进行接口自动化测试,并结合allure进行测试报告展示。在每个测试用例上添加了@allure.feature和@allure.story装饰器,用于定义测试用例的特性和故事,方便测试报告展示和分类。 在运行pytest时,使用--alluredir指定测试报告生成的目录,然后使用allure命令生成测试报告,例如: ``` allure generate ./report -o ./report_html --clean ``` 以上命令会在report目录下生成测试报告,并保存在report_html目录下。可以使用浏览器打开report_html/index.html文件查看测试报告。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值