Howto use the library

Its easy to get started with the loadtestdata management command but its quite limited if you want to have more control of how your test data should be created. This chapter describes how you use the library in your python environment like the shell, a custom script or in unittests.

Creating model instances

The autofixture module contains a few shortcuts to make the creation of test data as fast as possible.

autofixture.create(model, count, *args, **kwargs)

Create count instances of model using the either an appropiate autofixture that was registry or fall back to the default:class:AutoFixture class. model can be a model class or its string representation (e.g. "app.ModelClass").

All positional and keyword arguments are passed to the autofixture constructor. It is demonstrated in the example below which will create ten superusers:

import autofixture
admins = autofixture('auth.User', 10, field_values={'is_superuser': True})

Note

See Using the AutoFixture class for more information.

create() will return a list of the created objects.

autofixture.create_one(model, *args, **kwargs)

create_one() is exactly the as the create() function but a shortcut if you only want to generate one model instance.

The function returns the instanciated model.

Using the AutoFixture class

class autofixture.base.AutoFixture(model, field_values=None, none_chance=None, overwrite_defaults=None, constraints=None, follow_fk=None, generate_fk=None, follow_m2m=None, generate_m2m=None)
__init__(model, field_values=None, none_chance=None, overwrite_defaults=None, constraints=None, follow_fk=None, generate_fk=None, follow_m2m=None, generate_m2m=None)
Parameters:

model: A model class which is used to create the test data.

field_values: A dictionary with field names of model as keys. Values may be static values that are assigned to the field, a Generator instance that generates a value on the fly or a callable which takes no arguments and returns the wanted value.

none_chance: The chance (between 0 and 1, 1 equals 100%) to assign None to nullable fields.

overwrite_defaults: All default values of fields are preserved by default. If set to True, default values will be treated like any other field.

constraints: A list of callables. The constraints are used to verify if the created model instance may be used. The callable gets the actual model as first and the instance as second parameter. The instance is not populated yet at this moment. The callable may raise an InvalidConstraint exception to indicate which fields violate the constraint.

follow_fk: A boolean value indicating if foreign keys should be set to random, already existing, instances of the related model.

generate_fk: A boolean which indicates if related models should also be created with random values. The follow_fk parameter will be ignored if generate_fk is set to True.

follow_m2m: A tuple containing minium and maximum of model instances that are assigned to ManyToManyField. No new instances will be created. Default is (1, 5). You can ignore ManyToManyField fields by setting this parameter to False.

generate_m2m: A tuple containing minimum and maximum number of model instance that are newly created and assigned to the ManyToManyField. Default is False which disables the generation of new related instances. The value of follow_m2m will be ignored if this parameter is set.

add_field_value(name, value)

Pass a static value that should be assigned to the field called name. value may be a Generator instance.

add_constraint(constraint)

Add a constraint to the autofixture.

check_constrains(instance)

Return fieldnames which need recalculation.

create(count=1, commit=True)

Create and return count model instances. If commit is False the instances will not be saved and many to many relations will not be processed.

May raise CreateInstanceError if constraints are not satisfied.

The method internally calls create_one() to generate instances.

create_one(commit=True)

Create and return one model instance. If commit is False the instance will not be saved and many to many relations will not be processed.

May raise CreateInstanceError if constraints are not satisfied.

Subclassing AutoFixture

The following methods may be overwritten by subclasses:

AutoFixture.prepare_class()

This method is called after the __init__() method. It has no semantic by default.

AutoFixture.post_process_instance(instance)

Overwrite this method to modify the created instance at the last possible moment. It gets the generated instance and must return the modified instance.

AutoFixture.get_generator(field)

Return a value generator based on the field instance that is passed to this method. This function may return None which means that the specified field will be ignored (e.g. if no matching generator was found).

Table Of Contents

Previous topic

The loadtestdata management command

Next topic

The AutoFixture registry

This Page