spymock
This library provides SpyMock which is similar to MagicMock but recording function return values and exceptions on call_values_or_exceptions attribute.
Installation
pip install python-spymock
Usage
Use spymock.spy function as-like patch.object to spy and mock the target attribute like:
import urllib.request
from spymock import spy
def request():
url = "http://httpbin.org/json"
req = urllib.request.Request(url)
with urllib.request.urlopen(req) as res:
return json.loads(res.read())
def test_request_with_spy():
with spy(urllib.request, "urlopen") as s:
assert request() == {
"slideshow": {
"author": "Yours Truly",
"date": "date of publication",
"slides": [
{"title": "Wake up to WonderWidgets!", "type": "all"},
{
"items": [
"Why <em>WonderWidgets</em> are great",
"Who <em>buys</em> WonderWidgets",
],
"title": "Overview",
"type": "all",
},
],
"title": "Sample Slide Show",
}
}
# 's' is like MagicMock but it has 'call_values_or_exceptions' attribute
assert len(s.call_values_or_exceptions) == 1
r = s.call_values_or_exceptions[0]
assert isinstance(r, HTTPResponse)
assert r.status == 200
assert r.reason == "OK"
Or directly create spymock.SpyMock instance as-like MagicMock like:
import urllib.request
from spymock import SpyMock
def request():
url = "http://httpbin.org/json"
req = urllib.request.Request(url)
with urllib.request.urlopen(req) as res:
return json.loads(res.read())
def test_request_with_spymock():
s = SpyMock(request)
assert s() == {
"slideshow": {
"author": "Yours Truly",
"date": "date of publication",
"slides": [
{"title": "Wake up to WonderWidgets!", "type": "all"},
{
"items": [
"Why <em>WonderWidgets</em> are great",
"Who <em>buys</em> WonderWidgets",
],
"title": "Overview",
"type": "all",
},
],
"title": "Sample Slide Show",
}
}
# 's' is like MagicMock but it has 'call_values_or_exceptions' attribute
assert len(s.call_values_or_exceptions) == 1
r = s.call_values_or_exceptions[0]
assert r == {
"slideshow": {
"author": "Yours Truly",
"date": "date of publication",
"slides": [
{"title": "Wake up to WonderWidgets!", "type": "all"},
{
"items": [
"Why <em>WonderWidgets</em> are great",
"Who <em>buys</em> WonderWidgets",
],
"title": "Overview",
"type": "all",
},
],
"title": "Sample Slide Show",
}
}
License
Distributed under the terms of the MIT license.
Release files for python-spymock 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python-spymock-0.3.0.tar.gz | 4.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_spymock-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.2 kB
Release files / python-spymock-0.3.0.tar.gz
| Download URL | python-spymock-0.3.0.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
adad41ff7d67ca7256bc9d07137e3669846b95c0d0abe052dde9fbfa332406f5
|
|
BLAKE2b-256 checksum How to use checksums |
4a70404a0606d834b002f2e6f4fdacb968e9a17ab70535c64e341c524b3ecfcb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1032-azure
|
Release files / python_spymock-0.3.0-py3-none-any.whl
| Download URL | python_spymock-0.3.0-py3-none-any.whl |
|---|---|
| Size | 4.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
17e316354dd7be54edc69ebcfd2384be0764792f507f8591ccfb6c1c2d3b4305
|
|
BLAKE2b-256 checksum How to use checksums |
cc1a58860a03801bebf0e45b95148e9472d30aad6cb1a74e3b1a954ff3135fd2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1032-azure
|