Writing tests for REST API endpoints
With everything in place, let’s create the test_login.py
file, where we’ll test the authentication routes:
(venv)$ touch tests/test_login.py
In the test file, we’ll start by importing the dependencies:
import httpx import pytest
Testing the sign-up route
The first endpoint we’ll be testing is the sign-up endpoint. We’ll be adding the pytest.mark.asyncio
decorator, which informs pytest
to treat this as an async test. Let’s define the function and the request payload:
@pytest.mark.asyncio async def test_sign_new_user(default_client: httpx.AsyncClient) -> None: payload = { "email": "testuser@packt.com", "password": "testpassword", }
Let’s define the request header and expected response:
...