【发布时间】:2018-12-18 20:43:13
【问题描述】:
我想为我的测试函数构建一个装饰器,它有多种用途。其中之一是帮助向生成的junitxml 添加属性。
我知道有一个名为 record_property 的内置 pytest fixture 正是这样做的。如何在我的装饰器中使用这个夹具?
def my_decorator(arg1):
def test_decorator(func):
def func_wrapper():
# hopefully somehow use record_property with arg1 here
# do some other logic here
return func()
return func_wrapper
return test_decorator
@my_decorator('some_argument')
def test_this():
pass # do actual assertions etc.
我知道我可以将夹具直接传递给每个测试函数并在测试中使用它,但是我有很多测试,这样做似乎非常多余。
另外,我知道我可以使用 conftest.py 并创建一个自定义标记并在装饰器中调用它,但是我有很多 conftest.py 文件,我不能单独管理所有这些文件,所以我不能强制执行。
最后,尝试将夹具直接导入我的装饰器模块,然后使用它会导致错误 - 所以这也是不行的。
感谢您的帮助
【问题讨论】:
标签: python-2.7 pytest