Skip to main content

Shims: Patching and Mocking Utilities

shims is an Apache2 licensed Python module with patching and mocking utilities.

The unittest package has clever functions for patching modules and objects. Unfortunately, the common interface is not very readable:

import unittest
from unittest.mock import patch

class TestThing(unittest.TestCase):
    @patch("a.b.c")
    @patch("x.y.z")
    @patch("foo.bar.baz")
    @patch("one.two.three")
    def test_thing(self, mock_three, mock_baz, mock_z, mock_c):
        ...

if __name__ == "__main__":
    unittest.main()

Now, raise your hand if you’ve ever confused the order of patch decorators and the order of the arguments (me, times :100:)?

Also, raise your hand if you’ve struggled to find the right string argument for patch :wave:?

Patch is a great utility but the decorator pattern is not very readable. The patch function actually returns patch objects. And patch objects include start() and stop() methods. But these methods are hard to invoke automatically without decorators using unittest.

Furthermore, the patch target is referenced using strings to identify the object to patch. Most editors lack go-to-definition support for these strings which sometimes results in even less readable code.

Pytest now to the rescue! Here’s the same code, now using Pytest:

from unittest.mock import MagicMock
import pytest

import a.b
import x.y
import foo.bar
import one.two

def test_thing(monkeypatch):
    mock_c = MagicMock()
    monkeypatch.setattr(a.b, "c", mock_c)
    mock_z = MagicMock()
    monkeypatch.setattr(x.y, "z", mock_z)
    mock_baz = MagicMock()
    monkeypatch.setattr(foo.bar, "baz", mock_baz)
    mock_three = MagicMock()
    monkeypatch.setattr(one.two, "three", mock_three)
    ...

if __name__ == "__main__":
    pytest.main([__file__])

We’re no longer abusing the decorator pattern in Python but it’s still not very reasonable. The fixture idea is a good one and shims goes a bit farther with it.

Here’s the same code, now using shims:

import pytest

import a.b
import x.y
import foo.bar
import one.two

def test_thing(shims):
    mock_c = shims.patch(a.b.c)
    mock_z = shims.patch(x.y.z)
    mock_baz = shims.patch(foo.bar.baz)
    mock_three = shims.patch(one.two.three)
    ...

if __name__ == "__main__":
    pytest.main([__file__])

The problems solved with shims:

  1. Decorator pattern replaced with function calls.

  1. Targets used directly rather than by strings.

  1. MagicMock objects are created automatically.

The end goal is to integrate shims into pytest itself.

Features

  • Pure-Python

  • Pytest Support (Optional)

  • Developed on Python 3.8

  • Tested on CPython 3.6, 3.7, 3.8 and PyPy, PyPy3

  • Tested using GitHub Actions on Linux, Mac, and Windows

https://github.com/grantjenks/python-shims/workflows/integration/badge.svg

Quickstart

Installing shims is simple with pip:

$ pip install shims

You can access documentation in the interpreter with Python’s built-in help function:

>>> import shims
>>> help(shims)                         # doctest: +SKIP

Tutorial

The shims module provides utilities for patching and mocking.

>>> import urllib.request
>>> response = urllib.request.urlopen('http://www.example.com/').read()
>>> print(response[:63].decode())
<!doctype html>
<html>
<head>
    <title>Example Domain</title>
>>> import shims
>>> mock_urlopen = shims.patch(urllib.request.urlopen)
>>> mock_urlopen.return_value = '<test response>'
>>> urllib.request.urlopen('http://www.example.com/')
'<test response>'
>>> shims.stop()

Reference

License

Copyright 2020 Grant Jenks

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Metadata

Release files for shims 0.0.1

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

Source distribution (sdist)

Source distribution for shims 0.0.1
File Size Uploaded
shims-0.0.1.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for shims 0.0.1
File Interpreter ABI Platform
shims-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.6 kB

Release files / shims-0.0.1.tar.gz

Download URL shims-0.0.1.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4d8e838caaa7c00cf5f3fa0df22cb7bcdb37e784247df773983496bc8c0fb4fa
BLAKE2b-256 checksum
How to use checksums
1ebace75b459599bb95c43cbfce805ba8ca88244c29e4acc31bffe061f6ec97c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

Release files / shims-0.0.1-py3-none-any.whl

Download URL shims-0.0.1-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9351d98c6b56d653aee10b189935c51488617944f6e2f1ecc2292baac24f1a75
BLAKE2b-256 checksum
How to use checksums
ec833b2a38c1ea543cb23ea4c76ff50c4d22644ff440c6dc4bc1568f3a0462b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.0.1 This release

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