Reputation: 3863
My flask server sends back the the name of a file after some operation.
return make_response(json.dumps({"file_id": uuid_name}), 200)
This file is generated and sent at the cloud. Loads of these files are generated during testing.
I would like to be able to delete these files after the tests. I can do that using fixtures.
However, the fixture function does not have access to this file name, in order to delete it.
I can get the file name from the response of the server, back to the pytest function.
Is there a way to pass this information back to the fixture, in order to delete the files there, after the tests have ended?
EDIT: In other words, how can the fixture get access to the response of the server? Because the information on the files to be deleted exist on the response of the server. The main pytest function has access to this response:
response = client.get('/getdata', query_string={'id': filename})
response_data = json.loads(response.data)
And can see the fileid. But the fixture does not have access to this information. Is there a way to pass this information to the fixture?
EDIT 2: Some relevant code
@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)
Upvotes: 1
Views: 677
Reputation: 5964
I would simply have the fixture yield a muteable object and pass the data back to it by editing that:
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")
Upvotes: 1