kgb
Utilities for spying on function calls in unit tests.
What it is and what it does
kgb is a spy library for Python unit tests that intercepts function calls without requiring you to write mock objects. When you spy on a function, kgb records every call—its arguments, return value, and any exceptions—and lets you query that history in your test assertions. You can configure a spy to call the original function, block it entirely, or reroute it to a fake function you provide.
The library integrates with unittest, pytest, nose, and nose2, and can be used as a mixin in test classes, a context manager for quick one-off spies, or through a SpyAgency object that manages multiple spies. It has no runtime dependencies and installs as a pure Python wheel, making it lightweight and easy to add to any test suite.
Use it for:
- Verify that a method was called with specific arguments without mocking the entire object
- Count how many times a function is invoked during a test to ensure it's not called more than expected
- Block a function call in tests (e.g., prevent side effects) while still recording that it was attempted
- Reroute a function to a fake implementation to test error handling or alternative behavior
- Track the sequence and details of multiple function calls across a test to verify interaction order
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
kgb intercepts and records function calls in unit tests, letting you verify how many times a function was called and with what arguments, while optionally blocking, rerouting, or allowing the original call to proceed.
Yes. kgb is a lightweight, actively maintained testing utility with no dependencies, permissive licensing, and a clean API for function call verification. It's well-suited for any Python test suite that needs spy functionality without the overhead of full mocking frameworks.
Install
kgb on PyPI
pip
pip install kgbuv
uv add kgbpoetry
poetry add kgbInstalling kgb
Before you install
Low friction: pure Python wheel with no runtime dependencies. Actively maintained with a recent release and active repository.
License in practice
MIT license is permissive; you can use kgb in commercial and proprietary projects with minimal restrictions.
Quickstart
pip install kgb
from kgb import SpyAgency
agency = SpyAgency()
agency.spy_on(obj.function)
obj.function()
print(obj.function.called) # True
print(obj.function.last_call.args)
Verify before relying
- Whether kgb works with Python 2.7 as documented (support statement says 2.7 and 3.6–3.11, but classifiers list 3.12–3.13)
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,>=2.7) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 246 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 9,403,872/month — #1,536 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: kgb-7.3-py2.py3-none-any.whl
Keywords: pytest, unit tests, spies
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
pytest-mockProvides a pytest fixture that wraps the mock…
permissive · top 1,000 on PyPI
snoopsnoop adds decorator-based and context-manager…
permissive · top 15,000 on PyPI
pytest-loggerPytest-logger is a pytest plugin that…
permissive · top 15,000 on PyPI
seeq-spySeeq-spy is a Python interface for interacting…
unclear · top 15,000 on PyPI
flexmockflexmock creates mock, stub, spy, and fake…
permissive · top 15,000 on PyPI
pyfakefspyfakefs mocks the Python file system modules…
permissive · top 5,000 on PyPI
pytest-subtestsAdds subTest() support to pytest, allowing…
permissive · top 5,000 on PyPI
pytest-fakerProvides pytest fixtures that integrate the…
permissive · top 15,000 on PyPI
wraptwrapt provides a Python module for building…
permissive · top 100 on PyPI
nose2nose2 is a test runner that extends Python's…
permissive · top 15,000 on PyPI