Pytest单元测试框架-allure测试报告

825 阅读4分钟

目录

Allure Test Report

对于不同的编程语言,有很多很酷的测试框架。不幸的是,它们中只有少数能够提供测试执行输出的良好表示。Qameta软件测试团队正在致力于Allure——一个开源框架,旨在创建测试执行报告,让团队中的每个人都清楚。

参考文章:

Allure官方文档:https://docs.qameta.io/allure/

blog.csdn.net/liuchunming…

pytest与allure(/əˈlo͝or/)中间的火花

  1. 安装配置allure:github.com/allure-fram…

    • 下载后解压文件并添加到对应操作系统的环境变量中
  2. 安装allure-pytest插件:pip install allure-pytest

  3. 添加allure测试结果报告存放路径pytest --alluredir=./report/allure_report_01(此方法会运行测试文件,并得到结果汇总到报告中,你还需执行下一步)

  4. 查看实际的报告(在浏览器显示,这样会在本地启用个web服务):allure serve ./report/allure_report_01

  5. 生成测试报告(每次生成前清空测试报告目录):allure generate ./report/allure_report_01/ -o ./report01/ --clean

  6. 注意事项

如果直接执行pytest --alluredir=report,将会直接在当前目录下新建一个report的目录,并会运行pytest测试;

