Skip to main content

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.

Project description

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

strongmock-0.1.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

strongmock-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file strongmock-0.1.0.tar.gz.

File metadata

  • Download URL: strongmock-0.1.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.0

File hashes

Hashes for strongmock-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8ff7d2940bded741cb7cae6223f6273482c296bee2048163d16bdeb91f39ce0b
MD5 33bdd197d9a9d63ef93a046eef31a447
BLAKE2b-256 c5504d0395401bfd1c5b781805704f0fab784de9bb3b82d1895e11b4fa3f4fbe

See more details on using hashes here.

File details

Details for the file strongmock-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: strongmock-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.0

File hashes

Hashes for strongmock-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4fc5941a628828d008ddaef9112e2b5a91edbeb37088d9317c0b36fddb91f1bb
MD5 5db0cdf8be53cfea07c054a05d0445dc
BLAKE2b-256 cb0ddba7b572e023939760e9673a3026e8c69cfb982986fa4381bf8d95adc414

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page