This library provides SpyMock
which is similar to MagicMock
but recording function return values and exceptions on call_values_or_exceptions
attribute.
pip install python-spymock
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",
}
}
Distributed under the terms of the MIT license.