Python 无法在作用域为类的装置中运行addfinalizer函数
以下是我的测试代码: 导入字符串 导入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时,会出现以下错误: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
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')
它将正常工作