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 Test Introduction
Search
Elvis Lin
July 06, 2017
Programming
0
190
Unit Test Introduction
Introduce briefly how to do a unit test and write a testable code
Elvis Lin
July 06, 2017
Tweet
Share
More Decks by Elvis Lin
See All by Elvis Lin
Protect Users' Privacy in iOS 14
elvismetaphor
0
52
Dubugging Tips and Tricks for iOS development
elvismetaphor
0
52
Strategies of Facebook LightSpeed project
elvismetaphor
0
85
Background Execution And WorkManager
elvismetaphor
2
490
作為一個跨平台的 Mobile App 開發者,從入門到放棄!?
elvismetaphor
2
510
Dependency Injection for testability of iOS app
elvismetaphor
1
1.4k
Briefly Introduction of Kotlin coroutines
elvismetaphor
1
290
MotionLayout Brief Introduction
elvismetaphor
1
330
Chapter 10. Pattern Matching with Regular Expressions
elvismetaphor
0
48
Other Decks in Programming
See All in Programming
フロントエンド開発に役立つクライアントプログラム共通のノウハウ / Universal client-side programming best practices for frontend development
nrslib
7
3.8k
非同期jobをtransaction内で 呼ぶなよ!絶対に呼ぶなよ!
alstrocrack
0
290
猫と暮らすネットワークカメラ生活🐈 ~Vision frameworkでペットを愛でよう~ / iOSDC Japan 2025
yutailang0119
0
210
開発者への寄付をアプリ内課金として実装する時の気の使いどころ
ski
0
320
なぜGoのジェネリクスはこの形なのか? Featherweight Goが明かす設計の核心
ryotaros
7
960
2分台で1500examples完走!爆速CIを支える環境構築術 - Kaigi on Rails 2025
falcon8823
3
2.2k
AIエージェント時代における TypeScriptスキーマ駆動開発の新たな役割
bicstone
4
1.2k
Go Conference 2025: Goで体感するMultipath TCP ― Go 1.24 時代の MPTCP Listener を理解する
takehaya
7
1.4k
Build your own WebP codec in Swift
kishikawakatsumi
2
830
生成AIを活用した初学者向けPython講座
soogie
1
130
Web Components で実現する Hotwire とフロントエンドフレームワークの橋渡し / Bridging with Web Components
da1chi
2
1.2k
Platformに“ちょうどいい”責務ってどこ? 関心の熱さにあわせて考える、責務分担のプラクティス
estie
2
510
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
135
9.5k
Scaling GitHub
holman
463
140k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
The Pragmatic Product Professional
lauravandoore
36
6.9k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Visualization
eitanlees
148
16k
Into the Great Unknown - MozCon
thekraken
40
2.1k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
358
30k
It's Worth the Effort
3n
187
28k
jQuery: Nuts, Bolts and Bling
dougneiner
64
7.9k
What's in a price? How to price your products and services
michaelherold
246
12k
Producing Creativity
orderedlist
PRO
347
40k
Transcript
Unit Test 簡介 Elvis Lin 2017-07-06 @KKday
關於測試 • 測試個概念念與定義 • 測試的分類 • End-to-End Test • Integration
Test • Unit Test • 測試循環:使⽤用 BDD 結合 TDD • 單元測試的實作 • 驗收測試的實作
這次分享的重點 • 測試個概念念與定義 • 測試的分類 • 測試循環:使⽤用 BDD 結合 TDD
•單元測試的實作 • 驗收測試的實作
傳統的測試流程 • ⼈人⼯工測試 • 屬於整合測試或 End-to-End 測試 • 集中在軟體開發完成之後 •
……
⼈人⼯工測試
傳統測試流程的問題 • 環境準備的時間比較長 • 執⾏行行測試的時間比較長 • 難以保證每次測試的操作都相同 • 發現錯誤後,除錯的時間比較長
漫長的等待
如何減少等待的時間 • 撰寫腳本讓⼈人⼯工操作變成⾃自動化的操作 • 撰寫⼤大量量的單元測試,讓問題在開發初期就可 以被發現
Unit Test
什什麼是單元測試 • 情侶間的單元測試: • 傳 Line 訊息,測 input 跟 output
• 情侶間的整合測試 • ⼀一起出去玩 — 來來源<Twitter>
什什麼是單元測試 • 測試軟體系統的最⼩小單元 • Method 或 Function • Class 或
Object • 某組⾏行行為
測試的流程 • (準備相關的資料與執⾏行行環境) • 執⾏行行要被測試的程式碼 • 檢查程式執⾏行行的結果是否符合預期
單元測試範例例1 • Production Code • Test Code
單元測試範例例2
3A • Arrange — 準備需要的資料 • Action — 執⾏行行被測試的程式 •
Assert — 驗證執⾏行行的結果
3A — 範例例說明 @Test public void testPop() { // Arrange
// 準備⼀一個 Stack,在裡⾯面放入幾筆資料 Stack<String> stack = new Stack<>(); stack.push("teddy"); stack.push("kay"); stack.push("eiffel"); // Action // 再放入⼀一筆資料 stack.push(“ada"); // Assert // 取出最後⼀一筆資料,確認是否等於剛剛放入的資料 assertEquals("ada", stack.pop()); }
真實的範例例 https://github.com/square/retrofit/tree/ master/samples
None
如何測試這段程式?
直接檢查 console 的輸出
延伸思考 •如果測試的時候沒有網路路怎麼辦? •如果輸出不是輸出到 console,⽽而是某個 GUI 的介 ⾯面怎麼辦?
原本的程式流程 System.out Main() Retrofit 取回 Contributor 輸出 Contributor
修改後的程式流程 Contributor Presenter Contributor App Contributor Manager 取回 Contributor 輸出
Contributor
修改後的程式流程 • ContributorManager — 使⽤用 retrofit 取得 contributor 的資訊 •
ContributorPresenter — 在螢幕上顯⽰示 contributor 的資 訊 • ContributorApp — 使⽤用 ContributorManager 跟 ContributorPresenter 取得並顯⽰示 contributor 的資訊
新的架構 Contributor App Contributor Manager Contributor Presenter ContributorApp(manager, presenter) Main()
ContributorManager
ContributorPresenter
ContributorApp
如何對 ContributorApp 測試 • 建立 MockContributorManager 實作 ContributorManager • 建立
MockContributorPresenter 實作 ContributorPresenter • 當建立 ContributorApp 時,使⽤用剛剛建立的 test double • 執⾏行行 ContributorApp.execute(),檢查執⾏行行的結果
Demo 程式碼:https://goo.gl/ZHjRhk
咦,這樣好像在測假的? 假的!
使⽤用單元測試的理理由 • 使⽤用 Mock 隔離不相⼲干的程式碼 • ContributorApp 的邏輯可能很複雜 • 之後可能包含
filter 的邏輯等... • 測試執⾏行行的時間可以很短
還是可以⽤用整合測試補⾜足
Testability
讓程式更更具備可測試性 •關注點分離 •依賴注入
關注點分離 • 類似「單⼀一職責原則」 • ⼀一個物件負責⼀一件事,要避免⼀一個物件擁有太多職責 • Wiki: https://en.wikipedia.org/wiki/ Separation_of_concerns
依賴注入 • 物件中所有使⽤用的其他物件不是由⾃自⼰己⽣生成,⽽而是透過外 部⽣生成後注入 • Wiki: https://en.wikipedia.org/wiki/Dependency_injection
參參考資料 • 搞笑談軟⼯工 http://teddy-chen-tw.blogspot.tw/search/label/ %E6%B8%AC%E8%A9%A6 • 三⼗十天快速上⼿手TDD https://dotblogs.com.tw/hatelove/series/1? qq=30%E5%A4%A9%E5%BF%AB%E9%80%9F%E4%B8%8 A%E6%89%8BTDD
讀書會推廣 • 預計時間:每週⼀一 19:00 ~ 20:00 @2F會議室 • 書籍:暫定「Work Effectively
with Legacy Code」