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.13.0.tar.gz (91.2 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.13.0-py3-none-any.whl (151.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: object_mother_pattern-3.13.0.tar.gz
  • Upload date:
  • Size: 91.2 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.13.0.tar.gz
Algorithm Hash digest
SHA256 10f3e41ad8d3cd3d123cd31be7468079c96a49ff48905c154d6143e3098da3b4
MD5 54e7d118007e426f9e8d8695661aafc8
BLAKE2b-256 e7eb50812d6248602f606c48416b73a1fa6125455a4965b5213e2e849ec23cac

See more details on using hashes here.

Provenance

The following attestation bundles were made for object_mother_pattern-3.13.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.13.0-py3-none-any.whl.

File metadata

File hashes

Hashes for object_mother_pattern-3.13.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1167bb864107882ffe0a551d876c6a3e9f0e2caf67d07feb3a7163ee6f9d9269
MD5 5fe78f85359d080f347f0b636aa40936
BLAKE2b-256 61e2a66e5726ebc32ef751b684203de4bdcf03f019a98b2bd5d85024715bca75

See more details on using hashes here.

Provenance

The following attestation bundles were made for object_mother_pattern-3.13.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