Package: shims
shimsis an Apache2 licensed Python module with patching and mocking
utilities.Theunittestpackage has clever functions for patching modules and
objects. Unfortunately, the common interface is not very readable:importunittestfromunittest.mockimportpatchclassTestThing(unittest.TestCase):@patch("a.b.c")@patch("x.y.z")@patch("foo.bar.baz")@patch("one.two.three")deftest_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 ofpatchdecorators
and the order of the arguments (me, times :100:)?Also, raise your hand if you’ve struggled to find the right string argument forpatch:wave:?Patch is a great utility but the decorator pattern is not very readable. Thepatchfunction actually returns patch objects. And patch objects includestart()andstop()methods. But these methods are hard to invoke
automatically without decorators usingunittest.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:fromunittest.mockimportMagicMockimportpytestimporta.bimportx.yimportfoo.barimportone.twodeftest_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:importpytestimporta.bimportx.yimportfoo.barimportone.twodeftest_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:Decorator pattern replaced with function calls.Targets used directly rather than by strings.MagicMock objects are created automatically.The end goal is to integrate shims into pytest itself.FeaturesPure-PythonPytest Support (Optional)Developed on Python 3.8Tested on CPython 3.6, 3.7, 3.8 and PyPy, PyPy3Tested using GitHub Actions on Linux, Mac, and WindowsQuickstartInstallingshimsis simple withpip:$ pip install shimsYou can access documentation in the interpreter with Python’s built-in help
function:>>>importshims>>>help(shims)# doctest: +SKIPTutorialTheshimsmodule provides utilities for patching and mocking.>>>importurllib.request>>>response=urllib.request.urlopen('http://www.example.com/').read()>>>print(response[:63].decode())<!doctypehtml><html><head><title>ExampleDomain</title>>>>importshims>>>mock_urlopen=shims.patch(urllib.request.urlopen)>>>mock_urlopen.return_value='<test response>'>>>urllib.request.urlopen('http://www.example.com/')'<test response>'>>>shims.stop()Referenceshims Documentationshims at PyPIshims at GitHubshims Issue TrackerLicenseCopyright 2020 Grant JenksLicensed 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 athttp://www.apache.org/licenses/LICENSE-2.0Unless 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.