Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 在函数装饰器中使用pytestfixture_Python 2.7_Pytest - Fatal编程技术网

Python 2.7 在函数装饰器中使用pytestfixture

Python 2.7 在函数装饰器中使用pytestfixture,python-2.7,pytest,Python 2.7,Pytest,我想为我的测试函数构建一个具有多种用途的装饰器。其中之一是帮助向生成的junitxml添加属性 我知道这个名为record\u属性的内置pytest就是这样做的。我如何在我的装饰器内使用这个固定装置 def my_decorator(arg1): def test_decorator(func): def func_wrapper(): # hopefully somehow use record_property with arg1 here

我想为我的测试函数构建一个具有多种用途的装饰器。其中之一是帮助向生成的
junitxml
添加属性

我知道这个名为
record\u属性
的内置pytest就是这样做的。我如何在我的装饰器内使用这个固定装置

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
创建一个自定义标记并在decorator中调用它,但是我有很多
conftest.py
文件,我不能单独管理所有这些文件,因此无法强制执行

最后,尝试将该装置直接导入到我的decorator模块中,然后使用它会导致一个错误-因此,这也是不可能的


感谢您的帮助

虽然有点晚了,但我在我们的代码库中遇到了同样的问题。我可以找到一个解决方案,但它是相当黑客,所以我不会保证它的作品与旧版本或将在未来盛行

因此,我问是否有更好的解决办法。您可以在这里查看:

其基本思想是注册经过修饰的测试函数,然后诱使pytest认为它们需要参数列表中的fixture:

类寄存器测试数据:
#全局测试数据注册表
testdata_identifier_map={}#Dict[str,List[str]]
定义初始化(self,testdata\u标识符,direct\u import=True):
self.testdata\u标识符=testdata\u标识符
self.direct\u import=直接导入
self.\u始终\u通过\u我的\u导入\u夹具=False
定义调用(self,func):
如果RegisterTestData.testdata\u标识符\u映射中的函数名\u\u
RegisterTestData.testdata\u标识符\u映射[func.\u\u名称\u]。追加(self.testdata\u标识符)
其他:
RegisterTestData.testdata\u标识符\u映射[func.\u名称\u]=[self.testdata\u标识符]
#我们需要知道我们是装饰了原来的功能,还是已经装饰好了
#用另一个RegisterTestData装饰器装饰。这是必要的
#确定是否需要传递直接导入装置
如果getattr(func,“\u用\u寄存器\u测试数据装饰”,False):
self.\u始终\u通过\u我的\u导入\u夹具=真
setattr(func,“\u用\u寄存器\u测试数据”修饰,真)
@functools.wrapps(func)
@pytest.mark.usefixtures(“my_import_fixture”)#将fixture注册到测试中,以防它没有作为参数
def包装(*args:Any,my_import_fixture,**kwargs:Any):
#由于包装的签名,我的\u导入\u夹具不是零件
#传递给装饰函数的KWARG的。万一
#装饰功能的签名中有my_import_fixture,我们需要打包
#它回到**kwargs。这对年轻人来说总是尤其如此
#即使修饰函数没有
#我的进口夹具在其签名中
如果是自己,请始终通过我的进口夹具或任何(
签名(func.parameters.values()中p的p.name中的“hana_import”
):
kwargs[“hana_导入”]=hana_导入
如果self.direct\u导入:
my_import_fixture.import_all()
返回函数(*args,**kwargs)
返回包装器
def pytest_collection_modifyitems(配置:配置,项:列表[项])->无:
对于项目中的项目:
如果RegisterTestData.testdata_标识符_映射中的item.name和“我的导入装置”不在item中。\u fixtureinfo.argname:
#欺骗pytest使其认为my_import_fixture是原始函数参数列表的一部分
#仅适用于装饰器中的@pytest.mark.usefixtures(“my_import_fixture”)
item.\u fixtureinfo.argnames=item.\u fixtureinfo.argnames+(“我的\u导入\u夹具”,)