factory_boy icon indicating copy to clipboard operation
factory_boy copied to clipboard

Difference factory_boy vs model bakery?

Open lggwettmann opened this issue 5 years ago • 4 comments

Hey guys,

why would I use factory boy over model bakery or vice versa? Could anyone shed some light on the similarities and differences? I tried to look it up but really can't anything in either docs and also not on the internets.

Your help is greatly appreciated.

lggwettmann avatar May 31 '20 16:05 lggwettmann

Hi,

factory-boy helps you provide a recipe for creating realistic objects. For instance, if your model is:

class User:
    fullname: str
    shortname: str
    email: str
    password: str
    is_admin: bool

You could write a factory as follow:

class UserFactory(factory.Factory):
    class Meta:
        model = User
    fullname = factory.Faker('name')
    shortname = factory.Faker('first_name')
    email = factory.Faker('email')
    password = factory.Faker('pystr')
    is_admin = False

factory_boy takes the position that "Explicit is better than implicit", and encourages you to provide a domain-relevant definition of fields (for instance, having a realistic-looking full name instead of a random string of digits). This focus pushed us to add many declarations and helpers to help you define clearly the relationships between your models, including adding your own custom logic if needed - maybe hooking in some of your actual business code.

You could, for instance, derive a AdminUserFactory() from the simpler UserFactory() above, which would toggle the is_admin flag and add the user to a set of groups, and maybe add a "user promotion" record somewhere.

Moreover, factory_boy isn't limited to Django: you could use it to craft HTTP requests or JSON payloads.

If I had to sum up the difference:

  • model-bakery focuses on giving you a DB-valid Django instance with minimal work;
  • factory_boy gives you the tools to define what a business-realistic object looks like — and provides helpers for your tests.

rbarrois avatar Aug 21 '20 08:08 rbarrois

As I understand, Faker can be used with Model Bakery too? For those fields where we do need realistic values, we can use it, right?

karina-klinkeviciute avatar May 08 '21 15:05 karina-klinkeviciute

@karina-klinkeviciute From 10 minutes look at model_bakery I think that factory boy API is much more intuitive to use.

@rbarrois Speaking of intuitive API I do not like the Faker wrapper provided by factory boy because it's hiding object attributes behind strings and once faker will finally provide stubs IDEs will be able to whisper the methods provided. eg. Faker().n -ctrl+space> Faker().name() - You are most likely not able to achieve something like this with plaintext arguments thouhg.

1oglop1 avatar Sep 03 '21 10:09 1oglop1

@1oglop1 indeed, I'm not fully satisfied with the factory.Faker wrapper. However, since Faker allows users to dynamically register providers, it's perfectly possible to have extra callables that aren't listed on Faker's stubs... This needs further thought!

rbarrois avatar Sep 03 '21 15:09 rbarrois