前言
由于allure描述装饰器过于臃肿,不方便看起来不美观。所以决定将其封装成一个装饰器。
可以通过一个装饰器来封装 Allure 的装饰器,然后通过一个函数来实现一行代码注入需要的内容。例如:
import allure
def allure_decorator(title=None, description=None, feature=None, story=None, issue=None, link=None):
def decorator(func):
if title:
allure.title(title)
if description:
allure.description(description)
if feature:
allure.feature(feature)
if story:
allure.story(story)
if issue:
allure.issue(issue)
if link:
allure.link(link[0], link[1])
return func
return decorator
这里我们定义了一个 allure_decorator
装饰器,它接受多个参数,包括测试用例的标题、描述、功能模块、用户故事、关联缺陷和链接。这些参数可以为空,表示不需要注入对应的内容。
然后我们定义了一个内部装饰器 decorator
,它接受一个测试用例函数作为参数,并根据传入的参数,使用 Allure 的装饰器来注入对应的内容。
最后,我们可以通过一个函数来使用这个装饰器,例如:
@allure_decorator(title="测试用例1", description="这是一个测试用例", feature="登录功能", story="用户登录", issue="BUG-001", link=("http://www.baidu.com", "百度一下")) def test_login(): # 测试用例代码 pass