Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test mode #1894

Merged
merged 19 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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