• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Python自动化测试框架Allure-pytest功能特性

武飞扬头像
软件测试凡哥
帮助1

Python自动化测试框架【Allure-pytest功能特性介绍】

目录:导读

前言

生成报告

测试代码

目录结构

Allure特性

Environment

Categories

Fixtures and Finalizers

allure.attach

总结

写在最后


学新通

前言

Allure框架是一个灵活的轻量级多语言测试报告工具,它不仅以web的方式展示了简介的测试结果,而且允许参与开发过程的每个人从日常执行的测试中最大限度的提取有用信息。

从dev/qa的角度来看,Allure报告简化了常见缺陷的统计:失败的测试可以分为bug和被中断的测试,还可以配置日志、步骤、fixture、附件、计时、执行历史以及与TMS和BUG管理系统集成,所以,通过以上配置,所有负责的开发人员和测试人员可以尽可能的掌握测试信息。

从管理者的角度来看,Allure提供了一个清晰的“大图”,其中包括已覆盖的特性、缺陷聚集的位置、执行时间轴的外观以及许多其他方便的事情。allure的模块化和可扩展性保证了您总是能够对某些东西进行微调,使Allure更适合您,那么今天我们就来说说如何使报告更加详细的显示我们需要的信息,以及allure与jenkins的集成

生成报告

pytest框架编写的项目如何生成测试报告,这里将不再讲解,具体过程可以参考我之前的文章,往上翻能找得到。

注意:python使用的allure插件为allure-pytest

测试代码

为了大家能够快速的认识allure的所有功能特性,附上完整的测试代码

 conftest.py
 test_allure_feature.py
 test_allure_fixture.py
 categories.json
 environment.properties
 run.py

目录结构

学新通

Allure特性

Environment

在Allure报告中添加环境信息,通过创建environment.properties或者environment.xml文件,并把文件存放到allure-results(这个目录是生成最后的html报告之前,生成依赖文件的目录)目录下

environment.properties

  1.  
    Browser=Chrome
  2.  
    Browser.Version=63.0
  3.  
    Stand=Production
  4.  
    ApiUrl=127.0.0.1/login
  5.  
    python.Version=3.6

或者

environment.xml

  1.  
    <environment>
  2.  
    <parameter>
  3.  
    <key>Browser</key>
  4.  
    <value>Chrome</value>
  5.  
    </parameter>
  6.  
    <parameter>
  7.  
    <key>Browser.Version</key>
  8.  
    <value>63.0</value>
  9.  
    </parameter>
  10.  
    <parameter>
  11.  
    <key>Stand</key>
  12.  
    <value>Production</value>
  13.  
    </parameter>
  14.  
    <parameter>
  15.  
    <key>ApiUrl</key>
  16.  
    <value>127.0.0.1/login</value>
  17.  
    </parameter>
  18.  
    <parameter>
  19.  
    <key>python.Version</key>
  20.  
    <value>3.6</value>
  21.  
    </parameter>
  22.  
    </environment>
学新通

执行run.py查看报告

学新通

Categories

测试报告默认统计两种类型的测试用例结果,失败的用例和故障测试用例,我们可以自定义添加用例的统计类型,同样需要在allure-results目录下新建categories.json文件

  1.  
    [
  2.  
    {
  3.  
    "name": "Ignored tests",
  4.  
    "matchedStatuses": ["skipped"]
  5.  
    },
  6.  
    {
  7.  
    "name": "Infrastructure problems",
  8.  
    "matchedStatuses": ["broken", "failed"],
  9.  
    "messageRegex": ".*bye-bye.*"
  10.  
    },
  11.  
    {
  12.  
    "name": "Outdated tests",
  13.  
    "matchedStatuses": ["broken"],
  14.  
    "traceRegex": ".*FileNotFoundException.*"
  15.  
    },
  16.  
    {
  17.  
    "name": "Product defects",
  18.  
    "matchedStatuses": ["failed"]
  19.  
    },
  20.  
    {
  21.  
    "name": "Test defects",
  22.  
    "matchedStatuses": ["broken"]
  23.  
    }
  24.  
    ]
学新通

执行run.py查看报告

学新通

Fixtures and Finalizers

