Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
factory_boy: testing like a pro. DjangoCon EU 2022
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Camila Maia
September 22, 2022
Technology
950
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
factory_boy: testing like a pro. DjangoCon EU 2022
https://pretalx.evolutio.pt/djangocon-europe-2022/talk/XWUYA8/
Camila Maia
September 22, 2022
More Decks by Camila Maia
See All by Camila Maia
GitHub Actions - Semana DevOps LINUXTips.pdf
camilamaia
0
57
Minha Jornada em Developer Experience (DX) - DevOpsDays Salvador 2024
camilamaia
0
62
Cumbuca Dev: Ensinando com Open Source - 71º Python Floripa no MeLi
camilamaia
0
89
COMO E PORQUE ENSINAR A PROGRAMAR COM OPEN SOURCE - Python Brasil 2023
camilamaia
0
89
Como testar e manter APIs de qualidade - Codecon Digital 2023
camilamaia
0
1.2k
factory_boy: testing like a pro. DjangoCon US 2022
camilamaia
0
950
ScanAPI Workshop. DjangoCon EU 2022
camilamaia
0
830
Como testar e documentar REST APIs com ScanAPI - Agile Testers Conference 2021
camilamaia
0
470
factory_boy: Testando Objetos Complexos Python Brasil 2021
camilamaia
0
560
Other Decks in Technology
See All in Technology
初めてのGitHub Actions / GitHub Actions at First
tooppoo
0
120
書籍セキュアAPIについて
riiimparm
0
400
データエンジニアこそ組織のオントロジーに向き合うべき — 問いに答えるAIから、事業を動かすAIへ
gappy50
7
3k
Power Automateアップデート情報
miyakemito
0
290
WEBフロントエンド研修【MIXI 26新卒技術研修】
mixi_engineers
PRO
2
900
13年運用タイトルのサーバーサイドが辿り着いた現在地 ― モンスターストライクにおける技術・組織・AI活用から得た知見
mixi_engineers
PRO
1
380
【CEDEC2026】ゲームシナリオライターを支援するAIツール開発の実践 ― 設計とプロンプトの工夫 ―
cygames
PRO
1
160
LangfuseによるLLMOps基盤の構築と活用事例
zozotech
PRO
1
220
AIQAのナレッジ構築について
qatonchan
1
140
AIで楽になるはずが、なぜ疲れる?
kinopeee
0
150
Webの技術とガジェットで子どもも大人も楽しめるワクワク体験を提供する / Qiita Tech Festa Day 2026
you
PRO
1
330
なぜ、あなたのAPIは使われないのか? AX時代の設計原則、ガードレール、運用体制
yokawasa
1
1.2k
Featured
See All Featured
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
620
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.4k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
360
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
160
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Designing for humans not robots
tammielis
254
26k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.5k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Prompt Engineering for Job Search
mfonobong
0
390
Transcript
@cmaiacd factory_boy DjangoCon EU 2022 - Porto Camila Maia
testing like a pro
@cmaiacd You can find this presentation at: speakerdeck.com/cmaiacd
@cmaiacd Who am I?
@cmaiacd Backend Developer @
@cmaiacd Brazilian
@cmaiacd Living in Berlin
@cmaiacd Queer
@cmaiacd 🐶
@cmaiacd Coding since 2010 👵
@cmaiacd Python 🐍 and Ruby 💎
@cmaiacd Community ❤
@cmaiacd Conferences
@cmaiacd Open Source ❤
@cmaiacd Creator of ScanAPI
@cmaiacd +1.5k + 1.2k ⭐
@cmaiacd Workshop Tomorrow 4:55 PM
@cmaiacd
@cmaiacd factory_boy: what is it?
@cmaiacd It is a fixtures replacement
@cmaiacd Based on factory_bot (Thoughtbot)
@cmaiacd First version: Django only Nowadays: framework-independent Unittest, Pytest...
@cmaiacd For complex objects: ❌ Fixtures: static, hard to maintain
✅ Factories: easy-to-use
@cmaiacd
@cmaiacd 🧰 🛠 🔧 ⚙ Sequence Faker Fuzzy attributes LazyFunction
LazyAttribute Inheritance Inheritance Params Traits Strategies RelatedFactory / SubFactory
@cmaiacd My Experience
@cmaiacd +3 years Django Monolith 🐘
@cmaiacd +230 tables +2200 relevant files +75k relevant lines
@cmaiacd
@cmaiacd A poorly designed factory might affect many tests
@cmaiacd Implicit errors
@cmaiacd The tests are created in a way to fit
the factory factory-oriented testing
@cmaiacd Factories can get too tied
@cmaiacd Developers bump into the same issue again and again
@cmaiacd Patterns 👀 Best practices? 🤔
@cmaiacd Demo App
@cmaiacd Polls
@cmaiacd Poll
@cmaiacd Results
@cmaiacd s
@cmaiacd Model - Poll Poll Question Choice n:1 pub_date :
DateTimeField premium : BooleanField author : CharField question_text : CharField language : CharField choice_text : CharField votes : IntegerField 1:1
@cmaiacd Model - Poll
@cmaiacd Model - Poll
@cmaiacd Model - Question
@cmaiacd Model - Choice
@cmaiacd Best Practices
@cmaiacd 1. FACTORIES SHOULD REPRESENT THEIR MODELS
@cmaiacd Avoid implicit errors
@cmaiacd BAD ❌
@cmaiacd GOOD ✅
@cmaiacd 2. DO NOT RELY ON DEFAULTS FROM FACTORIES
@cmaiacd • If a default value is changed, all tests
that depend on it will break • The setup of a test should contain all the logic to ensure it will always pass • Explicit better than implicit
@cmaiacd BAD ❌
@cmaiacd GOOD ✅
@cmaiacd 3. FACTORIES SHOULD CONTAIN ONLY THE REQUIRED DATA
@cmaiacd If the field is nullable (null=True) the attribute should
be under a trait and not as a default value
@cmaiacd BAD ❌
@cmaiacd GOOD ✅
@cmaiacd If we want to have an author, we can
use PollFactory(with_author=True) now When are we going to remember to test the case PollFactory(author=None)? We should not assume there is an author when DB actually allows to not have it.
@cmaiacd 4. BUILD OVER CREATE
@cmaiacd MyFactory.build() creates a local object (memory) MyFactory.create() creates a
local object + stores it in the DB
@cmaiacd BAD ❌
@cmaiacd GOOD ✅
@cmaiacd BUILD STRATEGY ========== 14 passed in 1.76 seconds ==========
CREATE STRATEGY ========== 14 passed in 3.26 seconds ==========
@cmaiacd 5. IF FK IS IN THE TABLE: SUBFACTOR IF
FK IS IN THE OTHER TABLE: RELATEDFACTORY + TRAIT
@cmaiacd SubFactory: builds/creates the SubFactory during the process of creation
of the main factory RelatedFactory: builds/creates the RelatedFactory after creating the main factory
@cmaiacd Good ✅
@cmaiacd 6. USE FIXTURES TO WRAP FACTORIES TO AVOID DUPLICATION
@cmaiacd BAD ❌
@cmaiacd GOOD ✅
@cmaiacd 7. AVOID SHARING FACTORIES OR FIXTURES AMONG DIFFERENT FILES
@cmaiacd Many tests depending on the same factory/fixture
@cmaiacd Tends to inflate the factory/fixture
@cmaiacd Hard to maintain
@cmaiacd Change a factory/fixture… tons of tests breaking
@cmaiacd Fixture / Factory oriented testing
@cmaiacd Ok, ok, I got it! 😌 Now I know
the best practices 💪
@cmaiacd So let’s try to fix one first factory… 🤓
@cmaiacd
@cmaiacd Baby steps 🐣
@cmaiacd Código
@cmaiacd Official doc 📄 factoryboy.readthedocs.io
@cmaiacd Common recipes factoryboy.readthedocs.io/ en/stable/recipes.html
@cmaiacd Code github.com/FactoryBoy/ factory_boy
@cmaiacd THANK YOU! MUITO OBRIGADA! @cmaiacd camilamaia cmaiacd.com