-
-
Notifications
You must be signed in to change notification settings - Fork 203
Testing with teardown_request #430
Unanswered
johansigfrids
asked this question in
Q&A
-
How do I do a test where I make use of the test_client to make a request, but also get the teardown_request code to run? I am trying to make an e2e test to make sure all the request lifecycle style hooks are called, and I have one in teardown_request that is not running in request context. I assume it has something to do with the teardown_request running after the request is done, so it ends up running after the test code?
I've tired to create a minimal reproducible example:
import pytest from quart import Quart import asyncio @pytest.fixture def app(): app = Quart(__name__) app.teardown_request_called_flag = False @app.before_request def minimal_before(): print("MINIMAL_APP: Before request") @app.route("/") async def index(): print("MINIMAL_APP: Handling route /") return "OK" @app.after_request def minimal_after(response): print("MINIMAL_APP: After request") return response @app.teardown_request def minimal_teardown_request(exception): print("MINIMAL_APP: Teardown request") app.teardown_request_called_flag = True return app @pytest.mark.asyncio async def test_minimal_app_teardowns_run(app: Quart): async with app.test_client() as client: print("MINIMAL_TEST: About to make GET request") response = await client.get("/") print(f"MINIMAL_TEST: GET request completed, status: {response.status_code}") assert response.status_code == 200 await asyncio.sleep(0.5) print("MINIMAL_TEST: Sleep after request finished") print("MINIMAL_TEST: Test client context exited.") assert app.teardown_request_called_flag, "Teardown request was not called"
The captured print statements are:
------------------------------------------------ Captured stdout call ------------------------------------------------
MINIMAL_TEST: About to make GET request
MINIMAL_APP: Before request
MINIMAL_APP: Handling route /
MINIMAL_APP: After request
MINIMAL_TEST: GET request completed, status: 200
MINIMAL_TEST: Sleep after request finished
MINIMAL_TEST: Test client context exited.
Is there something I can do to trigger the teardown_request to run?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment