kgb 7.0 has been released with a handful of new features:
- Python 3.10 support
- Support for pytest, and other non-unittest-based test frameworks
- Snake-case and standalone assertion methods
- Workarounds for spying on methods with poorly-implemented decorators
It also drops official support for Python 2.6 and 3.4-3.5 (though these should still work for now, if you need them).
kgb now provides a
spy_agency fixture for pytest unit tests. This will set up a
SpyAgency for you, letting you spy on functions and assert calls. For example:
device = DoomsdayDevice()
SpyAgency will be managed for the lifetime of the test, removing spies upon completion.
Snake-case and standalone assertion methods
We’ve provided snake_case versions of all the assertion methods, making for more natural tests. For instance, you can use
assert_spy_called_with() instead of
assertSpyCalledWith(). The old camelCase versions will continue to exist, though.
You can also call assertion methods without needing to mix in a
SpyAgency into your test. Just import from
kgb.asserts. This is useful if you have a need to spy on methods and assert them within non-unit test code, or without access to a
SpyAgency. For example:
from kgb.asserts import assert_spy_called_with
Workarounds for bad decorators
Before, if you were trying to spy on a method (particularly unbound methods) wrapped in a decorator, and that decorator didn’t preserve the function’s original name, you’d hit an error looking up the method.
You can now work around this by passing the original function name when setting up a spy. For example:
def _wrap(*args, **kwargs):
return func(*args, **kwargs)
kgb will try to detect when you need this and emit a warning during the test.
Get started with kgb 7.0
See the release notes for the full list of changes.
You can install kgb today through
$ pip install -U kgb
Visit our kgb documentation to see how to use kgb in your projects.