My Logo

Using PyTest Testing Suite for Password Generator API

This document explains how to run and extend an automated test suite for the Password Generator API built with FastAPI

Testing Suite for Password Generator API

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.


Prerequisites

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:

Install pytest and httpx (if not already):

pip install pytest httpx

Test Suite Overview


How to Run Tests

Run all tests with:

pytest test_password_api.py

Use additional flags for better output:

pytest -s -v test_password_api.py

What the Tests Do

test_get_generate_password function

Sends 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 function

Sends 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"]

How to Add More Tests

Example:

def test_invalid_length():
    response = client.get("/generate-password", params={"length": 3})  # below minimum allowed
    assert response.status_code == 422  # Expect validation error

Debugging Tests

pytest -s -v test_password_api.py

Summary