Mocking in Python made easy
Project description
PyMock
Mocking in Python made easy!
Install
PyMock can be installed with pip
$ pip install python-mock
Note: PyMock require Python version >= 3.7 to work properly.
How is it different?
The typical way of mocking in Python is with MagicMock
or simply Mock
(https://docs.python.org/3/library/unittest.mock.html).
def test_something():
blog = MagicMock()
blog.get_post.return_value = Post()
assert isinstance(blog.get_post(1), Post)
While this technically work the syntax can be difficult to remember, provides limited support for when your return values depend on input arguments and even if you're using a decent IDE there is no type hinting available to help with code completion.
PyMock
attempts to solve all of these issues by offering a simple syntax and full control over how
your mocks should respond in different situations. The same example as above can be written using
PyMock:
def test_something():
blog = PyMock.create(Blog)
PyMock.setup(blog.get_post(1)).returns(Post())
assert isinstance(blog.get_post(1), Post)
The benefits here are many, for example
- Just by looking at the first line it's very clear what type of object you intend to mock
- PyMock support setting different return values for different input arguments. In this case
it's
blog.get_post(1)
that should return an instance ofPost
, notblog.get_post(2)
or any other call to the same method. - Because PyMock know exactly what the mocked type is, code completion work just as if it were an object of that type.
Feedback
PyMock is an early state of development and is likely to contain bugs. Quite possibly lots of them. Don't let that hold you back from trying it out and let me know how it works for you! All kinds of feedback are valuable, good and bad
- Bug reports
- Performance
- Syntax
- Overall experience
- Success stories
- Anything else you want to share :)
Examples
Below are a few examples on how PyMock can be used.
from unittest import TestCase
from pymock import PyMock, Is
class Post:
def get_title(self) -> str:
pass
class Blog:
def get_post(self, id: int) -> Post:
pass
class CustomException(Exception):
pass
class TestExamples(TestCase):
def test_return_object(self):
post = Post()
mock = PyMock.create(Blog)
PyMock.setup(mock.get_post(123)).returns(post)
self.assertEqual(post, mock.get_post(123))
def test_setup_multiple_values(self):
post1 = Post()
post2 = Post()
mock = PyMock.create(Blog)
PyMock.setup(mock.get_post(1)).returns(post1)
PyMock.setup(mock.get_post(2)).returns(post2)
self.assertEqual(post1, mock.get_post(1))
self.assertEqual(post2, mock.get_post(2))
def test_raise_exception(self):
mock = PyMock.create(Blog)
PyMock.setup(mock.get_post(1)).raises(CustomException())
with self.assertRaises(CustomException):
mock.get_post(1)
def test_match_instance_type(self):
post = Post()
mock = PyMock.create(Blog)
PyMock.setup(mock.get_post(Is.type(int))).returns(post)
self.assertEqual(post, mock.get_post(12345))
def test_recursive_mocking(self):
mock = PyMock.create(Blog)
PyMock.setup(mock.get_post(123).get_title()).returns("PyMock is awesome")
self.assertEqual("PyMock is awesome", mock.get_post(123).get_title())
def test_mock_function(self):
def my_function():
pass
mock = PyMock.create(my_function)
PyMock.setup(mock()).returns("my_function return value")
self.assertEqual("my_function return value", mock())
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
File details
Details for the file python-mock-0.0.3.tar.gz
.
File metadata
- Download URL: python-mock-0.0.3.tar.gz
- Upload date:
- Size: 8.3 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 | fd3ac877545d4f1e6937d42e6c1c6284e14434b11aaab7c3960892950b0161fc |
|
MD5 | 0fec0cf54d4055bd13f28cd34d0ad9e4 |
|
BLAKE2b-256 | 41251755d388fdc86fbab75b6d00d4f01f2992a8c653695c223839a4b03ba722 |
File details
Details for the file python_mock-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: python_mock-0.0.3-py3-none-any.whl
- Upload date:
- Size: 11.4 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 | fefab19e8af652674400b40ef0f87ed5240d942c66b5daa9a9ecca05762cd973 |
|
MD5 | 238873a30a9abbbf919ac014f5e9482a |
|
BLAKE2b-256 | 01f558a4b963c30c56b595b3e74d689a1bc454e263de707d4210d00329b55ee5 |