Talk at DjangoCon EU 2022 about best practices using factory_boy.

https://pretalx.evolutio.pt/djangocon-europe-2022/talk/XWUYA8/



Details

After working using the factory_boy library for over 3 years on a Django monolith containing +230 tables, +2200 relevant files, and +75k relevant lines of code, I’ve collected and listed all my biggest learnings.

In this presentation, I try to share some of my experiences, showing what I consider to be good practices in using factory_boy in complex systems.

Content:

  • A short introduction of myself factory_boy: what is it? - the definition and the purpose of the tool
  • Example application - a quick overview of the application we will use to demonstrate the best practices
  • How to install factory_boy
  • How to use factory_boy
  • Best Practices:
    1. Factories must represent their models
    2. Do not rely on defaults from factories
    3. Factories must have only the required data. if the field is nullable -> under traits.
    4. Build over create
    5. If FK is in the table -> SubFactory. If FK is in the other table -> RelatedFactory + trait
    6. Fixtures only to wrap factories in the test file
    7. Avoid using fixtures on shared files like conftest
  • Wrapping up

Repository: github.com/camilamaia/factory-boy-best-practices