Pytest - 将信息传递给夹具

Pytest - Pass information to fixture

我的烧瓶服务器在一些操作后发回文件名。

return make_response(json.dumps({"file_id": uuid_name}), 200)

此文件在云端生成并发送。这些文件的负载是在测试期间生成的。

我希望能够在测试后删除这些文件。我可以使用固定装置来做到这一点。

但是,fixture 函数无法访问此文件名,以便将其删除。

我可以从服务器的响应中获取文件名,返回到pytest函数。

有没有办法将此信息传递回夹具,以便在测试结束后删除那里的文件?

编辑:换句话说,fixture 如何访问服务器的响应?因为要删除的文件的信息存在于服务器的响应中。 主要的 pytest 函数可以访问此响应:

response = client.get('/getdata', query_string={'id': filename}) 
response_data = json.loads(response.data)

并且可以看到fileid。但是夹具无法访问此信息。有没有办法将此信息传递给灯具?

编辑 2:一些相关代码

@pytest.fixture(scope="function")
def cleanup_for_testing(request):
  yield # Run the tests
  #DELETION CODE GOES HERE

@pytest.mark.parametrize("radius, length, ang, status, error_message_template", [
              ('10', '10', '10', 200, 'null')
           ])


def test_footparam(cleanup_for_testing, radius, length, ang, status, error_message_template):
   client = app.test_client()
   data = {"radius": radius, "length": length, "ang": ang}
   response = client.post('/generate', content_type='application/json', data=json.dumps(data))
   response_data = json.loads(response.data)
   if error_message_template == 'null':
      assert response.status_code == 200
   else:
      my_error_message = response_data["error_message"]
      assert (response.status_code == status) and (my_error_message == error_message_template)

我只想让 fixture 产生一个可变对象,然后通过编辑将数据传回给它:

import pytest

@pytest.fixture
def fixer():
    data = {}
    yield data
    print(f"Removing {data['fn'}")

def test_fn(fixer):
    fixer["fn"] = "/path/to/temp/file"
    print("Done testing")