factory_boy: testing like a pro

Oct 17 1:20 PM PDT :calendar: to 2:05 pm
Audience level: Intermediate

About This Talk

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.


  • 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: https://github.com/camilamaia/factory-boy-best-practices


    Photo of Camila Maia

    Camila Maia (she/her)

    I am a Brazilian software developer that loves to architect systems, create new DevTools, and automate processes. I am a backend developer that loves developing systems using Python or Ruby. 🐍 💎

    I am truly passionate about sharing knowledge and connecting with people. For the last couple of years, I’ve been focusing increasingly on open source. Open source is where I could find the perfect fit between people connection and coding in my day-to-day. As a result of this effort, my GitHub profile was the first one to be accepted for the GitHub Sponsors program in Brazil, which made me incredibly happy.

    Finally, I am an open-source advocate that does believe in the power of tech communities. People > Tech 💜