Skip to main content

StrongMock

Ever wanted Unlimited Power while patching things ?

StrongMock is a powerful mocking library for Python that leverages low-level ctypes functionality to provide extensive mocking capabilities. Some care may be needed while using this.

Functionality/Usage

Basic

  1. Instead of from unittest.mock import patch use from strongmock import strongpatch.
  2. strongpatch is an alias for patch in the module, meaning from strongmock import patch can also be used.
  3. Full compatibility - all methods and classes present in unittest.mock are present in strongmock.
  4. isinstance compatibility - limited to Mock, MagicMock and AsyncMock.

More features

strongpatch.equal_basic_objects

Can make floats / ints / True False and other basic objects equal to each other.
This customization of the equality behavior of basic objects can be useful when you have to test very very specific edge cases, maybe something governing program flow etc. Can also be used as a context manager.

strongpatch.mock_imports

Replaces imports with Magicmock, takes in a tuple of strings, optional parameter override which decides if we override existing imports as well (True by default). Can also be used as a context manager.

Async

Also works on async functions, with similar stronger patching

Safety

Some care has been taken to avoid crashes and breakage, but the user does have full control. One word answer - no.

Note - we have 100% Coverage, all Testcases passing

Working and usage

strongpatch

This applies when the target of a strongmock.strongpatch is a function defined in python with a __code__ attribute. For other cases (methods in a class, classes, lbrary functions in c, etc.), the behaviour is the same as unittest.patch.
This will patch the __code__ attribute of the function to call the mock, meaning that references will also have the functionality of mock.
This can be extremely convenient in some cases.

strongpatch.mock_imports

We can pass testcases that need imports inside them

class TestImportPatch(unittest.TestCase):
    @strongpatch.mock_imports(("somelib_abcd",))
    def test_import_somelib_abcd(self):
        import somelib_abcd
        self.assertIsInstance(somelib_abcd.somefn(), MagicMock)

strongpatch.equal_basic_objects

We dump the bytes from objsrc to objdst, meaning we can do basically do True = False or 1 = 2 (not == as in comparison but = as in assignment)

huh?

Yes, you can now pass these testcases.

import unittest
from strongmock import strongpatch
class StrongMockDemoTest(unittest.TestCase):
    @strongpatch.equal_basic_objects(False, True)
    def test_truefalse_patch_0(self):
        if True != False:
            raise RuntimeError("TRUEFALSE PATCH FAILED")

Links

PyPi
GitHub

License

StrongMock is licensed under the Unlicense. See the LICENSE file for details.

Official theme music

When using these methods, it is recommended that you listen to this for better code.
Kai's Theme Epic Version (Slowed + BassBoosted)

Metadata

Release files for strongmock 0.1.0

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

Source distribution (sdist)

Source distribution for strongmock 0.1.0
File Size Uploaded
strongmock-0.1.0.tar.gz 6.4 kB Details

Built distribution (wheel)

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

Total release size: 11.8 kB

Release files / strongmock-0.1.0.tar.gz

Download URL strongmock-0.1.0.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8ff7d2940bded741cb7cae6223f6273482c296bee2048163d16bdeb91f39ce0b
BLAKE2b-256 checksum
How to use checksums
c5504d0395401bfd1c5b781805704f0fab784de9bb3b82d1895e11b4fa3f4fbe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.0

Release files / strongmock-0.1.0-py3-none-any.whl

Download URL strongmock-0.1.0-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4fc5941a628828d008ddaef9112e2b5a91edbeb37088d9317c0b36fddb91f1bb
BLAKE2b-256 checksum
How to use checksums
cb0ddba7b572e023939760e9673a3026e8c69cfb982986fa4381bf8d95adc414
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

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