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
66
Background Execution And WorkManager
elvismetaphor
2
480
作為一個跨平台的 Mobile App 開發者,從入門到放棄!?
elvismetaphor
2
490
Dependency Injection for testability of iOS app
elvismetaphor
1
1.4k
Briefly Introduction of Kotlin coroutines
elvismetaphor
1
280
MotionLayout Brief Introduction
elvismetaphor
1
320
Chapter 10. Pattern Matching with Regular Expressions
elvismetaphor
0
41
Other Decks in Programming
See All in Programming
AIコードエディタの基盤となるLLMのFlutter性能評価
alquist4121
0
200
Memory API : Patterns, Performance et Cas d'Utilisation
josepaumard
0
100
PHPで書いたAPIをGoに書き換えてみた 〜パフォーマンス改善の可能性を探る実験レポート〜
koguuum
0
130
AI Agents with JavaScript
slobodan
0
220
自分のために作ったアプリが、グローバルに使われるまで / Indie App Development Lunch LT
pixyzehn
1
150
小田原でみんなで一句詠みたいな #phpcon_odawara
stefafafan
0
320
AHC 044 混合整数計画ソルバー解法
kiri8128
0
320
「影響が少ない」を自分の目でみてみる
o0h
PRO
2
960
Building a macOS screen saver with Kotlin (Android Makers 2025)
zsmb
1
140
ベクトル検索システムの気持ち
monochromegane
31
9.8k
Code smarter, not harder - How AI Coding Tools Boost Your Productivity | Webinar 2025
danielsogl
0
120
Coding Experience Cpp vs Csharp - meetup app osaka@9
harukasao
0
730
Featured
See All Featured
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
52
2.4k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
34
2.2k
Automating Front-end Workflow
addyosmani
1369
200k
VelocityConf: Rendering Performance Case Studies
addyosmani
328
24k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
227
22k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.2k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Statistics for Hackers
jakevdp
798
220k
GraphQLの誤解/rethinking-graphql
sonatard
71
10k
Stop Working from a Prison Cell
hatefulcrawdad
268
20k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
331
21k
A better future with KSS
kneath
239
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」