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
Camila Maia
September 22, 2022
Technology
1
660
factory_boy: testing like a pro. DjangoCon EU 2022
https://pretalx.evolutio.pt/djangocon-europe-2022/talk/XWUYA8/
Camila Maia
September 22, 2022
Tweet
Share
More Decks by Camila Maia
See All by Camila Maia
GitHub Actions - Semana DevOps LINUXTips.pdf
camilamaia
0
29
Minha Jornada em Developer Experience (DX) - DevOpsDays Salvador 2024
camilamaia
0
23
Cumbuca Dev: Ensinando com Open Source - 71º Python Floripa no MeLi
camilamaia
0
56
COMO E PORQUE ENSINAR A PROGRAMAR COM OPEN SOURCE - Python Brasil 2023
camilamaia
0
46
Como testar e manter APIs de qualidade - Codecon Digital 2023
camilamaia
0
1k
factory_boy: testing like a pro. DjangoCon US 2022
camilamaia
0
650
ScanAPI Workshop. DjangoCon EU 2022
camilamaia
0
580
Como testar e documentar REST APIs com ScanAPI - Agile Testers Conference 2021
camilamaia
0
290
factory_boy: Testando Objetos Complexos Python Brasil 2021
camilamaia
0
380
Other Decks in Technology
See All in Technology
GitHub Copilot の概要
tomokusaba
1
150
Node-REDのFunctionノードでMCPサーバーの実装を試してみた / Node-RED × MCP 勉強会 vol.1
you
PRO
0
130
CursorによるPMO業務の代替 / Automating PMO Tasks with Cursor
motoyoshi_kakaku
2
780
20250625 Snowflake Summit 2025活用事例 レポート / Nowcast Snowflake Summit 2025 Case Study Report
kkuv
1
370
使いたいMCPサーバーはWeb APIをラップして自分で作る #QiitaBash
bengo4com
0
1.2k
生成AIで小説を書くためにプロンプトの制約や原則について学ぶ / prompt-engineering-for-ai-fiction
nwiizo
4
3.4k
KubeCon + CloudNativeCon Japan 2025 Recap Opening & Choose Your Own Adventureシリーズまとめ
mmmatsuda
0
230
さくらのIaaS基盤のモニタリングとOpenTelemetry/OSC Hokkaido 2025
fujiwara3
2
240
急成長を支える基盤作り〜地道な改善からコツコツと〜 #cre_meetup
stefafafan
0
150
本が全く読めなかった過去の自分へ
genshun9
0
710
作曲家がボカロを使うようにPdMはAIを使え
itotaxi
0
380
KubeCon + CloudNativeCon Japan 2025 に行ってきた! & containerd の新機能紹介
honahuku
0
120
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
231
18k
How STYLIGHT went responsive
nonsquared
100
5.6k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.3k
How GitHub (no longer) Works
holman
314
140k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
Practical Orchestrator
shlominoach
188
11k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.6k
Speed Design
sergeychernyshev
32
1k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Designing for humans not robots
tammielis
253
25k
Building Applications with DynamoDB
mza
95
6.5k
Music & Morning Musume
bryan
46
6.6k
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