定制allure报告

  1. 给allure报告中的测试方法添加描述: 只需要在测试函数/方法下加上"""描述的内容"""

    def test_sum(self):
    """ 这部分内容将显示在 allure报告中"""
        # assert进行断言
        assert 1 == 2
    
  2. 测试步骤描述:在测试方法之前添加@allure.step('步骤描述')内容

    #!/usr/bin/env/python3
    # -*- coding:utf-8 -*-
    """
    @project: pytest_study
    @author: zy7y
    @file: test_09.py
    @ide: PyCharm
    @time: 2020/7/27
    """
    
    import allure
    import pytest
    
    
    @allure.step('第一步')
    def passing_step():
        pass
    
    
    @allure.step('第二步')
    def step_with_nested_steps():
        nested_step()
    
    
    @allure.step('第二.1步')
    def nested_step():
        nested_step_with_arguments(1, 'abc')
    
    
    @allure.step('第二.2步')
    def nested_step_with_arguments(arg1, arg2):
        pass
    
    
    def test_with_imported_step():
        passing_step()
    
    
    def test_with_nested_steps():
        passing_step()
        step_with_nested_steps()
    

    Snipaste_2020-07-27_23-21-30.png

    步骤可以有一个描述行,该描述行支持传递的位置参数和关键字参数的占位符。关键字参数的默认参数也将被捕获。

    import allure
    
    @allure.step('测试步骤标题中的占位符, 第一个参数: "{0}", 第二个参数keyword: "{key}"')
    def step_with_title_placeholders(arg1, key=None):
        pass
    
    def test_steps_with_placeholders():
        step_with_title_placeholders(1, key='something')
        step_with_title_placeholders(2)
        step_with_title_placeholders(3, 'anything')
    
  3. ids测试用例标题: 每条用例ids关键字参数的内容将被填充到allure报告中

    @pytest.mark.parametrize('username, password, expect',
                             [('admin', '123456', '登录成功'),
                              ('admin', '111111', '密码错误')], ids=["正常登录测试用例标题", "密码错误登录测试用例"])
    def test_login(username, password, expect):
        if username == 'admin' and password == '123456':
            assert expect == '登录成功'
        else:
            assert expect == '密码错误'
    

    Snipaste_2020-07-27_23-29-48.png

  4. 向allure某个测试用例/函数/方法中添加附件:arllure.attcah(body, name, attachment_type, extension)或者添加本地附件allure.attach.file(source, name, attachment_type, extension)

    1. body - 需要写入的文件内容
    2. name - 文件名称
    3. attachment_type - 文件类型 allure.attachment_type
    4. extension - 文件后缀名
    5. source - 文件所在路径
    import allure
    import pytest
    
    
    @pytest.fixture
    def attach_file_in_module_scope_fixture_with_finalizer(request):
        # 添加一个txt文件,文件名称为 file_name, 文件里面的内容为:allure文本附件内容
        allure.attach('allure文本附件内容', 'file_name', allure.attachment_type.TEXT)
    
    
    def test_multiple_attachments(attach_file_in_module_scope_fixture_with_finalizer):
        # 本地上传一个图片文件给allure,
        allure.attach.file('./Snipaste_2020-07-27_23-29-48.png', name='allure专用', attachment_type=allure.attachment_type.PNG)
        allure.attach('<head></head><body> a page </body>', 'html附件', allure.attachment_type.HTML)
    

    Snipaste_2020-07-28_00-03-18.png

  5. 定制测试标题(测试函数在allure中的显示方式)内容:@allure.title()

    @allure.title('测试标题:测试打开blog')
    @allure.step('3. 点击回车')
    def test_open_blog(browse_driver):
        browse_driver.find_element_by_id('su').click()
        #allure.dynamic.title('在成功完成测试后,标题被替换为这一行。')
    

    Snipaste_2020-07-28_00-11-13.png

  6. 添加链接:@allure.link或者@allure.issue或者@allure.testcase

    # 显示链接
    @allure.link('https://www.baidu.com/')
    def test_with_link():
        pass
    
    
    @allure.link('https://www.youtube.com/watch?v=Su5p2TqZxKU', name='allure.link 实现的,传递了一个url,一个name')
    def test_with_named_link():
        pass
    
    
    # 可以用来放禅道bug地址,它的图标是个虫子挺好的
    @allure.issue('https://www.baidu.com', '这就是显示图标,点击可以访问链接的 allure.issue')
    def test_with_issue_link():
        pass
    
    # 链接栏下方显示 测试用例标题, 点击实现跳转
    @allure.testcase(TEST_CASE_LINK, '测试用例标题')
    def test_with_testcase_link():
        pass
    

    Snipaste_2020-07-28_00-26-12.png

  7. 按标签分组执行:@allure.feature and @allure.story

    @allure.story('epic_1')
    def test_with_epic_1():
        pass
    
    
    @allure.story('story_1')
    def test_with_story_1():
        pass
    
    @allure.story('story_2')
    def test_with_story_2():
        pass
    
    
    @allure.feature('feature_2')
    @allure.story('story_2')
    def test_with_story_2_and_feature_2():
        pass
    
    # pytest test_09.py --allure-stories story_1,story_2 只执行 含这两个标签的
    
    # pytest test_09.py --allure-features feature2 --allure-stories story2
    
  8. 按严重度(优先级)分组执行:@allure.severity

    # test.py
    import allure
    
    @allure.severity('blocker')
    def test_with_no_severity_label():
        pass
    
    
    @allure.severity(allure.severity_level.TRIVIAL)
    def test_with_trivial_severity():
        pass
    
    
    @allure.severity(allure.severity_level.NORMAL)
    def test_with_normal_severity():
        pass
    
    
    @allure.severity(allure.severity_level.NORMAL)
    class TestClassWithNormalSeverity(object):
    
        def test_inside_the_normal_severity_test_class(self):
            pass
    
        @allure.severity(allure.severity_level.CRITICAL)
        def test_inside_the_normal_severity_test_class_with_overriding_critical_severity(self):
            pass
     
    # 将执行严重性为NORML、CRITICAL的方法/类/函数
    # pytest tests.py --allure-severities normal,critical
    
    严重度(优先级)解释:
    BLOCKER = 'blocker'		系统阻断  对应bug优先级: 马上解决
    CRITICAL = 'critical'	严重缺陷	- 
    NORMAL = 'normal'		普通	- 可倒数第三执行  正常处理
    MINOR = 'minor'			易用性问题
    TRIVIAL = 'trivial'	易用性问题
    

    关于bug严重度、优先级可以看这里:blog.sina.com.cn/s/blog_4adc…