首页 > 其他分享 >pytest系列——allure(四)之在测试用例添加描述(@allure.description())

pytest系列——allure(四)之在测试用例添加描述(@allure.description())

时间:2022-08-25 10:37:00浏览次数:90  
标签:__ 测试报告 description pytest allure test 描述

前言

allure支持往测试报告中对测试用例添加非常详细的描述语用来描述测试用例详情;这对阅读测试报告的人来说非常的友好,可以清晰的知道每个测试用例的详情。

allure添加描述的三种方式:

  • 使用装饰器@allure.description,传递一个字符串参数来描述测试用例。
  • 使用装饰器@allure.description_html,传递一段HTML文本,这将在测试报告的“Description”部分渲染出来。
  • 直接在测试用例方法中通过编写文档注释的方法来添加描述。

实例

# file_name: test_description.py


import pytest
import allure


@allure.description("""
多行描述语:
这是通过传递字符串参数的方式添加的一段描述语,
使用的是装饰器@allure.description
""")
def test_description_provide_string():
    assert True


@allure.description_html("""
<h1>Test with some complicated html description</h1>
<table style="width:100%" border="1">
  <tr>
    <th>Name</th>
    <th>Age</th>
    <th>Sex</th>
  </tr>
  <tr align="center">
    <td>lwjnicole</td>
    <td>28</td>
    <td>男</td>
  </tr>
  <tr align="center">
    <td>nicole</td>
    <td>29</td>
    <td>女</td>
  </tr>
</table>
""")
def test_description_privide_html():
    assert True


def test_description_docstring():
    """
    这是通过文本注释的方式添加的描述语

    同样支持多行描述

    大家好,我是lwjnicole
    :return:
    """
    assert True


if __name__ == '__main__':
    pytest.main(['-s', 'test_description.py'])

执行命令:

> pytest test_description.py --alluredir=./report/result_data

> allure serve ./report/result_data

1、查看测试报告展示效果【下面的测试报告显示的是通过装饰器@allure.description传递字符串参数来添加描述语的方式】:

image

2、查看测试报告展示效果【 下面的测试报告显示的是通过装饰器@allure.description_html传递一段HTML文本来添加描述语的方式,这段HTML会渲染在报告的"Description"部分】:

image

3、查看测试报告展示效果【 下面的测试报告显示的是通过直接在测试用例方法中编写文档注释来添加描述语的方式】:

image

allure动态更新描述语(allure.dynamic.description):

# file_name: test_description.py


import pytest
import allure


@allure.description("这是更新前的描述语,在使用allure.dynamic.description后将会被更新成新的描述语")
def test_dynamic_description():
    assert True
    allure.dynamic.description("这是通过使用allure.dynamic.description更新后的描述语")


if __name__ == '__main__':
    pytest.main(['-s', 'test_description.py'])

执行命令:

> pytest test_description.py --alluredir=./report/result_data

> allure serve ./report/result_data

查看测试报告展示效果【从下面的测试报告中可以看到,Description中展示的是使用allure.dynamic.description更新后的描述语;而不是allure.description传递的那一段字符串描述语】:

image

标签:__,测试报告,description,pytest,allure,test,描述
From: https://www.cnblogs.com/guanqibuyu/p/16623420.html

相关文章