Schema-aware automated testing for FastAPI applications
Project description
Here’s the README in proper Markdown format ready to save as README.md:
# FastAPI Test Automation Library
A production-ready **CI/CD and batch testing framework** for FastAPI projects. This library automatically scans your FastAPI project, detects endpoints and Pydantic models, generates payloads, runs tests, validates responses, and generates detailed reports.
It is designed to simplify automated API testing and improve quality assurance workflows for FastAPI applications.
---
## Features
- **Automatic Project Scanning**: Detects FastAPI apps and routers across your project.
- **Endpoint Extraction**: Automatically extracts all registered endpoints.
- **Pydantic Model Extraction**: Extracts request and response models for payload validation.
- **Payload Generation**: Generates test payloads for POST, PUT, PATCH requests.
- **Automated Testing**: Runs single, batch, or CI/CD style tests with retries and timeouts.
- **Validation**:
- HTTP status code validation
- Response schema validation against Pydantic models
- **Authentication Support**: Supports JWT, API keys, and custom headers.
- **Reporting**: Generates JSON reports and prints a terminal summary.
- **CLI Integration**: Run tests easily from the command line.
---
## Installation
Install the library in your Python environment:
```bash
pip install fastapptest
Ensure your FastAPI project is accessible and all dependencies are installed.
CLI Usage
CI/CD Testing
Run full CI/CD style tests for your FastAPI project:
ci_test <project_root> [--output <output_dir>]
Arguments:
project_root: Root directory of your FastAPI project.--output: Directory to save CI/CD reports (default:ci_reports).
Example:
ci_test E:/Projects/my_fastapi_app --output ci_reports
This will:
- Scan your project for Python files.
- Detect FastAPI apps and routers.
- Extract endpoints and Pydantic models.
- Generate payloads for testing.
- Run automated tests.
- Validate responses and status codes.
- Generate a detailed JSON report and print a summary.
Batch Testing
Run tests using a JSON batch payload file:
batch_test <project_root> <batch_file> [--output <output_file>] [--auth <auth_json>]
Arguments:
project_root: Root directory of your FastAPI project.batch_file: JSON file containing batch payloads.--output: Path to save the batch test report (default:batch_reports/batch_test_report.json).--auth: Optional JSON string for authentication headers, e.g.,{"Authorization": "Bearer <token>"}.
Example:
batch_test E:/Projects/my_fastapi_app batch_payload.json --output batch_reports/report.json --auth '{"Authorization": "Bearer <token>"}'
Batch File Format:
[
{
"path": "/users",
"method": "POST",
"body": {"username": "test", "email": "test@example.com"}
},
{
"path": "/users/{id}",
"method": "GET",
"path_params": {"id": 1}
}
]
Library Usage
You can also use the library programmatically:
CI/CD Runner
from fastapptest.cli.ci_runner import run_ci_cd_tests
run_ci_cd_tests(
project_root="E:/Projects/my_fastapi_app",
output_dir="ci_reports",
auth={"Authorization": "Bearer <token>"},
fail_fast=True
)
Batch Runner
from fastapptest.cli.batch_runner import run_batch_tests
run_batch_tests(
project_root="E:/Projects/my_fastapi_app",
batch_file="batch_payload.json",
output_file="batch_reports/report.json",
auth={"Authorization": "Bearer <token>"}
)
Authentication
Supports common authentication methods:
-
Bearer Token (JWT) Add in auth dict:
{"Authorization": "Bearer <token>"} -
API Key Add in auth dict:
{"X-API-KEY": "<your-api-key>"}
Auth headers will be automatically added to all requests.
Configuration Options
CI/CD Runner
| Parameter | Type | Default | Description |
|---|---|---|---|
project_root |
str | None | Root directory of the FastAPI project |
output_dir |
str | ci_reports |
Directory to save CI/CD JSON report |
auth |
dict | None | Optional auth headers (JWT/API keys) |
fail_fast |
bool | False | Stop execution on first test failure |
Batch Runner
| Parameter | Type | Default | Description |
|---|---|---|---|
project_root |
str | None | Root directory of the FastAPI project |
batch_file |
str | None | JSON file containing batch payloads |
output_file |
str | batch_reports/batch_test_report.json |
Path to save JSON batch report |
auth |
dict | None | Optional auth headers (JWT/API keys) |
Reports
-
JSON Reports: Contain full details of each request:
status_codestatus_validschema_validresponseerror
-
Terminal Summary: Quick overview of tests with pass/fail indicators.
Recommended Workflow
- Install the library in your project environment.
- Create batch payloads for your endpoints.
- Run
ci_testduring CI/CD pipeline for automated validation. - Use
batch_testfor targeted testing with predefined payloads. - Review JSON reports and fix failing endpoints.
- Integrate into your GitHub Actions, Jenkins, or GitLab CI pipeline for automated testing.
Dependencies
- Python 3.9+
- FastAPI
- requests
- Standard Python libraries (
pathlib,json,typing)
License
This library is released under the MIT License.
---
If you want, I can also **create a ready-to-go `docs/` folder** with:
- Example payloads
- CI/CD pipeline YAML snippets (GitHub Actions, GitLab, Jenkins)
- Full usage guide
This will make your repository **professional and production-ready**.
Do you want me to create that next?
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapptest-2.0.0.tar.gz.
File metadata
- Download URL: fastapptest-2.0.0.tar.gz
- Upload date:
- Size: 28.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c30c3a95051e1c84d0d47d0bc78b5f94288ce366322cc3e7225b09d696f37e09
|
|
| MD5 |
d26aa5a94f577dc3dcf4df19d0f8b91b
|
|
| BLAKE2b-256 |
46a358abbc5721db8f334745cbe55f405f11b18c21f89e7a36fc12fc6705a360
|
File details
Details for the file fastapptest-2.0.0-py3-none-any.whl.
File metadata
- Download URL: fastapptest-2.0.0-py3-none-any.whl
- Upload date:
- Size: 42.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
115b14d250a6f9b83430dc20f11a0a495ded6b00ae2cb843f3bd8ac2a723356b
|
|
| MD5 |
47107ab9f40bf351127540d5d2cbe83d
|
|
| BLAKE2b-256 |
ea03172c83d536536cb812cfe84fe115ff853c854752fbc895a11c33ca03f3b9
|