Allure除了具备Pytest基本状态外,其余几乎全部功能也都支持。html
若是你想对测试用例进行严重等级划分,可使用 @allure.severity 装饰器,它能够应用于函数,方法或整个类。session
它以 allure.severity_level 枚举值做为参数,分别为:BLOCKER(中断),CRITICAL(严重),NORMAL(常规),MINOR(轻微),TRIVIAL(不重要)。函数
示例:测试
# test_sample.py import allure # 两数相加 def add(x, y): return x + y # 测试类 @allure.severity(allure.severity_level.TRIVIAL) class TestAdd: @allure.severity(allure.severity_level.MINOR) def test_first(self): assert add(3, 4) == 7 @allure.severity(allure.severity_level.NORMAL) def test_second(self): assert add(-3, 4) == 1 @allure.severity(allure.severity_level.CRITICAL) def test_three(self): assert add(3, -4) == -1 @allure.severity(allure.severity_level.BLOCKER) def test_four(self): assert add(-3, -4) == -7
运行:this
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 .... [100%] =========================================================================== 4 passed in 0.06s ===========================================================================
报告:spa
你还能够经过 --allure-severities 选项指定严重等级运行,多个以逗号分隔。code
E:\workspace-py\Pytest>pytest test_sample.py --allure-severities normal,critical ========================================================================== 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 .. [100%] =========================================================================== 2 passed in 0.02s ===========================================================================
若是你想对测试功能、测试场景进行行为描述,能够分别使用装饰器:@allure.feature 和 @allure.story 。orm
示例:htm
# test_sample.py import allure # 两数相加 def add(x, y): return x + y @allure.feature('测试类') class TestAdd: @allure.story('01测试两个正数相加') def test_first(self): assert add(3, 4) == 7 @allure.story('02测试负数正数相加') def test_second(self): assert add(-3, 4) == 1 @allure.story('03测试正数负数相加') def test_three(self): assert add(3, -4) == -1 @allure.story('04测试两个负数相加') def test_four(self): assert add(-3, -4) == -7
运行:blog
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 .... [100%] =========================================================================== 4 passed in 0.06s ===========================================================================
报告:
你也能够经过 --allure-features 和 --allure-stories 选择指定具体功能和故事运行,多个以逗号分隔。
E:\workspace-py\Pytest>pytest test_sample.py --allure-stories 01测试两个正数相加 ========================================================================== 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 . [100%] =========================================================================== 1 passed in 0.02s ===========================================================================
若是你想对每一个测试调用进行很是详细的逐步说明,能够经过 @allure.step 装饰器来实现(固件一样支持)。
该装饰器会将方法或函数的调用与提供的参数一块儿添加到报表中,而且能够包含一条描述行,该行支持位置和关键字参数。
示例:
# test_sample.py import pytest import allure @allure.step('两数相加:{0} + {y}') def add(x, y): r = x + y print_res(r) return r @allure.step def print_res(r): print('计算结果:', r) class TestLearning: data = [ [3, 4, 7], [-3, 4, 1], [3, -4, -1], [-3, -4, -7], ] @pytest.mark.parametrize("data", data) def test_add(self, data): assert add(data[0], data[1]) == data[2]
报告:
若是你想让测试标题更具可读性,可使用 @allure.title 装饰器,该装饰器支持参数的占位符并支持动态替换。
示例:
# test_sample.py import pytest import allure def add(x, y): return x + y class TestLearning: data = [ [3, 4, 7], [-3, 4, 1], [3, -4, -1], [-3, -4, -7], ] @allure.title("测试用例-{data}") @pytest.mark.parametrize("data", data) def test_add(self, data): assert add(data[0], data[1]) == data[2]
若是你想添加测试的详细说明,能够经过添加测试方法描述信息,也可使用装饰器 @allure.description 和 @allure.description_html 。
示例:
# test_sample.py import allure # 被测功能 def add(x, y): return x + y # 测试类 class TestLearning: @allure.description('测试正数相加') def test_first(self): assert add(3, 4) == 7 @allure.description_html('<h1> 测试负数相加 </h1>') def test_second(self): """你也能够在这里添加用例的描述信息,可是会被allure装饰器覆盖""" assert add(-3, -4) == -7
报告:
若是你想在报告中显示不一样类型的附件,能够经过如下两种方式来实现:
allure.attach(body, name, attachment_type, extension)
allure.attach.file(source, name, attachment_type, extension)
示例:
import allure
def test_multiple_attachments(): allure.attach.file(r'C:\Users\Public\Pictures\Sample Pictures\Koala.jpg', attachment_type=allure.attachment_type.JPG) allure.attach('<head></head><body> 测试页面 </body>', 'Attach with HTML type', allure.attachment_type.HTML)
报告:
若是你想关联缺陷追踪系统或测试管理系统,你可使用装饰器 @allure.link、@allure.issue、@allure.testcase。
示例:
import allure @allure.link('http://www.baidu.com') def test_with_named_link(): pass @allure.issue('101', '缺陷问题描述') def test_with_issue_link(): pass @allure.testcase('http://this.testcase.com', '测试用例标题') def test_with_testcase_link(): pass
报告:
注意:
@allure.issue 将提供带有小错误图标的连接,该描述符将测试用例ID做为输入参数,以将其与提供的问题连接类型的连接模板一块儿使用。
连接模板在 --allure-link-patternPytest 的配置选项中指定。连接模板和类型必须使用冒号指定:
pytest test_sample.py --alluredir=report --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}
做者:Leozhanggg
出处:http://www.javashuo.com/article/p-gfwgfnud-nw.html
本文版权归做者和博客园共有,欢迎转载,但未经做者赞成必须保留此段声明,且在文章页面明显位置给出原文链接,不然保留追究法律责任的权利。