The Object Mother Pattern is a Python package that simplifies and standardizes the creation of test objects.
Project description
⚒️ Object Mother Pattern
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
📥 Installation
You can install Object Mother Pattern using pip:
pip install object-mother-pattern
📚 Documentation
This project's documentation is powered by DeepWiki, which provides a comprehensive overview of the Object Mother Pattern and its usage.
💻 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
📃 Available Mothers
The package offers a wide collection of object mothers grouped by domain:
Primitives
object_mother_pattern.BooleanMother- Responsible for generating random boolean values.object_mother_pattern.BytesMother- Responsible for generating random bytes objects.object_mother_pattern.FloatMother- Responsible for generating random float values within a range.object_mother_pattern.IntegerMother- Responsible for generating random integer numbers within a range.object_mother_pattern.StringMother- Responsible for generating random strings with configurable length and characters.
Dates
object_mother_pattern.DateMother- Responsible for generating random date instances.object_mother_pattern.DatetimeMother- Responsible for generating random datetime instances.object_mother_pattern.StringDateMother- Responsible for generating ISO formatted date strings.object_mother_pattern.StringDatetimeMother- Responsible for generating ISO 8601 formatted datetime strings.object_mother_pattern.mothers.dates.TimezoneMother- Responsible for generating random timezone objects.object_mother_pattern.mothers.dates.StringTimezoneMother- Responsible for generating timezone names as strings.
Identifiers
object_mother_pattern.UuidMother- Responsible for generating random UUIDv4 values.object_mother_pattern.StringUuidMother- Responsible for generating UUIDv4 as strings.object_mother_pattern.mothers.identifiers.countries.spain.DniMother- Responsible for generating valid Spanish DNI numbers.object_mother_pattern.mothers.identifiers.countries.spain.NieMother- Responsible for generating valid Spanish NIE numbers.
Internet
object_mother_pattern.mothers.internet.AwsCloudRegionMother- Responsible for generating AWS region codes.object_mother_pattern.mothers.internet.DomainMother- Responsible for generating domain names with valid TLDs.object_mother_pattern.mothers.internet.Ipv4AddressMother- Responsible for generating IPv4 addresses.object_mother_pattern.mothers.internet.Ipv4NetworkMother- Responsible for generating IPv4 network ranges.object_mother_pattern.mothers.internet.Ipv6AddressMother- Responsible for generating IPv6 addresses.object_mother_pattern.mothers.internet.Ipv6NetworkMother- Responsible for generating IPv6 network ranges.object_mother_pattern.mothers.internet.MacAddressMother- Responsible for generating MAC addresses in various formats.object_mother_pattern.mothers.internet.PortMother- Responsible for generating network port numbers.
Money
object_mother_pattern.mothers.money.cryptocurrencies.BtcWalletMother- Responsible for generating Bitcoin wallet addresses using BIP39 words list.
People
object_mother_pattern.mothers.people.FullNameMother- Responsible for generating realistic full names.object_mother_pattern.mothers.people.PasswordMother- Responsible for generating password strings with strength options.object_mother_pattern.mothers.people.UsernameMother- Responsible for generating username strings.
Extra
object_mother_pattern.mothers.extra.TextMother- Responsible for generating random text snippets.
🎄 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)
🧑🔧 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)
🤝 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! 🌟
🔑 License
This project is licensed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6dee43670c4d66594f4be271232b9220793781471ad01323582ca79e8208a145
|
|
| MD5 |
5e7b54914deec0b48cdf717ca1f24392
|
|
| BLAKE2b-256 |
bcd08630a615b15596768a136f13ef8da0c840e650fb7611285b6e894e7a5986
|
Provenance
The following attestation bundles were made for object_mother_pattern-3.11.0.tar.gz:
Publisher:
ci.yaml on adriamontoto/object-mother-pattern
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
object_mother_pattern-3.11.0.tar.gz -
Subject digest:
6dee43670c4d66594f4be271232b9220793781471ad01323582ca79e8208a145 - Sigstore transparency entry: 996225519
- Sigstore integration time:
-
Permalink:
adriamontoto/object-mother-pattern@b5443e8f78c33e4d1a597f5e3667efb804ce5da7 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/adriamontoto
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yaml@b5443e8f78c33e4d1a597f5e3667efb804ce5da7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file object_mother_pattern-3.11.0-py3-none-any.whl.
File metadata
- Download URL: object_mother_pattern-3.11.0-py3-none-any.whl
- Upload date:
- Size: 124.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d43ebdbfefbe5421d503fbc8cf9c63013aeaaed9477178c2e7cdae2064a650d0
|
|
| MD5 |
2ae18c9c8bb37a9df2c2b6e18f39bc32
|
|
| BLAKE2b-256 |
e0478ab0bda57330434fd2dda90bac2f083acc5589f35437786c05d553135d3b
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
object_mother_pattern-3.11.0-py3-none-any.whl -
Subject digest:
d43ebdbfefbe5421d503fbc8cf9c63013aeaaed9477178c2e7cdae2064a650d0 - Sigstore transparency entry: 996225619
- Sigstore integration time:
-
Permalink:
adriamontoto/object-mother-pattern@b5443e8f78c33e4d1a597f5e3667efb804ce5da7 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/adriamontoto
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yaml@b5443e8f78c33e4d1a597f5e3667efb804ce5da7 -
Trigger Event:
push
-
Statement type: