Skip to main content

Simian Version Build Coverage Health

Compatibility Implementations Format Downloads

A decorator for easily mocking out multiple dependencies by monkey-patching.

@simian.patch(module=..., external=[...], internal=[...])

Installation:

$ pip install simian

simian.patch is a convenience wrapper around mock.patch with the following benefits:

  1. All patched objects are collected under a single master_mock, which is provided to the function being decorated. Any patched target can be accessed by its basename (e.g., a patched time.sleep would be accessed in the decorated function as master_mock.sleep).

  2. The patching works even if the target is imported directly (e.g., a call to sleep is patched the same as a call to time.sleep). Simian handles this by reloading the module under test after applying the external patches.

  3. Objects internal to the module under test can be patched as well. They are collected under the same master_mock and can be referenced in the same way.

After leaving the decorated function, simian reloads the module under test again, bringing it back to its pre-patched state (although all of the module’s addresses in memory have changed).

External Patching

#
# my_package.my_module
#

from time import sleep


def my_sleep(duration_secs):
    print('Sleeping for {n} seconds'.format(n=duration_secs))
    sleep(duration_secs)


#
# my_package.test.test_my_module
#

import simian
from my_package import my_module


@simian.patch(my_module, external=['time.sleep'])
def test_my_sleep(master_mock):
    my_module.my_sleep(99)
    master_mock.sleep.assert_called_once_with(99)

Internal Patching

The above example demonstrates external patching, but internal (same-module) patching works as well. Let’s extend the above example.

#
# my_package.my_module
#

from time import sleep


def my_sleep(duration_secs):
    my_logger('Starting {n}-second sleep'.format(n=duration_secs))
    sleep(duration_secs)
    my_logger('Finished {n}-second sleep'.format(n=duration_secs))


def my_logger(msg):
    print(msg)


#
# my_package.test.test_my_module
#

import simian
from mock import call
from my_package import my_module


@simian.patch(my_module, external=['time.sleep'], internal=['my_logger'])
def test_my_sleep(master_mock):
    my_module.my_sleep(99)
    master_mock.assert_has_calls(
        calls=[
            call.my_logger('Starting 99-second sleep'),
            call.sleep(99),
            call.my_logger('Finished 99-second sleep')],
        any_order=False)

Metadata

Release files for simian 2.0.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 simian 2.0.0
File Size Uploaded Provenance Actions
simian-2.0.0.tar.gz 3.3 kB Without provenance Details for simian-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for simian 2.0.0
File Interpreter ABI Platform Actions
simian-2.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details for simian-2.0.0-py2.py3-none-any.whl

Total release size: 6.9 kB

Release files / simian-2.0.0.tar.gz

Download URL simian-2.0.0.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
86ab477e4793b0b6a35b94f9a3a218173587d6ed7d76ccd5125ef52e9cbadcd8
BLAKE2b-256 checksum
How to use checksums
3ecd50af692bb724f41f91555bdf053acd825bc5b19261221fb9186567859d2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / simian-2.0.0-py2.py3-none-any.whl

Download URL simian-2.0.0-py2.py3-none-any.whl
Size 3.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
fd67335019b90078a5d89e0af20963bceea3b4ecd4365c79e8a2ce090ff8fb8f
BLAKE2b-256 checksum
How to use checksums
1cebf982a28107f9c4ae05f49e851284780adc0c124551a0ec7859341f15db7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for simian 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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