Merge pull request #1894 from huge-success/test_mode
add a test_mode boolean variable to sanic `app` which is set to True when using Sanic TestClient or ASGIClient, and False all other times.
This commit is contained in:
commit
5ee8ee7b04
|
@ -58,6 +58,36 @@ More information about
|
||||||
the available arguments to `httpx` can be found
|
the available arguments to `httpx` can be found
|
||||||
[in the documentation for `httpx <https://www.encode.io/httpx/>`_.
|
[in the documentation for `httpx <https://www.encode.io/httpx/>`_.
|
||||||
|
|
||||||
|
Additionally, Sanic has an asynchronous testing client. The difference is that the async client will not stand up an
|
||||||
|
instance of your application, but will instead reach inside it using ASGI. All listeners and middleware are still
|
||||||
|
executed.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_index_returns_200():
|
||||||
|
request, response = await app.asgi_client.put('/')
|
||||||
|
assert response.status == 200
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
Whenever one of the test clients run, you can test your app instance to determine if it is in testing mode:
|
||||||
|
`app.test_mode`.
|
||||||
|
|
||||||
|
Additionally, Sanic has an asynchronous testing client. The difference is that the async client will not stand up an
|
||||||
|
instance of your application, but will instead reach inside it using ASGI. All listeners and middleware are still
|
||||||
|
executed.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_index_returns_200():
|
||||||
|
request, response = await app.asgi_client.put('/')
|
||||||
|
assert response.status == 200
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
Whenever one of the test clients run, you can test your app instance to determine if it is in testing mode:
|
||||||
|
`app.test_mode`.
|
||||||
|
|
||||||
|
|
||||||
Using a random port
|
Using a random port
|
||||||
-------------------
|
-------------------
|
||||||
|
|
|
@ -90,6 +90,7 @@ class Sanic:
|
||||||
self.named_response_middleware = {}
|
self.named_response_middleware = {}
|
||||||
# Register alternative method names
|
# Register alternative method names
|
||||||
self.go_fast = self.run
|
self.go_fast = self.run
|
||||||
|
self.test_mode = False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def loop(self):
|
def loop(self):
|
||||||
|
|
|
@ -22,6 +22,14 @@ class SanicTestClient:
|
||||||
self.port = port
|
self.port = port
|
||||||
self.host = host
|
self.host = host
|
||||||
|
|
||||||
|
@app.listener("after_server_start")
|
||||||
|
def _start_test_mode(sanic, *args, **kwargs):
|
||||||
|
sanic.test_mode = True
|
||||||
|
|
||||||
|
@app.listener("before_server_end")
|
||||||
|
def _end_test_mode(sanic, *args, **kwargs):
|
||||||
|
sanic.test_mode = False
|
||||||
|
|
||||||
def get_new_session(self):
|
def get_new_session(self):
|
||||||
return httpx.AsyncClient(verify=False)
|
return httpx.AsyncClient(verify=False)
|
||||||
|
|
||||||
|
@ -209,6 +217,14 @@ class SanicASGITestClient(httpx.AsyncClient):
|
||||||
def _collect_request(request):
|
def _collect_request(request):
|
||||||
self.last_request = request
|
self.last_request = request
|
||||||
|
|
||||||
|
@app.listener("after_server_start")
|
||||||
|
def _start_test_mode(sanic, *args, **kwargs):
|
||||||
|
sanic.test_mode = True
|
||||||
|
|
||||||
|
@app.listener("before_server_end")
|
||||||
|
def _end_test_mode(sanic, *args, **kwargs):
|
||||||
|
sanic.test_mode = False
|
||||||
|
|
||||||
app.request_middleware.appendleft(_collect_request)
|
app.request_middleware.appendleft(_collect_request)
|
||||||
|
|
||||||
async def request(self, method, url, gather_request=True, *args, **kwargs):
|
async def request(self, method, url, gather_request=True, *args, **kwargs):
|
||||||
|
|
|
@ -222,3 +222,28 @@ def test_handle_request_with_nested_sanic_exception(app, monkeypatch, caplog):
|
||||||
def test_app_name_required():
|
def test_app_name_required():
|
||||||
with pytest.deprecated_call():
|
with pytest.deprecated_call():
|
||||||
Sanic()
|
Sanic()
|
||||||
|
|
||||||
|
|
||||||
|
def test_app_has_test_mode_sync():
|
||||||
|
app = Sanic("test")
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
def handler(request):
|
||||||
|
assert request.app.test_mode
|
||||||
|
return text("test")
|
||||||
|
|
||||||
|
_, response = app.test_client.get("/")
|
||||||
|
assert response.status == 200
|
||||||
|
|
||||||
|
|
||||||
|
# @pytest.mark.asyncio
|
||||||
|
# async def test_app_has_test_mode_async():
|
||||||
|
# app = Sanic("test")
|
||||||
|
|
||||||
|
# @app.get("/")
|
||||||
|
# async def handler(request):
|
||||||
|
# assert request.app.test_mode
|
||||||
|
# return text("test")
|
||||||
|
|
||||||
|
# _, response = await app.asgi_client.get("/")
|
||||||
|
# assert response.status == 200
|
||||||
|
|
Loading…
Reference in New Issue
Block a user