Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation:
The SanicASGITestClient class provides only a client that lifts a new instance of the test application for each request.
This is very expensive and time-consuming. The application I'm trying to migrate to the current version of sanic-testing has 1500+ tests that pass on the old testing engine in 7 minutes. With the new SanicASGITestClient, passing 30 percent of the tests takes about 8 minutes.
In addition to the increased passing time, at 31 percent of the tests, there is a recursion error somewhere deep in ast.py. I couldn't figure out the cause of this behavior, and with the current version of the client, it's simply impossible to run the tests to completion.
Proposal to solve the problem:
The ability to use the SanicASGITestClient as a context manager or manually control its execution through the run() and stop() methods has been added to the class.
This allows for the repeated use of the running test server for multiple requests to the test server. With the right configuration of the test environment, the test server can be used within the scope="session."
The running server also executes all lifecycle hooks of the provided real application instance, allowing you to start the server and test some parts of the application more isolated, while still having the ability to access the real context of the application and everything initialized in that context in the tests.
When running 100 consecutive API requests in one test using the old client in my application, the speed is about 44 seconds. With the use of the new client, the speed increases to 14 seconds.
Full backward compatibility is maintained; the client can be used as before, preserving the old API and behavior completely.