Generate
Generator functionality.
This module provides flexible value generators for use in Pact contracts. Generators allow you to specify dynamic values for contract testing, ensuring that your tests remain robust and non-deterministic where appropriate. These generators are typically used in conjunction with matchers to produce example data for consumer-driven contract tests.
Generators are essential for producing dynamic values in contract tests, such as random integers, dates, UUIDs, and more. This helps ensure that your contracts are resilient to changes and do not rely on hardcoded values, which can lead to brittle tests.
Warning
Do not import functions directly from pact.generate
to avoid shadowing
Python built-in types. Instead, import the generate
module and use its
functions as generate.int
, generate.str
, etc.
Many functions in this module are named after the type they generate (e.g.,
int
, str
, bool
). Importing directly from this module may shadow Python
built-in types, so always use the generate
module.
Generators are typically used in conjunction with matchers, which allow Pact to
validate values during contract tests. If a value
is not provided within a
matcher, a generator will produce a random value that conforms to the specified
constraints.
Basic Types¶
Generate random values for basic types:
from pact import generate
random_bool = generate.bool()
random_int = generate.int(min=0, max=100)
random_float = generate.float(precision=2)
random_str = generate.str(size=12)
Dates, Times, and UUIDs¶
Produce values in specific formats:
random_date = generate.date(format="%Y-%m-%d")
random_time = generate.time(format="%H:%M:%S")
random_datetime = generate.datetime(format="%Y-%m-%dT%H:%M:%S%z")
random_uuid = generate.uuid(format="lowercase")
Regex and Hexadecimal¶
Generate values matching a pattern or hexadecimal format:
Provider State and Mock Server URLs¶
For advanced contract scenarios:
provider_value = generate.provider_state(expression="user_id")
mock_url = generate.mock_server_url(regex=r"http://localhost:\d+")
For more details and advanced usage, see the documentation for each function below.
Classes¶
AbstractGenerator
¶
Bases: ABC
Abstract generator.
In Pact, a generator is used by Pact to generate data on-the-fly during the contract verification process. Generators are used in combination with matchers to provide more flexible matching of data.
This class is abstract and should not be used directly. Instead, use one of the concrete generator classes. Alternatively, you can create your own generator by subclassing this class
The matcher provides methods to convert into an integration JSON object and a matching rule. These methods are used internally by the Pact library when communicating with the FFI and generating the Pact file.
Functions¶
to_generator_json() -> dict[str, Any]
abstractmethod
¶
Convert the generator to a generator JSON object.
This method is used internally to convert the generator to a JSON object which can be embedded directly in a number of places in the Pact FFI.
For more information about this format, refer to the Pact specification and the matchers section
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
The generator as a generator JSON object. |
Source code in src/pact/generate/generator.py
to_integration_json() -> dict[str, Any]
abstractmethod
¶
Convert the generator to an integration JSON object.
See
AbstractGenerator.to_integration_json
for more information.
Source code in src/pact/generate/generator.py
Functions¶
bool() -> AbstractGenerator
¶
Generate a random boolean value.
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing random boolean values. |
boolean() -> AbstractGenerator
¶
Alias for generate.bool
.
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing random boolean values. |
date(format: builtins.str = '%Y-%m-%d', *, disable_conversion: builtins.bool = False) -> AbstractGenerator
¶
Generate a date value.
Uses Python's
strftime
format, converted to Java
SimpleDateFormat
for Pact compatibility.
PARAMETER | DESCRIPTION |
---|---|
format
|
Expected format of the date. Defaults to ISO 8601:
TYPE:
|
disable_conversion
|
If True, disables conversion from Python's format to Java's format. The value must then be a Java format string.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing dates in the specified format. |
Source code in src/pact/generate/__init__.py
datetime(format: builtins.str, *, disable_conversion: builtins.bool = False) -> AbstractGenerator
¶
Generate a datetime value.
Uses Python's
strftime
format, converted to Java
SimpleDateFormat
for Pact compatibility.
PARAMETER | DESCRIPTION |
---|---|
format
|
Expected format of the timestamp. Defaults to ISO 8601:
TYPE:
|
disable_conversion
|
If True, disables conversion from Python's format to Java's format. The value must then be a Java format string.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing datetimes in the specified format. |
Source code in src/pact/generate/__init__.py
decimal(precision: builtins.int | None = None) -> AbstractGenerator
¶
Alias for generate.float
.
PARAMETER | DESCRIPTION |
---|---|
precision
|
Number of digits to generate.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing random decimal values. |
Source code in src/pact/generate/__init__.py
float(precision: builtins.int | None = None) -> AbstractGenerator
¶
Generate a random decimal number.
Precision refers to the total number of digits (excluding leading zeros),
not decimal places. For example, precision of 3 may yield 0.123
or 12.3
.
PARAMETER | DESCRIPTION |
---|---|
precision
|
Number of digits to generate.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing random decimal values. |
Source code in src/pact/generate/__init__.py
hex(digits: builtins.int | None = None) -> AbstractGenerator
¶
Generate a random hexadecimal value.
PARAMETER | DESCRIPTION |
---|---|
digits
|
Number of digits to generate.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing random hexadecimal values. |
Source code in src/pact/generate/__init__.py
hexadecimal(digits: builtins.int | None = None) -> AbstractGenerator
¶
Alias for generate.hex
.
PARAMETER | DESCRIPTION |
---|---|
digits
|
Number of digits to generate.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing random hexadecimal values. |
Source code in src/pact/generate/__init__.py
int(*, min: builtins.int | None = None, max: builtins.int | None = None) -> AbstractGenerator
¶
Generate a random integer.
PARAMETER | DESCRIPTION |
---|---|
min
|
Minimum value for the integer.
TYPE:
|
max
|
Maximum value for the integer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing random integers. |
Source code in src/pact/generate/__init__.py
integer(*, min: builtins.int | None = None, max: builtins.int | None = None) -> AbstractGenerator
¶
Alias for generate.int
.
PARAMETER | DESCRIPTION |
---|---|
min
|
Minimum value for the integer.
TYPE:
|
max
|
Maximum value for the integer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing random integers. |
Source code in src/pact/generate/__init__.py
mock_server_url(regex: builtins.str | None = None, example: builtins.str | None = None) -> AbstractGenerator
¶
Generate a mock server URL.
PARAMETER | DESCRIPTION |
---|---|
regex
|
Regex pattern to match.
TYPE:
|
example
|
Example URL to use.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing mock server URLs. |
Source code in src/pact/generate/__init__.py
provider_state(expression: builtins.str | None = None) -> AbstractGenerator
¶
Generate a value from provider state context.
PARAMETER | DESCRIPTION |
---|---|
expression
|
Expression to look up provider state.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing values from provider state context. |
Source code in src/pact/generate/__init__.py
regex(regex: builtins.str) -> AbstractGenerator
¶
Generate a string matching a regex pattern.
PARAMETER | DESCRIPTION |
---|---|
regex
|
Regex pattern to match.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing strings matching the pattern. |
Source code in src/pact/generate/__init__.py
str(size: builtins.int | None = None) -> AbstractGenerator
¶
Generate a random string.
PARAMETER | DESCRIPTION |
---|---|
size
|
Size of the string to generate.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing random strings. |
Source code in src/pact/generate/__init__.py
string(size: builtins.int | None = None) -> AbstractGenerator
¶
Alias for generate.str
.
PARAMETER | DESCRIPTION |
---|---|
size
|
Size of the string to generate.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing random strings. |
Source code in src/pact/generate/__init__.py
time(format: builtins.str = '%H:%M:%S', *, disable_conversion: builtins.bool = False) -> AbstractGenerator
¶
Generate a time value.
Uses Python's
strftime
format, converted to Java
SimpleDateFormat
PARAMETER | DESCRIPTION |
---|---|
format
|
Expected format of the time. Defaults to ISO 8601:
TYPE:
|
disable_conversion
|
If True, disables conversion from Python's format to Java's format. The value must then be a Java format string.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing times in the specified format. |
Source code in src/pact/generate/__init__.py
timestamp(format: builtins.str, *, disable_conversion: builtins.bool = False) -> AbstractGenerator
¶
Alias for generate.datetime
.
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing datetimes in the specified format. |
Source code in src/pact/generate/__init__.py
uuid(format: _UUID_FORMAT_NAMES = 'lowercase') -> AbstractGenerator
¶
Generate a UUID.
PARAMETER | DESCRIPTION |
---|---|
format
|
Format of the UUID to generate. Only supported under the V4 specification.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
AbstractGenerator
|
Generator producing UUIDs in the specified format. |