Skip to main content

Observe what's missing. Haki scans your FastAPI project and tells you which endpoints have no test written for them.

Project description

haki

Haki is a fast, zero-runtime static analysis tool that scans your FastAPI project and tells you which endpoints have no test written for them, before you run a single line of code.

Built in Rust, exposed as a Python package.

Why

pytest --cov tells you coverage after running your entire test suite. That requires a running server, dependencies, databases, all of it.

Haki answers a simpler question earlier: did someone actually write a test function for this endpoint? It runs in milliseconds, needs no server, and is perfect as a pre-commit hook or CI lint step on every PR. Please keep in my mind that i am a student and know for sure that this may not be the best code and could've been most likely better in the eyes of alot of very good programmers, so please make a issue! Hopefully i can learn alot and improve this project.

Installation

pip install haki

Usage

As a Python package

import haki
import json

results = json.loads(haki.check_endpoints("/path/to/your/fastapi/project"))

for ep in results:
    status = "✓" if ep["has_test"] else "✗"
    print(f"{status} {ep['method']} {ep['path']}{ep['name']}")

Output

[
  {
    "name": "get_workflow",
    "path": "/{workflow_id}",
    "method": "Get",
    "file_path": "/project/routers/workflows.py",
    "has_test": true
  },
  {
    "name": "delete_workflow",
    "path": "/{workflow_id}",
    "method": "Delete",
    "file_path": "/project/routers/workflows.py",
    "has_test": false
  }
]

In CI (fail the pipeline if any endpoint is untested)

import haki
import json
import sys

results = json.loads(haki.check_endpoints("."))
missing = [ep for ep in results if not ep["has_test"]]

if missing:
    print("Missing tests for:")
    for ep in missing:
        print(f"  ✗ {ep['method']} {ep['path']} ({ep['name']})")
    sys.exit(1)

How it works

Haki uses tree-sitter to parse your Python source files into AST trees without executing any code. It then:

  1. Scans all .py files for FastAPI route decorators (@app.get, @router.post etc.) and extracts the function name, path, and HTTP method
  2. Scans your tests/ or test/ directory for functions named test_*
  3. Matches each endpoint to a test by prefix — get_workflow is considered tested if any function named test_get_workflow* exists

Naming convention

Haki follows pytest convention. Your test functions must be named test_<endpoint_function_name>. Variants like test_get_workflow_returns_404 or test_get_workflow_unauthorised are all matched correctly.

# endpoint
@router.get("/{workflow_id}")
async def get_workflow(workflow_id: str):
    ...

# all of these count as a test for get_workflow
def test_get_workflow(): ...
def test_get_workflow_not_found(): ...
def test_get_workflow_returns_401(): ...

Requirements

  • Python 3.9+
  • FastAPI project using decorator-based routing (@app.get, @router.post etc.)
  • Tests follow test_<function_name> naming convention in a tests/ or test/ directory

Limitations

  • FastAPI only (Flask, Django not supported yet)
  • Matches by function name convention, not by which routes are actually called in tests
  • Does not replace pytest --cov — use both
  • Plugin system will be added in the future and custom naming convention

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

haki-0.1.0.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

haki-0.1.0-cp314-cp314-win_amd64.whl (2.0 MB view details)

Uploaded CPython 3.14Windows x86-64

File details

Details for the file haki-0.1.0.tar.gz.

File metadata

  • Download URL: haki-0.1.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for haki-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0a35753ed9f253af6402433f2aa42f4b16f79d0a0fb582bcd8bfe08b9102ccc0
MD5 de50c3bf866aa5d293aa42407604aaed
BLAKE2b-256 c40d54e5a0e35b50a7a4c15f3744a231266b34e09e7f421f6c31d7e6a9162581

See more details on using hashes here.

File details

Details for the file haki-0.1.0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: haki-0.1.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 2.0 MB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for haki-0.1.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 f1330b3fbba67ea56425ba8d424499c94d988be6326aee8ed77c450a69ac9cad
MD5 a8acbaabff10b53c4ac46ec39033a332
BLAKE2b-256 05478aa199c4bd6026f7248c19938201ecf21ff61adad5aa31da8a6d093ddc01

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page