Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
factory_boy: testing like a pro. DjangoCon EU 2022
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Camila Maia
September 22, 2022
Technology
980
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
Transcript
84 slides
@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
More Decks by Camila Maia
See All by Camila Maia
Infraestrutura não é caridade - Keynote Python Brasil 2026
camilamaia
0
24
GitHub Actions - Semana DevOps LINUXTips.pdf
camilamaia
0
69
Minha Jornada em Developer Experience (DX) - DevOpsDays Salvador 2024
camilamaia
0
73
Cumbuca Dev: Ensinando com Open Source - 71º Python Floripa no MeLi
camilamaia
0
98
COMO E PORQUE ENSINAR A PROGRAMAR COM OPEN SOURCE - Python Brasil 2023
camilamaia
0
100
Como testar e manter APIs de qualidade - Codecon Digital 2023
camilamaia
0
1.3k
factory_boy: testing like a pro. DjangoCon US 2022
camilamaia
0
980
ScanAPI Workshop. DjangoCon EU 2022
camilamaia
0
880
Como testar e documentar REST APIs com ScanAPI - Agile Testers Conference 2021
camilamaia
0
500
Other Decks in Technology
See All in Technology
AIが会計業務の伴走者に。 マネーフォワード クラウド会計MCPサーバーで実現するAI前提の新しい業務プロセス
nobitabashi
0
120
Kiro Crew の Bot (続く役目を任せる窓口) | 目指せ Kiro マスター 12
inariku
PRO
2
380
DBTS2026 盛り上がってますか!?Databricksが実現するAI時代のデータ基盤
akuwano
1
200
大江戸Ruby会議でデータベース クライアントの話をするのは 間違っているだろうか
yahonda
1
120
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
17
120k
アラートを見直していたら営業組織への理解にちょっとだけ繋がった話
z63d
1
190
BALLAS Technology Deck
ballas_inc
0
590
AWS Quick 入門 | 目指せ Quick マスター 01
inariku
PRO
2
340
異夢同船〜異なる夢でも同じ船でなんとかする〜
fortegp05
0
180
「任されビリティ」で引き受けて「よしなりてぃ」に頼らず任せる / Accept tasks with "makasarebility" and hand them over without relying on "yoshinarily"
pauli
0
380
ラクスル会社紹介資料|エンジニア向け / RAKSUL Introduction for Engineers
raksulrecruiting
0
100
【Oracle AI SPOTLIGHT ウェビナー】【特別開催:日本語で解説!】Oracle AI World 2026 最新情報フィードバック
oracle4engineer
PRO
1
210
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
433
67k
The SEO identity crisis: Don't let AI make you average
varn
0
630
Google's AI Overviews - The New Search
badams
0
1.7k
Music & Morning Musume
bryan
48
7.5k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.3k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
2
400
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
Writing Fast Ruby
sferik
630
63k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
330
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.4k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.8k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.2k