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 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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python-spymock-0.3.0.tar.gz.
File metadata
- Download URL: python-spymock-0.3.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1032-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
adad41ff7d67ca7256bc9d07137e3669846b95c0d0abe052dde9fbfa332406f5
|
|
| MD5 |
14fb236aa2c2cb25843c80e8850a5602
|
|
| BLAKE2b-256 |
4a70404a0606d834b002f2e6f4fdacb968e9a17ab70535c64e341c524b3ecfcb
|
File details
Details for the file python_spymock-0.3.0-py3-none-any.whl.
File metadata
- Download URL: python_spymock-0.3.0-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1032-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17e316354dd7be54edc69ebcfd2384be0764792f507f8591ccfb6c1c2d3b4305
|
|
| MD5 |
f4506ca57dfe40d597c623710c97e832
|
|
| BLAKE2b-256 |
cc1a58860a03801bebf0e45b95148e9472d30aad6cb1a74e3b1a954ff3135fd2
|