Skip to main content

The Object Mother Pattern is a Python package that simplifies and standardizes the creation of test objects.

Project description

⚒️ Object Mother Pattern

CI Pipeline Coverage Pipeline Package Version Supported Python Versions Package Downloads Project Documentation

The Object Mother Pattern is a Python 🐍 package that simplifies and standardizes the creation of test 🧪 objects. This pattern is especially helpful in testing scenarios where you need to generate multiple instances of complex objects quickly and consistently. By providing a set of prebuilt 🛠️ object mothers, you can drop these into your existing test suite and skip the boilerplate setup yourself.

Table of Contents

🔼 Back to top



📥 Installation

You can install Object Mother Pattern using pip:

pip install object-mother-pattern

🔼 Back to top



📚 Documentation

This project's documentation is powered by DeepWiki, which provides a comprehensive overview of the Object Mother Pattern and its usage.

🔼 Back to top



💻 Utilization

Here is how you can utilize the Object Mother library to generate various types of test data:

from object_mother_pattern import (
    BooleanMother,
    FloatMother,
    IntegerMother,
    StringDateMother,
    StringMother,
    UuidMother,
)

# Generate a random integer between -4 and 15
number = IntegerMother.create(min=-4, max=15)
print(number)
# >>> 8

# Generate a random float between -4 and 15 with 5 Decimal Places
number = FloatMother.create(min=-4, max=15, decimals=5)
print(number)
# >>> 0.83396

# Generate a random boolean
boolean = BooleanMother.create()
print(boolean)
# >>> True

# Generate a random string
string = StringMother.create()
print(string)
# >>> zFUmlsODZqzwyGjrOOqBtYzNwlJdOETalkXbuSegoQpgEnYQTCDeoifWrTQXMm

# Generate a random string of specific length
string = StringMother.of_length(length=10)
print(string)
# >>> TfkrYRxUFT

# Generate a random UUID
uuid = UuidMother.create()
print(uuid)
# >>> 3e9e0f3a-64a3-474f-9127-368e723f389f

# Generate a random date
date = StringDateMother.create()
print(date)
# >>> 2015-09-15

🔼 Back to top



📃 Available Mothers

The package offers a wide collection of object mothers grouped by domain:

Primitives

Dates

Identifiers

Internet

Money

People

Extra

🔼 Back to top



🎄 Real-Life Case: Christmas Detector Service

Below is an example of a real-life scenario where Object Mother Pattern can help simplify test date creation. We have a ChristmasDetectorService that checks if a given date falls within a specific Christmas holiday range. Using the DateMother class, we can easily generate dates both within and outside of this range for our tests, this ensuring that every possible scenario is covered.

from datetime import date
from object_mother_pattern import DateMother


class ChristmasDetectorService:
    def __init__(self) -> None:
        self.christmas_start = date(year=2024, month=12, day=24)
        self.christmas_end = date(year=2025, month=1, day=6)

    def is_christmas(self, today: date) -> bool:
        return self.christmas_start <= today <= self.christmas_end


christmas_detector_service = ChristmasDetectorService()


def test_christmas_detector_is_christmas() -> None:
    date_mother = DateMother.create(
        start_date=date(year=2024, month=12, day=25),
        end_date=date(year=2025, month=1, day=6),
    )

    assert christmas_detector_service.is_christmas(today=date_mother)


def test_christmas_detector_is_not_christmas() -> None:
    date_mother = DateMother.out_of_range(
        start_date=date(year=2024, month=12, day=24),
        end_date=date(year=2025, month=1, day=6),
    )

    assert not christmas_detector_service.is_christmas(today=date_mother)

🔼 Back to top



🧑‍🔧 Creating your own Object Mother

You can extend the functionality of this library by subclassing the BaseMother class. Your custom mother must implement the create method to generate the desired type.

from random import randint

from object_mother_pattern.models import BaseMother


class IntegerMother(BaseMother[int]):
    @classmethod
    def create(cls, *, value: int | None = None, min: int = -100, max: int = 100) -> int:
        if value is not None:
            if not isinstance(value, int):
                raise TypeError('IntegerMother value must be an integer.')

            return value

        return randint(a=min, b=max)

🔼 Back to top



🤝 Contributing

We love community help! Before you open an issue or pull request, please read:

Thank you for helping make ⚒️ Object Mother Pattern package awesome! 🌟

🔼 Back to top



🔑 License

This project is licensed under the terms of the MIT license.

🔼 Back to top

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

object_mother_pattern-3.11.0.tar.gz (79.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

object_mother_pattern-3.11.0-py3-none-any.whl (124.0 kB view details)

Uploaded Python 3

File details

Details for the file object_mother_pattern-3.11.0.tar.gz.

File metadata

  • Download URL: object_mother_pattern-3.11.0.tar.gz
  • Upload date:
  • Size: 79.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for object_mother_pattern-3.11.0.tar.gz
Algorithm Hash digest
SHA256 6dee43670c4d66594f4be271232b9220793781471ad01323582ca79e8208a145
MD5 5e7b54914deec0b48cdf717ca1f24392
BLAKE2b-256 bcd08630a615b15596768a136f13ef8da0c840e650fb7611285b6e894e7a5986

See more details on using hashes here.

Provenance

The following attestation bundles were made for object_mother_pattern-3.11.0.tar.gz:

Publisher: ci.yaml on adriamontoto/object-mother-pattern

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file object_mother_pattern-3.11.0-py3-none-any.whl.

File metadata

File hashes

Hashes for object_mother_pattern-3.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d43ebdbfefbe5421d503fbc8cf9c63013aeaaed9477178c2e7cdae2064a650d0
MD5 2ae18c9c8bb37a9df2c2b6e18f39bc32
BLAKE2b-256 e0478ab0bda57330434fd2dda90bac2f083acc5589f35437786c05d553135d3b

See more details on using hashes here.

Provenance

The following attestation bundles were made for object_mother_pattern-3.11.0-py3-none-any.whl:

Publisher: ci.yaml on adriamontoto/object-mother-pattern

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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