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
Unit testing in python
Search
Gabriel Falcão
February 06, 2012
Programming
6
700
Unit testing in python
motivation, techniques and tools
Gabriel Falcão
February 06, 2012
Tweet
Share
More Decks by Gabriel Falcão
See All by Gabriel Falcão
Testing in Django
gabrielfalcao
8
690
Other Decks in Programming
See All in Programming
Composerが「依存解決」のためにどんな工夫をしているか #phpcon
o0h
PRO
1
250
地方に住むエンジニアの残酷な現実とキャリア論
ichimichi
5
1.5k
「テストは愚直&&網羅的に書くほどよい」という誤解 / Test Smarter, Not Harder
munetoshi
0
170
Node-RED を(HTTP で)つなげる MCP サーバーを作ってみた
highu
0
120
生成AI時代のコンポーネントライブラリの作り方
touyou
1
210
5つのアンチパターンから学ぶLT設計
narihara
1
170
プロダクト志向なエンジニアがもう一歩先の価値を目指すために意識したこと
nealle
0
130
“いい感じ“な定量評価を求めて - Four Keysとアウトカムの間の探求 -
nealle
1
10k
Systèmes distribués, pour le meilleur et pour le pire - BreizhCamp 2025 - Conférence
slecache
0
120
Is Xcode slowly dying out in 2025?
uetyo
1
270
10 Costly Database Performance Mistakes (And How To Fix Them)
andyatkinson
0
330
Webの外へ飛び出せ NativePHPが切り拓くPHPの未来
takuyakatsusa
2
550
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.5k
Raft: Consensus for Rubyists
vanstee
140
7k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.7k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
181
54k
The Cult of Friendly URLs
andyhume
79
6.5k
We Have a Design System, Now What?
morganepeng
53
7.7k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.7k
Code Review Best Practice
trishagee
69
18k
Git: the NoSQL Database
bkeepers
PRO
430
65k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
126
53k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
2.9k
Transcript
Unit testing motivation, techniques and tools
Past •Tests were written after the code was “ready” •Mocks,
Stubs, are also an old concept: isolate modules •Microsoft, Sun and other huge software companies were using since... ever
Hey wait... Microsoft Testing after written? +
coincidence?
Present •Kent Back changed the world •Google, Facebook, [your badass
company here] doing •Mocks, Stubs, now are less heavily used
Why is it cool ? •Automagic refactoring •Modules are isolated
•Code is cleaner •Modules are isolated •No more fear when changing code that is too important •They run just so fast •White box
Wait.... white box?!
White box •Comes from hacking/security vocabulary •The developer knows the
codebase in depth •Test relies on internal calls
Test-driven development How to start it
Baby steps
Baby steps •Don’t write the whole thing at once •The
next step will come naturally •Small units of code •The name is cute
The Mantra Red (1) Green (2) Refactor (3)
... in other words
YAGNI YOU AIN’T GONNA NEED IT ...at least for now.
YAGNI
YAGNI Focus on what you need to achieve now don’t
try to predict the future
Python Past/present/future?
Python - Past •unittest and unittest2 •class-based •camel case •overcomplicated
•the community wasn’t really into it •... so we didn’t have many libraries BORING...
Python - Past
None
Python - Present •Nose — •DSLs are growing and becoming
more popular •it is becoming fun to write tests •so that we’ll want to write more and more ROBUST CODE
Python - Present •Easier to write •conventions instead of classes
•libraries leveraging mocks and stubs
Old-school mocking and asserting
But now we are hipsters
and cool libraries does the hard job
What about stubs? •They’re just like mocks •But doesn’t have
behavior (no methods, just info)
What about stubs?
Future?!
Future?!
Future?!
Future?!
Django let’s have some fun
How it is •The default test support uses unittest2 •or
doctests •don’t separate between unit, functional, etc •which leverages having very slow suites •doesn’t provide support for isolation •kinda focused on black box
Unclebob •per app tests (unit, functional, integration) •isolate the database
so it fails if your test tries to use it •discover tests automagically
Unclebob http://bit.ly/woTgXq
testing huge views
None
None
the idea is simple
move logic into objects, testing each unit of logic separately
links •http://readthedocs.org/docs/nose/en/latest/ •http://github.com/gabrielfalcao/sure •http://github.com/gabrielfalcao/unclebob