We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
factory_boy: testing like a pro with Camila Maia - DjangoCon US 2022
Discover how to test like a pro with factory_boy and Camila Maia at DjangoCon US 2022. Learn best practices for creating complex objects, controlling creation, and ensuring maintainable tests.
- Use factories not to create complex objects, but to allow explicit control over the creation
-
Avoid using
create
in factories, as it hits the database, usebuild
instead - Have separate factories for each model, relationships and sub-factories
- Factory should contain only the required data to avoid errors and duplication
- Avoid duplicating code and use sub-factories to create complex objects
- Use fixures only when necessary, avoiding inflation of the fixture
- Separate the fixture from the factory to easier maintainability
- Use Faker in the factory to create fake data
- Pass in the required data to the create method to avoid implicit errors
- Fix errors by explicitly defining tests
-
Use the
related_f
actory to create related objects - Reflect database relationships in the factory
-
In tests, use
assert
. For example,assert object.email == '[email protected]'.