FauxFactory¶
FauxFactory generates random data for your automated tests easily!
There are times when you’re writing tests for your application when you need to pass random, non-specific data to the areas you are testing. For these scenarios when all you need is a random string, numbers, dates, times, email address, IP, etc, then FauxFactory can help!
Installation¶
FauxFactory is available in PyPi and can be installed using pip:
$ pip install fauxfactory
You can install FauxFactory by downloading the latest version of the source code:
$ git clone git@github.com:omaciel/fauxfactory.git
$ cd fauxfactory
$ uv sync
$ uv pip install .
Usage¶
Need a 15 character string for one of your tests?
>>> string = fauxfactory.gen_string('alphanumeric', 15)
>>> string.isalnum()
True
>>> len(string)
15
Need a 5 character numeric string?
>>> string = fauxfactory.gen_string('numeric', 5)
>>> string.isnumeric()
True
>>> len(string)
5
Now, let’s say you need a random date:
>>> import datetime
>>> isinstance(fauxfactory.gen_date(), datetime.date)
True
>>> isinstance(fauxfactory.gen_datetime(), datetime.datetime)
True
Or a fake email with your company domain:
>>> email = fauxfactory.gen_email(domain='mycompany')
>>> '@mycompany' in email
True
Simple, right?
Structure Generators¶
FauxFactory also provides powerful structure generators for creating complex nested data:
Generate dictionaries from schemas:
>>> user = fauxfactory.gen_dict({
... 'name': fauxfactory.gen_alpha,
... 'email': fauxfactory.gen_email,
... 'age': lambda: fauxfactory.gen_integer(min_value=18, max_value=100),
... 'active': True,
... })
>>> isinstance(user, dict)
True
>>> '@' in user['email']
True
>>> 18 <= user['age'] <= 100
True
Generate JSON strings:
>>> import json
>>> json_str = fauxfactory.gen_json({
... 'user': {
... 'name': fauxfactory.gen_alpha,
... 'verified': fauxfactory.gen_boolean,
... }
... }, indent=2)
>>> data = json.loads(json_str)
>>> isinstance(data['user']['name'], str)
True
Generate lists directly:
>>> tags = fauxfactory.gen_list(fauxfactory.gen_alpha, size=5)
>>> len(tags)
5
>>> all(isinstance(tag, str) for tag in tags)
True
Generate lists of dictionaries:
>>> users = fauxfactory.gen_list({
... 'name': fauxfactory.gen_alpha,
... 'email': fauxfactory.gen_email,
... }, size=3)
>>> len(users)
3
>>> all('@' in user['email'] for user in users)
True
Validation¶
All string functions allow validation of inputs using 3 parameters:
validator: a callable or str with regex returning boolean signaling if random data is valid or not.
tries: maximum number of times random data will be generated after failing validation. If the limit is reached “default” parameter will be returned.
default: value to be returned if validation fails a “tries” number of times.
Example using callable:
>>> def start_a(value):
... return value[0] == 'a'
>>> email = fauxfactory.gen_email(validator=start_a, default = 'a@b.c')
>>> email[0] == 'a'
True
Example using regex:
>>> n = fauxfactory.gen_string(
... 'numeric', validator='[^0].*', default = '2')
>>> n != '0'
True
Example using tries and default:
>>> def always_false(value):
... print('Executed')
... return False
>>> fauxfactory.gen_alpha(
... validator=always_false, default = 'default value', tries=1)
Executed
'default value'
>>> fauxfactory.gen_alpha(
... validator=always_false, default = 'default value 2', tries=3)
Executed
Executed
Executed
'default value 2'
API¶
For a full list of available methods, see the API documentation.
Contribute¶
Fork the repository on GitHub and make some changes. Make sure to add yourself to AUTHORS.
Install the development requirements.
pip install -r requirements-optional.txt.Test your changes.
Run
make test-alland make sure nothing has broken.Run
coverage report --show-missingto check for untested code.Add tests to the
tests/directory if appropriate.
Repeat this cycle as needed.
Send a pull request and bug the maintainer until it gets merged and published. :)