This document explains how to run and extend an automated test suite for the Password Generator API built with FastAPI
This document explains how to run and extend an automated test suite for the Password Generator API built with FastAPI.
The tutorial for the Password Generator API built with FastAPI can be found here.
This article assumes you have already created the Password Generator API application found here.
Make sure you have the following installed in your development environment:
pytest library httpx library (used internally by FastAPI TestClient) Install pytest and httpx (if not already):
pip install pytest httpx
test_password_api.py
TestClient for simulating HTTP requests without starting
the
server. /generate-password endpoint with query parameters via
test_get_generate_password
/generate-password endpoint with JSON payload via
test_post_generate_password
Run all tests with:
pytest test_password_api.py
Use additional flags for better output:
pytest -s -v test_password_api.py
-s: Shows print() output during tests (useful for debugging). -v: Verbose mode; lists each test and its result.test_get_generate_password functionSends requests with different parameters to the GET /generate-password endpoint and validates
the
results.
import pytest
from fastapi.testclient import TestClient
from main import app # Your FastAPI application
client = TestClient(app)
@pytest.mark.parametrize("length, use_uppercase, use_numbers, use_special_chars", [
(12, True, True, True),
(4, False, False, False), # minimal length, only lowercase
(128, True, False, False), # max length, uppercase + lowercase
(20, False, True, True),
(15, True, False, True),
])
def test_get_generate_password(length, use_uppercase, use_numbers, use_special_chars):
params = {
"length": length,
"use_uppercase": str(use_uppercase).lower(),
"use_numbers": str(use_numbers).lower(),
"use_special_chars": str(use_special_chars).lower(),
}
response = client.get("/generate-password", params=params)
print(f"GET Request URL: {response.request.url}")
print(f"GET Response JSON: {response.json()}")
assert response.status_code == 200
data = response.json()
assert "password" in data
password = data["password"]
assert isinstance(password, str)
assert len(password) == length
test_post_generate_password functionSends JSON payloads with different parameters to the POST /generate-password endpoint and
validates
the results.
@pytest.mark.parametrize("payload", [
{"length": 12, "use_uppercase": True, "use_numbers": True, "use_special_chars": True},
{"length": 4, "use_uppercase": False, "use_numbers": False, "use_special_chars": False},
{"length": 128, "use_uppercase": True, "use_numbers": False, "use_special_chars": False},
{"length": 20, "use_uppercase": False, "use_numbers": True, "use_special_chars": True},
{"length": 15, "use_uppercase": True, "use_numbers": False, "use_special_chars": True},
])
def test_post_generate_password(payload):
response = client.post("/generate-password", json=payload)
print(f"POST Request URL: {response.request.url}")
print(f"POST Request Body: {payload}")
print(f"POST Response JSON: {response.json()}")
assert response.status_code == 200
data = response.json()
assert "password" in data
password = data["password"]
assert isinstance(password, str)
assert len(password) == payload["length"]
@pytest.mark.parametrize to easily test more combinations. test_ prefixed functions for different cases. client.get() and client.post() to simulate API requests. Example:
def test_invalid_length():
response = client.get("/generate-password", params={"length": 3}) # below minimum allowed
assert response.status_code == 422 # Expect validation error
print() statements inside tests to output details. -s flag to see prints: pytest -s -v test_password_api.py
-v) to see detailed test results./generate-password endpoints behave correctly for
various inputs. -s and -v flags for best experience.