question on post_generation

38 views
Skip to first unread message

David Binetti

unread,
Nov 22, 2017, 9:08:03 AM11/22/17
to facto...@googlegroups.com
I'm using post_generation decorators to create related records on
parent object create. This is very useful for a database seeding
script I'm employing when testing locally.

However, it also is slowing down my unit tests tremendously, because
it is creating all the sub-objects (even though they are not needed
for the unit tests themselves.)

Is there a way to disable the post_generation method for these unit
tests? OR am I employing an anti-pattern?

Thanks.

Raphaël Barrois

unread,
Nov 22, 2017, 10:35:01 AM11/22/17
to David Binetti, facto...@googlegroups.com
Hi,

You can disable a specific post_generation hook by overloading it with another post_generation hook:

@factory.post_generation
def noop(obj, create, extracted, **kwargs):
pass

MyFactory(some_post_gen=noop)


Once https://github.com/FactoryBoy/factory_boy/pull/433 is merged and released, you could also use a `Trait` to enable
all post-generation hooks based on a flag:

class MyFactory:
class Params:
deep = factory.Trait(
x=factory.RelatedFactory(SomeOtherFactory, y=1),
)

--
Raphaël

David Binetti

unread,
Nov 24, 2017, 12:11:40 PM11/24/17
to Raphaël Barrois, facto...@googlegroups.com
thanks! however, at least when using `pytest` this doesn't seem to
work. the post_generation hook is still being called. thoughts on
why?

Raphaël Barrois

unread,
Nov 26, 2017, 12:21:16 PM11/26/17
to David Binetti, facto...@googlegroups.com
Hi,

That's weird; have you tried running the code directly without pytest (e.g in a simple Python shell)?

If you have a minimal reproductible setup, the best option would be to open an issue on https://github.com/FactoryBoy/factory_boy/issues ;)

Thanks,

--
Raphaël

On Fri, 24 Nov 2017 09:11:39 -0800

David Binetti

unread,
Nov 27, 2017, 8:38:08 AM11/27/17
to Raphaël Barrois, facto...@googlegroups.com
I did run in the shell, and it worked as expected.

I don't have a clue how to set up a test case itself, but I will open
an issue with a description. thanks

On Sun, Nov 26, 2017 at 9:21 AM, Raphaël Barrois

David Binetti

unread,
Nov 27, 2017, 8:39:00 AM11/27/17
to Raphaël Barrois, facto...@googlegroups.com
actually, scratch that -- I'll just wait for the Trait feature.
Reply all
Reply to author
Forward
0 new messages