Python Allure库的使用示例教程

 更新时间:2023年12月22日 14:47:13   作者:奔向理想的星辰大海  
Python Allure库是一个实用可靠的测试报告框架,它几乎可以与Python的其他库和框架无缝集成,利用Python Allure库,可以轻松生成易于阅读的测试报告,让测试变得更加简单便捷,本文主要介绍Python Allure库的使用,感兴趣的朋友一起看看吧

Python Allure库是一个开源的跨平台的测试报告框架,用于生成漂亮、易于阅读和易于理解的测试报告。它支持Python的标准TestRunner框架并且可以生成不同的报告格式,如HTML报告、json报告等。本文将围绕着Python Allure库来进行讲解,让大家了解它的用途和主要功能。

一、安装Python Allure库

要使用Python Allure库,需要在命令行中安装Allure命令行工具。

brew install allure

安装完Allure命令行工具后,可通过pip安装Python Allure库。

pip install allure-pytest

二、使用Python Allure库

1.简单示例

import pytest
import allure
@allure.step("参数相加 : {0},{1}")
def add(x, y):
    return x + y
@pytest.mark.parametrize('x', [0, 1])
@pytest.mark.parametrize('y', [2, 3])
def test_add(x, y):
    with allure.step("步骤1:输入两个参数"):
        print("输入参数:x->{},y->{}".format(x, y))
    with allure.step("步骤2:调用相加方法"):
        result = add(x, y)
    with allure.step("步骤3:输出结果"):
        print("输出结果:{}".format(result))
if __name__ == '__main__':
    pytest.main(['-s', '-q', '--alluredir', './report/'])
import pytest
import allure
@allure.step("参数相加 : {0},{1}")
def add(x, y):
    return x + y
@pytest.mark.parametrize('x', [0, 1])
@pytest.mark.parametrize('y', [2, 3])
def test_add(x, y):
    with allure.step("步骤1:输入两个参数"):
        print("输入参数:x->{},y->{}".format(x, y))
    with allure.step("步骤2:调用相加方法"):
        result = add(x, y)
    with allure.step("步骤3:输出结果"):
        print("输出结果:{}".format(result))
if __name__ == '__main__':
    pytest.main(['-s', '-q', '--alluredir', './report/'])

在脚本中首先引入pytest和allure库,然后使用@allure.step包装每个测试步骤,使用@allure.parametrize注释来测试方法的步骤,最后使用pytest.main运行脚本,运行结果将生成在“./report/”文件夹中。

2.定制化报告

Python Allure库提供了多种注释,来增加测试报告的可读性,让报告更加直观。

使用@allure.feature注释来制定测试特性:

@allure.feature("加法运算测试")
def test_add():
    pass

使用@allure.story注释来制定测试场景:

@allure.story("测试加法")
def test_add():
    pass

3.报告展示

在生成报告后,可以通过运行命令在浏览器中查看报告。

allure serve 报告目录

三、总结

Python Allure库是一个实用可靠的测试报告框架,它几乎可以与Python的其他库和框架无缝集成。利用Python Allure库,可以轻松生成易于阅读的测试报告,让测试变得更加简单便捷。

到此这篇关于Python Allure库的使用的文章就介绍到这了,更多相关Python Allure库使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论