Skip to main content

PyMock

Mocking in Python made easy!

PyMock

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 of Post, not blog.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.

Code completion in action

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())

Metadata

Release files for python-mock 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-mock 0.0.3
File Size Uploaded
python-mock-0.0.3.tar.gz 8.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-mock 0.0.3
File Interpreter ABI Platform
python_mock-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 19.8 kB

Release files / python-mock-0.0.3.tar.gz

Download URL python-mock-0.0.3.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
fd3ac877545d4f1e6937d42e6c1c6284e14434b11aaab7c3960892950b0161fc
BLAKE2b-256 checksum
How to use checksums
41251755d388fdc86fbab75b6d00d4f01f2992a8c653695c223839a4b03ba722
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / python_mock-0.0.3-py3-none-any.whl

Download URL python_mock-0.0.3-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fefab19e8af652674400b40ef0f87ed5240d942c66b5daa9a9ecca05762cd973
BLAKE2b-256 checksum
How to use checksums
01f558a4b963c30c56b595b3e74d689a1bc454e263de707d4210d00329b55ee5
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page