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
46
Dubugging Tips and Tricks for iOS development
elvismetaphor
0
50
Strategies of Facebook LightSpeed project
elvismetaphor
0
77
Background Execution And WorkManager
elvismetaphor
2
480
作為一個跨平台的 Mobile App 開發者,從入門到放棄!?
elvismetaphor
2
500
Dependency Injection for testability of iOS app
elvismetaphor
1
1.4k
Briefly Introduction of Kotlin coroutines
elvismetaphor
1
280
MotionLayout Brief Introduction
elvismetaphor
1
330
Chapter 10. Pattern Matching with Regular Expressions
elvismetaphor
0
46
Other Decks in Programming
See All in Programming
設計やレビューに悩んでいるPHPerに贈る、クリーンなオブジェクト設計の指針たち
panda_program
6
1.1k
5つのアンチパターンから学ぶLT設計
narihara
1
100
Effect の双対、Coeffect
yukikurage
5
1.4k
Claude Codeの使い方
ttnyt8701
1
130
XSLTで作るBrainfuck処理系
makki_d
0
210
iOSアプリ開発で 関数型プログラミングを実現する The Composable Architectureの紹介
yimajo
2
210
なんとなくわかった気になるブロックテーマ入門/contents.nagoya 2025 6.28
chiilog
1
180
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
180
Blazing Fast UI Development with Compose Hot Reload (droidcon New York 2025)
zsmb
1
180
ReadMoreTextView
fornewid
1
460
データの民主化を支える、透明性のあるデータ利活用への挑戦 2025-06-25 Database Engineering Meetup#7
y_ken
0
300
C++20 射影変換
faithandbrave
0
520
Featured
See All Featured
Java REST API Framework Comparison - PWX 2021
mraible
31
8.6k
Gamification - CAS2011
davidbonilla
81
5.3k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
RailsConf 2023
tenderlove
30
1.1k
How to Ace a Technical Interview
jacobian
277
23k
Visualization
eitanlees
146
16k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
33
5.9k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Side Projects
sachag
455
42k
A Tale of Four Properties
chriscoyier
160
23k
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
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」