No project description provided
Project description
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 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
python-spymock-0.2.0.tar.gz
(4.1 kB
view hashes)
Built Distribution
Close
Hashes for python_spymock-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff7bb3f176f4f570ce2dddabd23bd015fc6ad108637f6f471f8e51eeeba24927 |
|
MD5 | a74e4d6bba50e642b6d4b795f7f81ecc |
|
BLAKE2b-256 | e3bbe2ebee89f783fc55c2a34a2c255408e77bbc59462ce5616ac041f79c6623 |