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
Unit Test Introduction.pdf
Search
hatelove
April 30, 2012
Programming
7.5k
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Unit Test Introduction.pdf
單元測試簡介以及mock的意義
hatelove
April 30, 2012
More Decks by hatelove
See All by hatelove
1. 在沙箱中開發
hatelove
12
3.1k
BDD in .NET - TDD 在實務上的最後一塊拼圖
hatelove
4
3k
OO training 基本原則整理
hatelove
2
5.3k
OO training homework 3 review and summary
hatelove
1
11k
OO training homework 2 review and summary
hatelove
1
4.7k
OO training homework 1 review and summary
hatelove
1
8.1k
Object Oriented Training - Session 4
hatelove
1
5.2k
Object Oriented Training - Session 3
hatelove
1
11k
Object Oriented Training - Session 2
hatelove
1
4.9k
Other Decks in Programming
See All in Programming
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
19
19k
iOSDC Japan 2026 - Swiftで作って学ぼう!データベース自作入門
kaseken
2
400
モデルのリファクタリングが難しいと思ったら、そもそも複雑だったのはビジネス仕様だった ? / is-the-business-domain-the-real-complexity
hatsu38
0
430
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
570
Ghostty + Neovimで作る 透明でカッコ良い開発環境
j341nono
0
150
技術的負債を組織課題として解く-増えすぎたマイクロサービスとの戦い-
reimaru
1
2.5k
Turning Architecture into Unit Tests in the AI Era (NSSpain XIV)
steliosf
PRO
1
120
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.3k
JPUG勉強会 OSSデータベースの内部構造を理解しよう(第2回)
oga5
0
280
一人だけ、Kiroが静止する日
hideg
0
140
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
100
AHC070解法紹介
eijirou
0
140
Featured
See All Featured
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
600
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
390
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.3k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
How to Talk to Developers About Accessibility
jct
2
560
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.8k
Speed Design
sergeychernyshev
33
2.1k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
66k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.4k
エンジニアに許された特別な時間の終わり
watany
109
250k
[SF Ruby Conf 2025] Rails X
palkan
3
1.4k
For a Future-Friendly Web
brad_frost
183
10k
Transcript
Joey Chen 2011/7/6 Unit Test Introduction
- 2 - 常見的問題 開發常 見問題 要到真實環境 方能測試程式 無誤 頁面發生錯誤,
到底是誰錯了? 交付的程式, 到底測過哪些 東西了? 我改了這支程 式,會不會害 別的程式掛掉? 怎麼讓UI, Service, Data Access平行開 發?
- 3 - 解決方式 Unit Test Unit Test使用 mock object
來模擬外部回 傳的資料 把input值當做 test case,跑 一次Unit Test 交付的程式, 包括Unit Test 程式碼 改完程式就跑 一次Unit Test 吧 Unit Test中使 用mock object,達到 關注點分離 模擬真實環境 針對案例,所有 單元可獨立測試 明確知道哪些程 式碼有被測到 迴歸測試保護 遵循介面
- 4 - 什麼是Unit Test Unit Test的定 義 最小的測試單位 外部相依性為零
不具備邏輯 測試案例之間 相依性為零 一個測試案例 只測一種方法
- 5 -
- 6 - 如何撰寫Unit Test
Demo
- 8 - 我的程式沒辦法測? • 為什麼我的console跟web site沒辦法跑Unit Test? • 為什麼我沒法子決定expect跟目標return的值?
• 別人的service或DB還沒準備好,我沒法測?
- 9 - • 測試目標需封裝成DLL • 測試方法內容,需不相依於外部類別 • 只能測試Public的類別方法 Unit
Test的前提 測試目標方法內容 類別庫 Unit test 資料庫 外部 Service 測試專案 File
Demo 與外部類別相依的方法
- 11 - 如何與外部類別獨立
- 12 - 模擬外部類別 Stub • 定義預期回傳值 Mock • 定義預期回傳
值 • 可驗證呼叫次 數、順序以及 相關參數
- 13 - Stub Class原理
- 14 - Stub Class示意圖
- 15 - Mock framework做的事 • 將Stub類別透過Dynamic proxy打包 • Run
time才產生Stub類別,決定回傳值,用完即丟
Demo Mock framework
- 17 - 好的Unit Test Features • 執行速度快 • Unit
test程式碼應該很短 • Naming應該要可以看出要測的東西 • Unit test內容就類似code的spec • 應該與外界獨立 • 應該要能自動化
- 18 - 好的Unit Test Features (cont.) • 應該在任何環境下都可以測試 •
一次只測一件事 • IoC設計 • 失敗應該可以馬上知道出了什麼問題 • 設計test case要考量是否有boundary需要測試
- 19 - 結論 • Unit Test撰寫並不困難 • 讓架構與設計能力獲得提昇 •
Unit Test可以保護自己 • 可以循序漸進的讓現有系統固若金湯 • 可以平行開發,不用依賴外部元件
Thanks for your listening Q & A