Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.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 无法在作用域为类的装置中运行addfinalizer函数_Python_Testing_Pytest - Fatal编程技术网

Python 无法在作用域为类的装置中运行addfinalizer函数

Python 无法在作用域为类的装置中运行addfinalizer函数,python,testing,pytest,Python,Testing,Pytest,以下是我的测试代码: 导入字符串 导入pytest 导入临时文件 导入操作系统 @pytest.fixturescope='class' def临时文件请求: tmp_cfg_fd,tmp_cfg_file_path=tempfile.mkstemp os.closetmp\u cfg\u fd def REMOVETEMP文件: 打印删除%r%tmp\u cfg\u文件路径 os.removemp_cfg_文件路径 request.addfinalizerRemoveTempFile 返回tm

以下是我的测试代码:

导入字符串 导入pytest 导入临时文件 导入操作系统 @pytest.fixturescope='class' def临时文件请求: tmp_cfg_fd,tmp_cfg_file_path=tempfile.mkstemp os.closetmp\u cfg\u fd def REMOVETEMP文件: 打印删除%r%tmp\u cfg\u文件路径 os.removemp_cfg_文件路径 request.addfinalizerRemoveTempFile 返回tmp\u cfg\u文件路径 @pytest.mark.usefixturesTempFile 课堂测试: def test1self: 我在测试一 def test2self: 我在测试2 当我对其运行py.test时,会出现以下错误:

test_4.py:17: in <module>
    @pytest.mark.usefixtures(TempFile)
test_4.py:14: in TempFile
    request.addfinalizer(RemoveTempFile)
E   AttributeError: class Test has no attribute 'addfinalizer'
当fixture具有scope='class',则测试类无法运行addfinalizer

但是如果fixture具有scope='function',并且我在test1和test2函数中分别调用TempFile fixture,那么addfinalizer将正常运行


如何让addfinalizer与scope='class'一起运行?

如果将标记更改为:

@pytest.mark.usefixtures('TempFile')
它将正常工作