Skip to main content

A general purpose mock library for Python

Project description

Python Fake

🌟 Overview

This project provides a Fake class that can be used to replace any object in a test, in development environment or in any other situation where you need to replace an object with a fake one.

The tries to provide a mock implementation for any usage, including properties, items, length, iterator, return value, await value, etc so that it never fails in runtime due to a missing implementation.

On the other hand, sometimes other pieces of code expect a certain implementation and the default implementation of the Fake class is not enough. In this case, you can use the __Fake_... parameters in the constructor to provide a more specific implementation.

At the moment, it doesn't provide tools for monitoring how the object was used, but it is planned to be implemented in the future.

🔎 Sample Usage

Fakeing the picamera2 module of Raspberry Pi so that one can run the code on a non-Raspberry Pi development environment:

from fake import Fake

sys.modules['picamera2'] = Fake(
    _Fake__props={
        'Picamera2': Fake(
            _Fake__return_value=Fake(
                _Fake__props={
                    'capture_array': Fake(
                        _Fake__return_value=np.zeros((1, 1, 3), dtype=np.uint8),
                    ),
                },
            ),
        ),
    },
)

⚙️ Parameters

A Fake object can be created with the following parameters:

  • _Fake__props: A dictionary that contains the properties of the object and their respective values.
  • _Fake__items: A dictionary that contains the items of the object and their respective values.
  • _Fake__list: A list that contains the items of the object. (in case the index is a number and not listed in the __items dictionary). It also sets the length of the object if _Fake__length is not set.
  • _Fake__return_value: The return value of the object when it is called.
  • _Fake__await_value: The return value of the object when it is awaited.
  • _Fake__length: The length of the object when it is queried via the len function.
  • _Fake__iter: The iterator of the object when it is queried via the iter function.

📦 Installation

Pip

pip install python-fake

Poetry

poetry add python-fake

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

python_fake-0.1.3.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

python_fake-0.1.3-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file python_fake-0.1.3.tar.gz.

File metadata

  • Download URL: python_fake-0.1.3.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for python_fake-0.1.3.tar.gz
Algorithm Hash digest
SHA256 cc693137abe00bdb3f16c4f945befe5b8746630ea769635843837c399b291d49
MD5 a80cdf4f09eeca0297eac1f1cc57c582
BLAKE2b-256 ba2d8da03111d88945a95bd671731224b7d49409655c133b7c274a2cd873e3b7

See more details on using hashes here.

File details

Details for the file python_fake-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: python_fake-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for python_fake-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 178b847bde6de1b0b5cc5643373bcee9ec70fe3d4558628b6bfac686e15da74c
MD5 7e53ce6080cd6c877df3b73389e15f66
BLAKE2b-256 f09ac030d3a892b31c6287da1227670eb051f93fe7bd1d62de6c9b909a9a8e6d

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