Fixtures和Finalizers是pytest在测试开始和测试结束调用的方法,allure会自动跟踪每一个fixture的调用,并且详细显示会调用哪些fixture和参数,而且会保留正确的调用顺数

测试代码

test_allure_html.py

  1.  
    def function_scope_step():
  2.  
    print("function_scope_step")
  3.  
     
  4.  
     
  5.  
    def class_scope_step():
  6.  
    print("class_scope_step")
  7.  
     
  8.  
     
  9.  
    def module_scope_step():
  10.  
    print("module_scope_step")
  11.  
     
  12.  
     
  13.  
    def session_scope_step():
  14.  
    print("session_scope_step")
  15.  
     
  16.  
     
  17.  
    def step_inside_test_body():
  18.  
    print("step_inside_test_body")
  19.  
     
  20.  
     
  21.  
    @pytest.fixture(params=[True, False], ids=['param_true', 'param_false'])
  22.  
    def function_scope_fixture_with_finalizer(request):
  23.  
    if request.param:
  24.  
    print('True')
  25.  
    else:
  26.  
    print('False')
  27.  
     
  28.  
    def function_scope_finalizer():
  29.  
    function_scope_step()
  30.  
     
  31.  
    request.addfinalizer(function_scope_finalizer)
  32.  
     
  33.  
     
  34.  
    @pytest.fixture(scope='class')
  35.  
    def class_scope_fixture_with_finalizer(request):
  36.  
    def class_finalizer_fixture():
  37.  
    class_scope_step()
  38.  
     
  39.  
    request.addfinalizer(class_finalizer_fixture)
  40.  
     
  41.  
     
  42.  
    @pytest.fixture(scope='module')
  43.  
    def module_scope_fixture_with_finalizer(request):
  44.  
    def module_finalizer_fixture():
  45.  
    module_scope_step()
  46.  
     
  47.  
    request.addfinalizer(module_finalizer_fixture)
  48.  
     
  49.  
     
  50.  
    @pytest.fixture(scope='session')
  51.  
    def session_scope_fixture_with_finalizer(request):
  52.  
    def session_finalizer_fixture():
  53.  
    session_scope_step()
  54.  
     
  55.  
    request.addfinalizer(session_finalizer_fixture)
  56.  
     
  57.  
     
  58.  
    class TestClass(object):
  59.  
     
  60.  
    def test_with_scoped_finalizers(self,
  61.  
    function_scope_fixture_with_finalizer,
  62.  
    class_scope_fixture_with_finalizer,
  63.  
    module_scope_fixture_with_finalizer,
  64.  
    session_scope_fixture_with_finalizer):
  65.  
    step_inside_test_body()
学新通

执行run.py查看报告

学新通

@allure.step

pytest支持使用@allure.step修饰某些测试用例中需要的函数,使测试用例在allure报告中能够更加详细的显示测试过程

测试代码

test_allure_feature.py文件中修改如下代码

  1.  
    @allure.step("输入用户名")
  2.  
    def input_username():
  3.  
    print("输入用户名")
  4.  
     
  5.  
     
  6.  
    @allure.step("输入密码")
  7.  
    def input_password():
  8.  
    print("输入密码")

执行run.py查看报告

学新通

conftest.py

@allure.step修饰的测试步骤还支持在conftest.py文件中定义,作为fixture的步骤,现在我们在项目目录下新建conftest.py文件,写入如下代码

conftest.py

  1.  
    @allure.step("打开浏览器")
  2.  
    def fixture_step():
  3.  
    pass
  4.  
     
  5.  
     
  6.  
    @pytest.fixture
  7.  
    def init_url():
  8.  
    fixture_step()
  9.  
    yield True

test_allure_feature.py文件中添加如下用例

  1.  
    def test_init_url(self, init_url):
  2.  
    flag = init_url
  3.  
    assert flag == True

执行run.py查看报告

学新通

allure.attach

使用allure.attach可以给报告中添加文件,图片,log,html代码等等。 我们修改test_allure_feature.py中如下用例, 并在用例所在目录添加attach.png图片

  1.  
    def test_failed(self):
  2.  
    """failed"""
  3.  
    try:
  4.  
    assert False
  5.  
    except AssertionError as e:
  6.  
    with open("attach.png", "rb") as f:
  7.  
    context = f.read()
  8.  
    allure.attach(context, "错误图片", attachment_type=allure.attachment_type.PNG)
  9.  
    raise e

