AutoFactoryBoy generates factories for you.
Project description
AutoFactoryBoy
Warning! Current version of AutoFactoryBoy supports only Django backend.
AutoFactoryBoy introspects Django's models and generates a factory with all not blank fields.
Quickstart
There are a couple of options to create an AutoFactory
for a model:
-
Subclass a
DjangoModelAutoFactory
:from autofactory import DjangoModelAutoFactory from models import Model class ModelFactory(DjangoModelAutoFactory): class Meta: model = Model fields = "__all__" model = ModelFactory.create()
-
Make a factory right from the model with the help of a shortcut:
from autofactory import autofactory from models import Model model_factory = autofactory(Model) model = model_factory.create()
Q & A
How do I specify fields to generate automatically?
AutoFactoryBoy will generate a ModelFactory
for a model with fields,
specified in the ModelFactory.Meta
:
class ModelFactory(DjangoModelAutoFactory):
class Meta:
model = Model
fields = ("integer", "string")
The code snippet above is identical to:
class ModelFactory(DjangoModelFactory):
integer = factory.Faker("pyint")
string = factory.Faker("text")
class Meta:
model = Model
How do I make an autofactory with all model fields?
You can set fields
to a special value (i.e. __all__
) and all fields with
blank=False
will be generated automatically:
# models.py
class Model(models.Model):
integer = models.IntegerField(blank=True, null=True)
string = models.CharField()
# factories.py
class ModelFactory(DjangoModelAutoFactory):
class Meta:
model = Model
fields = "__all__"
The code snippet above is identical to:
class ModelFactory(DjangoModelFactory):
string = factory.Faker("text")
class Meta:
model = Model
Testing
To test, run:
$ make test # Current environment
$ make test-tox # All tox environments
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.