Skip to content

Commit

Permalink
Merge pull request sanic-org#1894 from huge-success/test_mode
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ashleysommer authored Jul 15, 2020
2 parents 9f2818e + 521ae7f commit 5ee8ee7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/sanic/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,36 @@ More information about
the available arguments to `httpx` can be found
[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
-------------------
Expand Down
1 change: 1 addition & 0 deletions sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def __init__(
self.named_response_middleware = {}
# Register alternative method names
self.go_fast = self.run
self.test_mode = False

@property
def loop(self):
Expand Down
16 changes: 16 additions & 0 deletions sanic/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ def __init__(self, app, port=PORT, host=HOST):
self.port = port
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):
return httpx.AsyncClient(verify=False)

Expand Down Expand Up @@ -209,6 +217,14 @@ def __init__(
def _collect_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)

async def request(self, method, url, gather_request=True, *args, **kwargs):
Expand Down
25 changes: 25 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,28 @@ def handler(request):
def test_app_name_required():
with pytest.deprecated_call():
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

0 comments on commit 5ee8ee7

Please sign in to comment.