执行run.py查看报告

学新通

@allure.description

如果你想在报告中展示测试用例的描述信息,那么你可以使用@allure.description(string)或者@allure.description_html(html代码)修饰你的测试用例,test_allure_feature.py文件修改如下代码

  1.  
    @allure.description("这是一个一直执行失败的测试用例")
  2.  
    def test_failed(self):
  3.  
    """你也可以在这里添加用例的描述信息,但是会被allure.description覆盖"""
  4.  
    try:
  5.  
    assert False
  6.  
    except AssertionError as e:
  7.  
    with open("attach.png", "rb") as f:
  8.  
    context = f.read()
  9.  
    allure.attach(context, "错误图片", attachment_type=allure.attachment_type.PNG)
  10.  
    raise e

执行run.py查看报告

学新通

@allure.title

使用allure.title(title)可以重命名测试用例在allure报告中的名称,test_allure_feature.py文件修改如下代码

  1.  
    @allure.title("登录成功场景-{data}")
  2.  
    @pytest.mark.parametrize("data", login_success_data, ids=ids_login_success_data)
  3.  
    def test_login_success(self, data):
  4.  
    """测试登录成功"""
  5.  
    user = input_username(data["user"])
  6.  
    pwd = input_password(data["pwd"])
  7.  
    result = login(user, pwd)
  8.  
    assert result == data["expected"]

学新通

@allure.link

@allure.testcase

@allure.issue

这三种特性都可以给测试用例添加一个链接,test_allure_feature.py文件修改如下代码

  1.  
    @allure.testcase("https://www.cnblogs.com/linuxchao/", "测试用例地址")
  2.  
    def test_init_url(self, init_url):
  3.  
    flag = init_url
  4.  
    assert flag == True
  5.  
     
  6.  
    @allure.link("https://www.cnblogs.com/linuxchao/", name="bug链接")
  7.  
    @allure.description("这是一个一直执行失败的测试用例")
  8.  
    def test_failed(self):
  9.  
    """你也可以在这里添加用例的描述信息,但是会被allure.description覆盖"""
  10.  
    try:
  11.  
    assert False
  12.  
    except AssertionError as e:
  13.  
    with open("attach.png", "rb") as f:
  14.  
    context = f.read()
  15.  
    allure.attach(context, "错误图片", attachment_type=allure.attachment_type.PNG)
  16.  
    raise e
  17.  
     
  18.  
    @allure.issue("https://www.cnblogs.com/linuxchao/", "错误链接")
  19.  
    def test_broken(self):
  20.  
    """broken"""
  21.  
    with open("broken.json", "r", encoding='utf8') as f:
  22.  
    f.read()
学新通

执行run.py查看报告

学新通

学新通

学新通

@allure.feature

@allure.story

feature和story被称为行为驱动标记,因为使用这个两个标记,通过报告可以更加清楚的掌握每个测试用例的功能和每个测试用例的测试场景

在test_allure_feature.py文件中的测试类使用@allure.feature修饰, 测试方法使用@allure.story修饰

执行run.py查看报告

学新通

 以上两种标记不仅仅能够在测试报告中显示,而且还可以使用命令执行指定的测试模块或者场景

学新通

@allure.severity

此标记用来标识测试用例或者测试类的级别,分为blocker,critical,normal,minor,trivial5个级别,下面们把测试用例按级别标记,并查看一下测试报告

学新通

总结

写在最后

如果你觉得文章还不错,请大家 点赞、分享、留言 下,因为这将是我持续输出更多优质文章的最强动力!

看到这篇文章的人有觉得我的理解有误的地方,也欢迎评论和探讨~

你也可以加入下方的的群聊去和同行大神交流切磋

 学新通学新通学新通学新通学新通学新通学新通学新通学新通

这篇好文章是转载于:编程之路

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 编程之路
  • 本文地址: /boutique/detail/tanhhgbakg
系列文章
更多 icon
同类精品
更多 icon
继续加载