# kgb

> Utilities for spying on function calls in unit tests.

- **URL**: https://www.freshcrate.ai/projects/kgb
- **Author**: pypi
- **Category**: Frameworks
- **Latest version**: `7.3` (2026-04-21)
- **License**: MIT
- **Source**: https://github.com/beanbaginc/kgb
- **Homepage**: https://pypi.org/project/kgb/
- **Language**: Python
- **GitHub**: 53 stars, 3 forks
- **Registry**: pypi (`kgb`)
- **Tags**: `pypi`, `pytest`, `spies`, `tests`, `unit`

## Description

===============================
kgb - Function spies for Python
===============================

Ever deal with a large test suite before, monkey patching functions to figure
out whether it was called as expected? It's a dirty job. If you're not careful,
you can make a mess of things. Leave behind evidence.

kgb's spies will take care of that little problem for you.


What are spies?
===============

Spies intercept and record calls to functions. They can report on how many times
a function was called and with what arguments. They can allow the function call
to go through as normal, to block it, or to reroute it to another function.

Spies are awesome.

(If you've used Jasmine_, you know this.)

Spies are like mocks, but better. You're not mocking the world. You're
replacing very specific function logic, or listening to functions without
altering them. (See the FAQ below.)


.. _Jasmine: https://jasmine.github.io/


What test platforms are supported?
==================================

Anything Python-based:

* unittest_
* pytest_
* nose_
* nose2_

You can even use it outside of unit tests as part of your application. If you
really want to. (Probably don't do that.)


.. _unittest: https://docs.python.org/3/library/unittest.html
.. _pytest: https://pytest.org
.. _nose: https://nose.readthedocs.io/en/latest/
.. _nose2: https://docs.nose2.io/en/latest/


Where is kgb used?
==================

* `liveswot-api <https://github.com/imranariffin/liveswot-api>`_ --
  REST API Backend for liveswot
* `phabricator-emails
  <https://github.com/mozilla-conduit/phabricator-emails>`_ --
  Mozilla's utilities for converting Phabricator feeds to e-mails
* `projector <https://github.com/brennie/projector>`_ --
  Takes the overhead out of managing repositories and development environments
* `ynab-sdk-python <https://github.com/andreroggeri/ynab-sdk-python>`_ --
  Python implementation of the YNAB API

Plus our own products:

* `Django Evolution <https://django-evolution.readthedocs.io/>`_ --
  An alternative approach to Django database migrations
* `Djblets <https://github.com/djblets/djblets/>`_ --
  An assortment of utilities and add-ons for managing large Django projects
* `Review Board <https://www.reviewboard.org/>`_ --
  Our open source, extensible code review product
* `RBCommons <https://rbcommons.com>`_ --
  Our hosted code review service
* `RBTools <https://www.reviewboard.org/downloads/rbtools/>`_ --
  Command line tools for Review Board
* `Power Pack <https://www.reviewboard.org/powerpack/>`_ --
  Document review, reports, and enterprise SCM integrations for Review Board
* `Review Bot <https://www.reviewboard.org/downloads/reviewbot/>`_ --
  Automated code review add-on for Review Board

If you use kgb, let us know and we'll add you!


Installing kgb
==============

Before you can use kgb, you need to install it. You can do this by typing::

    $ pip install kgb

kgb supports Python 2.7 and 3.6 through 3.11, both CPython and PyPy.


Spying for fun and profit
=========================

Spying is really easy. There are four main ways to initiate a spy.


1. Creating a SpyAgency
-----------------------

A SpyAgency manages all your spies. You can create as many or as few as you
want. Generally, you'll create one per unit test run. Then you'll call
``spy_on()``, passing in the function you want.

.. code-block:: python

    from kgb import SpyAgency


    def test_mind_control_device():
        mcd = MindControlDevice()
        agency = SpyAgency()
        agency.spy_on(mcd.assassinate, call_fake=give_hugs)


2. Mixing a SpyAgency into your tests
-------------------------------------

A ``SpyAgency`` can be mixed into your unittest_-based test suite, making
it super easy to spy all over the place, discretely, without resorting to a
separate agency. (We call this the "inside job.")

.. code-block:: python

    from kgb import SpyAgency


    # Using Python's unittest:
    class TopSecretTests(SpyAgency, unittest.TestCase):
        def test_weather_control(self):
            weather = WeatherControlDevice()
            self.spy_on(weather.start_raining)


    # Using pytest with the "spy_agency" fixture (kgb 7+):
    def test_weather_control(spy_agency):
        weather = WeatherControlDevice()
        spy_agency.spy_on(weather.start_raining)


3. Using a decorator
--------------------

If you're creating a spy that calls a fake function, you can simplify some
things by using the ``spy_for`` decorator:


.. code-block:: python

    from kgb import SpyAgency


    # Using Python's unittest:
    class TopSecretTests(SpyAgency, unittest.TestCase):
        def test_doomsday_device(self):
            dd = DoomsdayDevice()

            @self.spy_for(dd.kaboom)
            def _save_world(*args, **kwargs)
                print('Sprinkles and ponies!')

            # Give it your best shot, doomsday device.
            dd.kaboom()


    # Using pytest:
    def test_doomsday_device(spy_agency):
        dd = DoomsdayDevice()

## Recent releases

| Version | Date | Urgency | Changes |
| --- | --- | --- | --- |
| `7.3` | 2026-04-21 | Low | Imported from PyPI (7.3) |
| `release-7.2` | 2024-11-04 | Low | kgb 7.2 introduces new support for Python 3.13, fixing crashes and ensuring stability going forward.  Python 3.13 made changes to how functions were composed and executed, requiring changes in kgb. These were pretty minimal, and we don't expect any issues going forward.   # Installation  ```shell $ pip install kgb==7.2 ```  # Learn More  * [kgb 7.2 release notes](https://github.com/beanbaginc/kgb/blob/master/NEWS.rst#kgb-72-3-november-2024) * [Documentation](https://github.com/bean |
| `release-7.2` | 2024-11-04 | Low | kgb 7.2 introduces new support for Python 3.13, fixing crashes and ensuring stability going forward.  Python 3.13 made changes to how functions were composed and executed, requiring changes in kgb. These were pretty minimal, and we don't expect any issues going forward.   # Installation  ```shell $ pip install kgb==7.2 ```  # Learn More  * [kgb 7.2 release notes](https://github.com/beanbaginc/kgb/blob/master/NEWS.rst#kgb-72-3-november-2024) * [Documentation](https://github.com/bean |
| `release-7.2` | 2024-11-04 | Low | kgb 7.2 introduces new support for Python 3.13, fixing crashes and ensuring stability going forward.  Python 3.13 made changes to how functions were composed and executed, requiring changes in kgb. These were pretty minimal, and we don't expect any issues going forward.   # Installation  ```shell $ pip install kgb==7.2 ```  # Learn More  * [kgb 7.2 release notes](https://github.com/beanbaginc/kgb/blob/master/NEWS.rst#kgb-72-3-november-2024) * [Documentation](https://github.com/bean |
| `release-7.2` | 2024-11-04 | Low | kgb 7.2 introduces new support for Python 3.13, fixing crashes and ensuring stability going forward.  Python 3.13 made changes to how functions were composed and executed, requiring changes in kgb. These were pretty minimal, and we don't expect any issues going forward.   # Installation  ```shell $ pip install kgb==7.2 ```  # Learn More  * [kgb 7.2 release notes](https://github.com/beanbaginc/kgb/blob/master/NEWS.rst#kgb-72-3-november-2024) * [Documentation](https://github.com/bean |
| `release-7.2` | 2024-11-04 | Low | kgb 7.2 introduces new support for Python 3.13, fixing crashes and ensuring stability going forward.  Python 3.13 made changes to how functions were composed and executed, requiring changes in kgb. These were pretty minimal, and we don't expect any issues going forward.   # Installation  ```shell $ pip install kgb==7.2 ```  # Learn More  * [kgb 7.2 release notes](https://github.com/beanbaginc/kgb/blob/master/NEWS.rst#kgb-72-3-november-2024) * [Documentation](https://github.com/bean |
| `release-7.2` | 2024-11-04 | Low | kgb 7.2 introduces new support for Python 3.13, fixing crashes and ensuring stability going forward.  Python 3.13 made changes to how functions were composed and executed, requiring changes in kgb. These were pretty minimal, and we don't expect any issues going forward.   # Installation  ```shell $ pip install kgb==7.2 ```  # Learn More  * [kgb 7.2 release notes](https://github.com/beanbaginc/kgb/blob/master/NEWS.rst#kgb-72-3-november-2024) * [Documentation](https://github.com/bean |
| `release-7.2` | 2024-11-04 | Low | kgb 7.2 introduces new support for Python 3.13, fixing crashes and ensuring stability going forward.  Python 3.13 made changes to how functions were composed and executed, requiring changes in kgb. These were pretty minimal, and we don't expect any issues going forward.   # Installation  ```shell $ pip install kgb==7.2 ```  # Learn More  * [kgb 7.2 release notes](https://github.com/beanbaginc/kgb/blob/master/NEWS.rst#kgb-72-3-november-2024) * [Documentation](https://github.com/bean |
| `release-7.2` | 2024-11-04 | Low | kgb 7.2 introduces new support for Python 3.13, fixing crashes and ensuring stability going forward.  Python 3.13 made changes to how functions were composed and executed, requiring changes in kgb. These were pretty minimal, and we don't expect any issues going forward.   # Installation  ```shell $ pip install kgb==7.2 ```  # Learn More  * [kgb 7.2 release notes](https://github.com/beanbaginc/kgb/blob/master/NEWS.rst#kgb-72-3-november-2024) * [Documentation](https://github.com/bean |
| `release-7.2` | 2024-11-04 | Low | kgb 7.2 introduces new support for Python 3.13, fixing crashes and ensuring stability going forward.  Python 3.13 made changes to how functions were composed and executed, requiring changes in kgb. These were pretty minimal, and we don't expect any issues going forward.   # Installation  ```shell $ pip install kgb==7.2 ```  # Learn More  * [kgb 7.2 release notes](https://github.com/beanbaginc/kgb/blob/master/NEWS.rst#kgb-72-3-november-2024) * [Documentation](https://github.com/bean |

## Citation

- HTML: https://www.freshcrate.ai/projects/kgb
- Markdown: https://www.freshcrate.ai/projects/kgb.md
- Dependencies JSON: https://www.freshcrate.ai/api/projects/kgb/deps

_Generated by freshcrate.ai. Indexes pypi releases for AI-agent ecosystem packages._
