I don't see why factory_boy is preferred over creating ORM/model instances directly in Django tests. And the factory_boy website does little to explain the benefits of using it.
It makes sense as an alternative to fixtures, which are difficult to manage, are slow, etc. etc.
But, why not just create model instances as needed for the tests?
If factory_boy completely replaced writing to the db, then fine, I think it'd be very useful in this case, but the the factory boy created django model instances still interact with the database.
Another potential benefit is the support for sequences, but it is not hard to create sequences / sample data without the need for factory boy.
All in all I'm seeing virtually no benefits in using factory boy, vs creating objects/model instances directly.
I hope I'm missing something glaringly obvious!
Yes, you can prepare your test data by using django ORM directly. But there are advantages of using factories and factory_boy
specifically, here are some of that I remember and use:
your model factories are defined in a nice, clean and readable manner:
class CasesFactory(factory.Factory):
FACTORY_FOR = models.Case
number = factory.Sequence(lambda n: '1021-{0}'.format(n))
create_date = datetime.datetime.now()
another benefit of this class-based approach is the ability to create SubFactories
also you can easily define factories for different kind of relationships: ForeignKey
, reverse ForeignKey
, ManyToMany
(documentation)
DjangoModelFactory
classSequence
s (as you've mentioned) helps make the data more "dynamic". Imagine handling it yourself.mute_signals
decorator - sometimes while testing you don't want the signal to be dispatchedBasically, factory_boy
is there to avoid writing "helper" functions for generating test data. Instead, it introduces a nice and easy-to-use interface to it.
Ask yourself: why reinvent the wheel is there is a tool specifically for the job?
Also see:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With