testing.generators.templates.templates

Module with implementation of templates that complement generator components providing more customization options.

Classes

  • EmptyTemplate - Template for generating an empty string.

  • ConstantTemplate - Template for generating a constant string.

  • ChoiceTemplate - Template for randomly selecting from a list of choices.

  • RandomTemplate - Template for generating a random string of ASCII letters and digits.

class EmptyTemplate[source]

Bases: BaseTemplate

Template for generating an empty string. Convenience template for default generator behavior.

Example

>>> template = EmptyTemplate()
... template.generate()
''
generate() str[source]

Generates an empty string.

Returns

str

An empty string.

class ConstantTemplate(value: str)[source]

Bases: BaseTemplate

Template for generating a constant string. Convenience template for always generating the same content.

Example

>>> template = ConstantTemplate("Hello World!")
... template.generate()
'Hello World!'
__init__(value: str) None[source]

Initializes ConstantTemplate instance.

Parameters

valuestr

The constant string value.

generate() str[source]

Generates the constant string.

Returns

str

The constant string value defined in initializer.

class ChoiceTemplate(choices: Iterable, seed: int | None = None)[source]

Bases: BaseTemplate

Template for randomly selecting from a list of choices.

Example

>>> template = ChoiceTemplate(["apple", "banana", "cherry"], seed=42)
... template.generate()
'cherry'
__init__(choices: Iterable, seed: int | None = None) None[source]

Initializes ChoiceTemplate instance.

Parameters

choicesIterable

Iterable of choices to select from.

seedint, optional

Random seed for reproducibility. Defaults to None, no seed is used.

generate() str[source]

Generates a random choice from the list of choices.

Returns

str

The randomly selected choice.

class RandomTemplate(length: int = 4, seed: int | None = None)[source]

Bases: BaseTemplate

Template for generating a random string of ASCII letters and digits.

Example

>>> template = RandomTemplate(length=4, seed=42)
... template.generate()
'Nbrn'
__init__(length: int = 4, seed: int | None = None) None[source]

Initializes RandomTemplate instance.

Parameters

lengthint, optional

Length of the random string. Defaults to 4.

seedint, optional

Random seed for reproducibility. Defaults to None.

generate() str[source]

Generates a random string from ASCII letters and digits.

Returns

str

The generated random string.