No project description provided
Project description
PyMock
PyMock is a set of tools to help with mocking in python.
The typical way of mocking and controlling returned values in python is with MagicMock or simply Mock (https://docs.python.org/3/library/unittest.mock.html).
def test_something():
my_object = MagicMock()
my_object.some_method.side_effect = [val1, val2, val3]
A challenge often faced when working in a project is to know exactly how many times the mocked method is called
and in what order. For lists, side_effect return values one by one for each call to the method which
quickly becomes a game of chance getting that list correct. Sometimes the input arguments to the method is what should
determine which of the values to return which leaves you with the only option of creating another method or function
that side_effect can call to retrieve the correct value.
This is where PyMock comes in. Instead of fiddling with lists or writing separate functions,
you configure a PyMock instance with a set of calls and pass it in as a side_effect.
PyMock will look at the arguments passed in to the mocked method and return the correct value for you.
No extra code needed!
Examples
from unittest import TestCase
from unittest.mock import MagicMock
from pymock import PyMock, Is
class TestExamples(TestCase):
def test_return_object(self):
class Person:
pass
bob = Person()
mock = PyMock()
mock.call("bob").returns(bob)
person_repository = MagicMock()
person_repository.get_person.side_effect = mock
self.assertEqual(bob, person_repository.get_person("bob"))
def test_call_multiple_values(self):
mock = PyMock()
mock.call(1, 1).returns(2)
mock.call(1, 2).returns(3)
calculator = MagicMock()
calculator.add.side_effect = mock
self.assertEqual(2, calculator.add(1, 1))
self.assertEqual(3, calculator.add(1, 2))
def test_raise_exception(self):
class MyException(Exception):
pass
mock = PyMock()
mock.call("invalid").raises(MyException())
my_object = MagicMock()
my_object.method.side_effect = mock
with self.assertRaises(MyException):
my_object.method("invalid")
def test_return_magicmock_when_not_matching_any_calls(self):
mock = PyMock()
mock.call("won't match this").returns(1)
my_object = MagicMock()
my_object.method.side_effect = mock
self.assertIsInstance(my_object.method("hello"), MagicMock)
def test_match_instance_type(self):
class Date:
pass
mock = PyMock()
mock.call(Is.type(Date)).returns(True)
calendar = MagicMock()
calendar.has_events.side_effect = mock
self.assertEqual(True, calendar.has_events(Date()))
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-mock-0.0.2.tar.gz.
File metadata
- Download URL: python-mock-0.0.2.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9a47fa91504fd104be8c38ed35bb1f32dc5d98897adb3d31521b988e35c2038
|
|
| MD5 |
e30c0d57855b13f285556cf160ef7530
|
|
| BLAKE2b-256 |
d8e446fb1304f446b31b508905a7f99fd222675452ddda562287be77c849d9da
|
File details
Details for the file python_mock-0.0.2-py3-none-any.whl.
File metadata
- Download URL: python_mock-0.0.2-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03f6643032f30482b3393a3b940da6a70d36cafb2194b45b69d805f44fe4f838
|
|
| MD5 |
87bc31611a540dbb47190831356e798f
|
|
| BLAKE2b-256 |
818176d0bc69485a0266a3cadb39d3a904443124847b54c02f503c6a93f467a3
|