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

docs: Fix testing code examples #3143

Closed
wants to merge 6 commits into from
Closed
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
44 changes: 35 additions & 9 deletions docs/usage/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,43 @@ We would then be able to rewrite our test like so:
.. tab-item:: Sync
:sync: sync

.. literalinclude:: /examples/testing/test_health_check_sync.py
.. code-block:: python
:caption: tests/test_health_check.py
:language: python

import pytest

from litestar.status_codes import HTTP_200_OK
from litestar.testing import TestClient

from app import app


def test_health_check_with_fixture(test_client: TestClient) -> None:
with test_client as client:
response = client.get("/health-check")
assert response.status_code == HTTP_200_OK
assert response.text == "healthy"


.. tab-item:: Async
:sync: async

.. literalinclude:: /examples/testing/test_health_check_async.py
.. code-block:: python
:caption: tests/test_health_check.py
:language: python

import pytest

from litestar.status_codes import HTTP_200_OK
from litestar.testing import AsyncTestClient

from app import app


async def test_health_check_with_fixture(test_client: AsyncTestClient) -> None:
async with test_client as client:
response = await client.get("/health-check")
assert response.status_code == HTTP_200_OK
assert response.text == "healthy"


Using sessions
Expand All @@ -141,7 +167,7 @@ across requests, then you might want to inject or inspect session data outside a

.. attention::

- The Session Middleware must be enabled in Litestar app provided to the TestClient to use sessions.
- The session middleware must be enabled in the Litestar app provided to the :class:`TestClient <.testing.TestClient>` to use sessions.
- If you are using the
:class:`ClientSideSessionBackend <litestar.middleware.session.client_side.ClientSideSessionBackend>` you need to
install the ``cryptography`` package. You can do so by installing ``litestar``:
Expand All @@ -162,7 +188,7 @@ across requests, then you might want to inject or inspect session data outside a

pipx install litestar[cryptography]

.. tab-item:: pdm
.. tab-item:: PDM

.. code-block:: bash
:caption: Using `PDM <https://pdm.fming.dev/>`_
Expand Down Expand Up @@ -229,7 +255,7 @@ an instance of Litestar and then a test client using it. There are multiple use
generic logic that is decoupled from a specific Litestar app, or when you want to test endpoints in isolation.

You can pass to this helper all the kwargs accepted by
the litestar constructor, with the ``route_handlers`` kwarg being **required**. Yet unlike the Litestar app, which
the Litestar constructor, with the ``route_handlers`` kwarg being **required**. Yet unlike the Litestar app, which
expects ``route_handlers`` to be a list, here you can also pass individual values.

For example, you can do this:
Expand Down Expand Up @@ -341,7 +367,7 @@ Using polyfactory
------------------------

`Polyfactory <https://github.com/litestar-org/polyfactory>`__ offers an easy
and powerful way to generate mock data from pydantic models and dataclasses.
and powerful way to generate mock data from Pydantic models and dataclasses.

Let's say we have an API that talks to an external service and retrieves some data:

Expand Down Expand Up @@ -401,7 +427,7 @@ We could test the ``/item`` route like so:

While we can define the test data manually, as is done in the above, this can be quite cumbersome. That's
where `polyfactory <https://github.com/litestar-org/polyfactory>`_ library comes in. It generates mock data for
pydantic models and dataclasses based on type annotations. With it, we could rewrite the above example like so:
Pydantic models and dataclasses based on type annotations. With it, we could rewrite the above example like so:


.. code-block:: python
Expand Down
Loading