API Documentation¶
This is a complete list of available methods, along with details about them.
fauxfactory¶
Generate random data for your tests.
fauxfactory.factories.booleans¶
Method for generating random boolean values.
- fauxfactory.factories.booleans.gen_boolean() bool[source]¶
Return a random Boolean value.
- Returns:
A random Boolean value.
- Return type:
- fauxfactory.factories.booleans.gen_choice(choices: Sequence[T]) T[source]¶
Return a random choice from the available choices.
- Parameters:
choices (list) – List of choices from which select a random value.
- Raises:
ValueErrorifchoicesisNoneor notIterableor adict.- Returns:
A random element from
choices.
fauxfactory.factories.choices¶
Module to keep methods related to selecting values.
- fauxfactory.factories.choices.gen_choice(choices: Sequence[T]) T[source]¶
Return a random choice from the available choices.
- Parameters:
choices (list) – List of choices from which select a random value.
- Raises:
ValueErrorifchoicesisNoneor notIterableor adict.- Returns:
A random element from
choices.
fauxfactory.constants¶
Constants used by fauxfactory.
- fauxfactory.constants.VALID_NETMASKS¶
A tuple of netmasks. The tuple index corresponds to a CIDR value. For example, a CIDR of “/1” corresponds to VALID_NETMASKS[1].
fauxfactory.factories.dates¶
Methods related to generating date/time related values.
- fauxfactory.factories.dates.gen_date(min_date: date | None = None, max_date: date | None = None) date[source]¶
Return a random date value.
- Parameters:
min_date – A valid
datetime.dateobject.max_date – A valid
datetime.dateobject.
- Raises:
ValueErrorif arguments are not validdatetime.dateobjects.- Returns:
Random
datetime.dateobject.
- fauxfactory.factories.dates.gen_datetime(min_date: datetime | None = None, max_date: datetime | None = None) datetime[source]¶
Return a random datetime value.
- Parameters:
min_date – A valid
datetime.datetimeobject.max_date – A valid
datetime.datetimeobject.
- Raises:
ValueErrorif arguments are not validdatetime.datetimeobjects.- Returns:
Random
datetime.datetimeobject.
fauxfactory.helpers¶
Collection of helper methods and functions.
- class fauxfactory.helpers.UnicodePlane(min, max)¶
- max¶
Alias for field number 1
- min¶
Alias for field number 0
- fauxfactory.helpers.base_repr(number: int, base: int) str[source]¶
Return the base representation of a decimal number.
As shared here: https://stackoverflow.com/a/2267446
Conversion steps:
Divide the number by the base
Get the integer quotient for the next iteration
Get the remainder for the hex digit
Repeat the steps until quotient is equal to zero
- Parameters:
number – (int) The decimal number to be converted.
base – The base to convert.
- Returns:
The base representation of <number>.
- fauxfactory.helpers.check_validation(fcn: F) F[source]¶
Decorate functions requiring validation.
Simple decorator to validate values generated by function fnc according to parameters validator, default and tries.
- Parameters:
fcn – function to be enhanced
- Returns:
decorated function
- fauxfactory.helpers.is_positive_int(length: Any) None[source]¶
Check that length argument is an integer greater than zero.
- Parameters:
length (int) – The desired length of the string
- Raises:
ValueError if length is not an int or is less than 1.
- fauxfactory.helpers.unicode_letters_generator(smp: bool = True) Generator[str, None, None][source]¶
Generate unicode characters in the letters category.
- Parameters:
smp (bool) – Include Supplementary Multilingual Plane (SMP) characters
- Returns:
a generator which will generates all unicode letters available
fauxfactory.factories.internet¶
Methods related to generating internet related values.
- fauxfactory.factories.internet.gen_alpha(length: int = 10, start: str | None = None, separator: str = '', validator: Callable[[str], bool] | Pattern[str] | str | None = None, default: str | None = None, tries: int = 10) str[source]¶
Return a random string made up of alpha characters.
- fauxfactory.factories.internet.gen_choice(choices: Sequence[T]) T[source]¶
Return a random choice from the available choices.
- Parameters:
choices (list) – List of choices from which select a random value.
- Raises:
ValueErrorifchoicesisNoneor notIterableor adict.- Returns:
A random element from
choices.
- fauxfactory.factories.internet.gen_domain(name: str | None = None, subdomain: str | None = None, tlds: str | None = None) str[source]¶
Generate a random domain name.
- fauxfactory.factories.internet.gen_email(name: str | None = None, domain: str | None = None, tlds: str | None = None) str[source]¶
Generate a random email address.
- fauxfactory.factories.internet.gen_ipaddr(ip3: bool = False, ipv6: bool = False, prefix: Sequence[str | int] = ()) str[source]¶
Generate a random IP address.
You can also specify an IP address prefix if you are interested in local network address generation, etc.
- Parameters:
- Returns:
An IP address.
- Return type:
- Raises:
ValueErrorifprefixwould lead to no random fields at all. This means the length that triggers theValueErroris 4 for regular IPv4, 3 for IPv4 with ip3 and 8 for IPv6. It will be raised in any case the prefix length reaches or exceeds those values.
- fauxfactory.factories.internet.gen_mac(delimiter: str = ':', multicast: bool | None = None, locally: bool | None = None) str[source]¶
Generate a random MAC address.
For more information about how unicast or multicast and globally unique and locally administered MAC addresses are generated check this link https://en.wikipedia.org/wiki/MAC_address.
- Parameters:
delimiter (str) – Valid MAC delimiter (e.g ‘:’, ‘-‘).
multicast (bool) – Indicates if the generated MAC address should be unicast or multicast. If no value is provided a random one will be chosen.
locally (bool) – Indicates if the generated MAC address should be globally unique or locally administered. If no value is provided a random one will be chosen.
- Returns:
A random MAC address.
- Return type:
- fauxfactory.factories.internet.gen_netmask(min_cidr: int = 1, max_cidr: int = 31) str[source]¶
Generate a random valid netmask.
For more info: http://www.iplocation.net/tools/netmask.php
- Parameters:
- Returns:
The netmask is chosen from
fauxfactory.constants.VALID_NETMASKSrespecting the CIDR range- Return type:
- Raises:
ValueErrorifmin_cidrormax_cidrhave an invalid value. For example,max_cidrcannot be 33.
fauxfactory.factories.numbers¶
Methods that generate random number values.
- fauxfactory.factories.numbers.gen_hexadecimal(min_value: str | None = None, max_value: str | None = None, *, base: int = 16) str¶
Return a random number (with <base> representation).
- Returns:
A random number with base of <base>.
- Return type:
- fauxfactory.factories.numbers.gen_integer(min_value: int | None = None, max_value: int | None = None) int[source]¶
Return a random integer value based on the current platform.
- fauxfactory.factories.numbers.gen_negative_integer() int[source]¶
Return a random negative integer based on the current platform.
- Returns:
Returns a random negative integer value.
- Return type:
- fauxfactory.factories.numbers.gen_number(min_value: str | None = None, max_value: str | None = None, base: int = 10) str[source]¶
Return a random number (with <base> representation).
- Returns:
A random number with base of <base>.
- Return type:
fauxfactory.factories.strings¶
Collection of string generating functions.
- fauxfactory.factories.strings.gen_alpha(length: int = 10, start: str | None = None, separator: str = '', validator: Callable[[str], bool] | Pattern[str] | str | None = None, default: str | None = None, tries: int = 10) str[source]¶
Return a random string made up of alpha characters.
- fauxfactory.factories.strings.gen_alphanumeric(length: int = 10, start: str | None = None, separator: str = '', validator: Callable[[str], bool] | Pattern[str] | str | None = None, default: str | None = None, tries: int = 10) str[source]¶
Return a random string made up of alpha and numeric characters.
- fauxfactory.factories.strings.gen_cjk(length: int = 10, start: str | None = None, separator: str = '', bmp_only: bool = False, validator: Callable[[str], bool] | Pattern[str] | str | None = None, default: str | None = None, tries: int = 10) str[source]¶
Return a random string made up of CJK characters.
(Source: Wikipedia - CJK Unified Ideographs)
- Parameters:
length (int) – Length for random data.
start (str) – Random data start with.
separator (str) – Separator character for start and random data.
bmp_only (bool) – Include only Basic Multilingual Plane (BMP) characters (U+0000 to U+FFFF). Set to True for compatibility with tools like ChromeDriver that don’t support non-BMP characters.
- Returns:
A random string made up of CJK characters.
- Return type:
- fauxfactory.factories.strings.gen_cyrillic(length: int = 10, start: str | None = None, separator: str = '', validator: Callable[[str], bool] | Pattern[str] | str | None = None, default: str | None = None, tries: int = 10) str[source]¶
Return a random string made up of Cyrillic characters.
- fauxfactory.factories.strings.gen_html(length: int = 10, include_tags: bool = True, validator: Callable[[str], bool] | Pattern[str] | str | None = None, default: str | None = None, tries: int = 10) str[source]¶
Return a random string made up of html characters.
- fauxfactory.factories.strings.gen_iplum(words: int | None = None, paragraphs: int | None = None) str[source]¶
Return a lorem ipsum string.
If no arguments are passed, then return the entire default lorem ipsum string.
- Parameters:
- Raises:
ValueErrorifwordsis not a valid positive integer.- Returns:
A
lorem ipsumstring containing either the number ofwordsorparagraphs, extending and wrapping around the text as needed to make sure that it has the specified length.- Return type:
- fauxfactory.factories.strings.gen_latin1(length: int = 10, start: str | None = None, separator: str = '', validator: Callable[[str], bool] | Pattern[str] | str | None = None, default: str | None = None, tries: int = 10) str[source]¶
Return a random string made up of UTF-8 characters.
(Font: Wikipedia - Latin-1 Supplement Unicode Block)
- fauxfactory.factories.strings.gen_numeric_string(length: int = 10, start: str | None = None, separator: str = '', validator: Callable[[str], bool] | Pattern[str] | str | None = None, default: str | None = None, tries: int = 10) str[source]¶
Return a random string made up of numbers.
- fauxfactory.factories.strings.gen_special(length: int = 10, start: str | None = None, separator: str = '', validator: Callable[[str], bool] | Pattern[str] | str | None = None, default: str | None = None, tries: int = 10) str[source]¶
Return a random special characters string.
- fauxfactory.factories.strings.gen_string(str_type: str, length: int | None = None, validator: Callable[[str], bool] | Pattern[str] | str | None = None, default: str | None = None, tries: int = 10, **kwargs: Any) str[source]¶
Call other string generation methods.
- Parameters:
str_type (str) – The type of string which should be generated.
length (int) – The length of the generated string. Must be 1 or greater.
validator – Function or regex (str). If a function it must receive one parameter and return True if value can be used and False of another value need to be generated. If str it will be used as regex to validate the generated value. Default is None which will not validate the value.
tries – number of times validator must be called before returning default. Default is 10.
default – If validator returns false a number of tries times, this value is returned instead. Must be defined if validator is not None
- Raises:
ValueErrorif an invalidstr_typeis specified.- Returns:
A string.
- Return type:
Valid values for
str_typeare as follows:alpha
alphanumeric
cjk
cyrillic
html
latin1
numeric
utf8
punctuation
- fauxfactory.factories.strings.gen_utf8(length: int = 10, smp: bool = True, start: str | None = None, separator: str = '', validator: Callable[[str], bool] | Pattern[str] | str | None = None, default: str | None = None, tries: int = 10) str[source]¶
Return a random string made up of UTF-8 letters characters.
Follows RFC 3629.
- Parameters:
- Returns:
A random string made up of
UTF-8letters characters.- Return type:
fauxfactory.factories.structures¶
Collection of structured data generating functions.
- fauxfactory.factories.structures.gen_dict(schema: dict[str, Any], *, list_sizes: dict[str, int] | None = None, max_depth: int = 10, validator: Callable[[dict[str, Any]], bool] | None = None, default: dict[str, Any] | None = None, tries: int = 10) dict[str, Any][source]¶
Generate a dictionary based on a schema definition.
The schema is a dictionary where:
Keys are the field names in the output
Values can be:
Callable: Will be invoked to generate the value
dict: Recursively generates nested dictionary
list: Generates list of items (first element defines generator pattern)
Any other value: Used as-is (literal value)
- Parameters:
schema – Schema definition for the dictionary structure
list_sizes – Dictionary mapping field paths to list sizes. For example,
{'tags': 5, 'user.hobbies': 3}would generate 5 items for the ‘tags’ field and 3 items for the nested ‘user.hobbies’ field. Default list size is 3 if not specified.max_depth – Maximum recursion depth to prevent infinite loops. Default is 10.
validator – Optional validation function for the entire dict. Should accept a dict and return True if valid.
default – Default value if validation fails after
triesattemptstries – Number of generation attempts before returning default
- Returns:
Generated dictionary matching the schema
- Return type:
- Raises:
ValueError if schema is None or max_depth is exceeded
Example:
from fauxfactory import gen_dict, gen_alpha, gen_email, gen_integer # Simple flat schema user = gen_dict({ 'name': gen_alpha, 'email': gen_email, 'age': lambda: gen_integer(min_value=18, max_value=100), 'status': 'active', # literal value }) # Nested schema user = gen_dict({ 'name': gen_alpha, 'contact': { 'email': gen_email, 'phone': gen_alpha, }, 'tags': [gen_alpha], # list of random alpha strings }, list_sizes={'tags': 5})
- fauxfactory.factories.structures.gen_json(schema: dict[str, Any], *, indent: int | None = None, list_sizes: dict[str, int] | None = None, max_depth: int = 10, validator: Callable[[str], bool] | None = None, default: str | None = None, tries: int = 10) str[source]¶
Generate JSON string based on a schema definition.
Uses the same schema format as
gen_dict(). The generated dictionary is serialized to JSON.- Parameters:
schema – Schema definition (same format as gen_dict)
indent – JSON indentation level (None for compact output, integer for pretty-printing with that many spaces)
list_sizes – Dictionary mapping field paths to list sizes
max_depth – Maximum recursion depth to prevent infinite loops
validator – Optional validation function for the JSON string. Should accept a string and return True if valid.
default – Default value if validation fails after
triesattemptstries – Number of generation attempts before returning default
- Returns:
JSON string matching the schema
- Return type:
- Raises:
ValueError if schema is None or max_depth is exceeded
Example:
from fauxfactory import gen_json, gen_alpha, gen_email json_str = gen_json({ 'user': { 'name': gen_alpha, 'email': gen_email, } }, indent=2) # With list sizes json_str = gen_json({ 'users': [{ 'name': gen_alpha, 'email': gen_email, }] }, list_sizes={'users': 5}, indent=2)
- fauxfactory.factories.structures.gen_list(item_schema: Any, *, size: int = 3, max_depth: int = 10, validator: Callable[[list[Any]], bool] | None = None, default: list[Any] | None = None, tries: int = 10) list[Any][source]¶
Generate a list of items based on a schema definition.
The item_schema can be:
Callable: Will be invoked to generate each list item
dict: Each list item will be a dictionary generated from this schema
list: Each list item will be a list generated from this schema
Any other value: Each list item will be this literal value
- Parameters:
item_schema – Schema definition for each list item
size – Number of items to generate in the list. Default is 3.
max_depth – Maximum recursion depth to prevent infinite loops. Default is 10.
validator – Optional validation function for the entire list. Should accept a list and return True if valid.
default – Default value if validation fails after
triesattemptstries – Number of generation attempts before returning default
- Returns:
Generated list of items matching the schema
- Return type:
list[Any]
- Raises:
ValueError if max_depth is exceeded
Example:
from fauxfactory import gen_list, gen_alpha, gen_email, gen_integer # Simple list of strings tags = gen_list(gen_alpha, size=5) # Returns: ['xKjPqR', 'mNoPqR', 'aBcDeF', 'ghIjKl', 'mnOpQr'] # List of integers with lambda scores = gen_list(lambda: gen_integer(min_value=0, max_value=100), size=10) # Returns: [42, 87, 23, 56, 91, 12, 68, 34, 79, 15] # List of dictionaries users = gen_list({ 'name': gen_alpha, 'email': gen_email, 'active': True, }, size=3) # Returns: [ # {'name': 'xKjPqR', 'email': 'abc@example.com', 'active': True}, # {'name': 'mNoPqR', 'email': 'def@example.com', 'active': True}, # {'name': 'aBcDeF', 'email': 'ghi@example.com', 'active': True}, # ] # List of nested lists matrix = gen_list([lambda: gen_integer(min_value=0, max_value=9)], size=3) # Returns: [[1, 5, 8], [3, 7, 2], [9, 4, 6]] (each inner list has 3 items by default)
fauxfactory.factories.systems¶
Collection of computer systems generating functions.
- fauxfactory.factories.systems.gen_alpha(length: int = 10, start: str | None = None, separator: str = '', validator: Callable[[str], bool] | Pattern[str] | str | None = None, default: str | None = None, tries: int = 10) str[source]¶
Return a random string made up of alpha characters.
- fauxfactory.factories.systems.gen_alphanumeric(length: int = 10, start: str | None = None, separator: str = '', validator: Callable[[str], bool] | Pattern[str] | str | None = None, default: str | None = None, tries: int = 10) str[source]¶
Return a random string made up of alpha and numeric characters.
- fauxfactory.factories.systems.gen_choice(choices: Sequence[T]) T[source]¶
Return a random choice from the available choices.
- Parameters:
choices (list) – List of choices from which select a random value.
- Raises:
ValueErrorifchoicesisNoneor notIterableor adict.- Returns:
A random element from
choices.
- fauxfactory.factories.systems.gen_domain(name: str | None = None, subdomain: str | None = None, tlds: str | None = None) str[source]¶
Generate a random domain name.
- fauxfactory.factories.systems.gen_integer(min_value: int | None = None, max_value: int | None = None) int[source]¶
Return a random integer value based on the current platform.
- fauxfactory.factories.systems.gen_ipaddr(ip3: bool = False, ipv6: bool = False, prefix: Sequence[str | int] = ()) str[source]¶
Generate a random IP address.
You can also specify an IP address prefix if you are interested in local network address generation, etc.
- Parameters:
- Returns:
An IP address.
- Return type:
- Raises:
ValueErrorifprefixwould lead to no random fields at all. This means the length that triggers theValueErroris 4 for regular IPv4, 3 for IPv4 with ip3 and 8 for IPv6. It will be raised in any case the prefix length reaches or exceeds those values.
- fauxfactory.factories.systems.gen_mac(delimiter: str = ':', multicast: bool | None = None, locally: bool | None = None) str[source]¶
Generate a random MAC address.
For more information about how unicast or multicast and globally unique and locally administered MAC addresses are generated check this link https://en.wikipedia.org/wiki/MAC_address.
- Parameters:
delimiter (str) – Valid MAC delimiter (e.g ‘:’, ‘-‘).
multicast (bool) – Indicates if the generated MAC address should be unicast or multicast. If no value is provided a random one will be chosen.
locally (bool) – Indicates if the generated MAC address should be globally unique or locally administered. If no value is provided a random one will be chosen.
- Returns:
A random MAC address.
- Return type:
- fauxfactory.factories.systems.gen_netmask(min_cidr: int = 1, max_cidr: int = 31) str[source]¶
Generate a random valid netmask.
For more info: http://www.iplocation.net/tools/netmask.php
- Parameters:
- Returns:
The netmask is chosen from
fauxfactory.constants.VALID_NETMASKSrespecting the CIDR range- Return type:
- Raises:
ValueErrorifmin_cidrormax_cidrhave an invalid value. For example,max_cidrcannot be 33.