skillfed

kgb

Utilities for spying on function calls in unit tests.

kgb v7.3 9.4M downloads/30d#1,536 on PyPI53
Permissive license MIT Active released

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 kgb

uv

uv add kgb

poetry

poetry add kgb

Installing 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

Development Status :: 5 - Production/StableEnvironment :: Other EnvironmentFramework :: PytestIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 2Programming Language :: Python :: 2.7Programming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.6Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Topic :: Software DevelopmentTopic :: Software Development :: Libraries :: Python ModulesTopic :: Software Development :: Testing

Tags

function call spies for testingmock function calls and verifyintercept and record function callsunit test spy librarypytest spy on functionsunittest function call tracking
testingspiesunittest

More Software Development packages