Skip to main content

AutoFactoryBoy generates factories for you.

Project description

AutoFactoryBoy

Build Status

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:

  1. Subclass a DjangoModelAutoFactory:

    from autofactory import DjangoModelAutoFactory
    
    from models import Model
    
    class ModelFactory(DjangoModelAutoFactory):
        class Meta:
            model = Model
            fields = "__all__"
    
    model = ModelFactory.create()
    
  2. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

autofactory-0.1.0.tar.gz (6.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page