--- id: kgb version: "7.3" license: MIT license_treatment: permissive maintenance: active --- # kgb — Utilities for spying on function calls in unit tests. License: permissive · Maintenance: active · Downloads: 9.4M/mo ## 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 above — 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 pip install kgb uv add kgb 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_current - Install friction: low - Maintenance: active - Downloads: 9.4M/month (top 5,000 on PyPI) - Known vulnerabilities: none known ## Tags function call spies for testing, mock function calls and verify, intercept and record function calls, unit test spy library, pytest spy on functions, unittest function call tracking, testing, spies, unittest [View on SkillFed](https://skillfed.io/packages/kgb) · [View on PyPI](https://pypi.org/project/kgb/)