builder555 / fastapi-starter-tdd

A primer for creating FastAPI application with tests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FastAPI primer

This FastAPI starter template provides a simple and efficient way to begin a new web project, including preconfigured setup for test-driven development (TDD).

Features:

  • unit/e2e and mutation testing
  • CORS preconfigured
  • sample allowed-origins
  • example routing in submodule
  • black code formatter

Usage

Requirements

  • python3
  • pipenv

Install

git clone https://github.com/builder555/fastapi-starter-tdd
cd fastapi-starter-tdd
pipenv install --dev

Run tests

# unit/e2e test
pipenv run test

# app/tests/test_main.py .
# app/tests/test_module.py .


# mutation test
pipenv run mutation
# 1. Running tests without mutations
# β ‹ Running...Done
#
# 2. Checking mutants
# β Ή 21/21  πŸŽ‰ 12  ⏰ 0  πŸ€” 0  πŸ™ 9  πŸ”‡ 0

# see results
pipenv run mutresult

Start a dev server

pipenv run start:dev

Navigate to http://localhost:8000/docs to see the Swagger UI or http://localhost:8000/redoc to see ReDoc.

To monitor tests while developing, run

pipenv run testmon

Structure

β”œβ”€β”€ app #<-- all your modules live here
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ module.py #<-- example module
β”‚   └── tests #<-- unit/integration tests are here. these files are excluded from mutation
β”‚       β”œβ”€β”€ __init__.py
β”‚       β”œβ”€β”€ test_main.py
β”‚       └── test_module.py
└── main.py #<-- main application code

Running in a Docker container

docker build -t fastapi:latest . && docker run --rm -d -p 8000:8000 fastapi:latest

Navigate to http://localhost:8000/docs to see the Swagger UI or http://localhost:8000/redoc to see ReDoc.

About

A primer for creating FastAPI application with tests


Languages

Language:Python 87.6%Language:Dockerfile 12.4%