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
69
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
42
Other Decks in Programming
See All in Programming
VibeCoding時代のエンジニアリング
daisuketakeda
0
200
ウォンテッドリーの「ココロオドル」モバイル開発 / Wantedly's "kokoro odoru" mobile development
kubode
2
560
バイラテラルアップサンプリング
fadis
3
520
Ruby で作る RISC-V CPU エミュレーター / RISC-V CPU emulator made with Ruby
hayaokimura
5
1.1k
eBPF超入門「o11yに使える」とは (20250424_eBPF_o11y)
thousanda
1
120
Road to Ruby for A Linguistics Nerd
hayat01sh1da
PRO
0
260
On-the-fly Suggestions of Rewriting Method Deprecations
ohbarye
3
5.4k
Golangci-lint v2爆誕: 君たちはどうすべきか
logica0419
1
260
ビカム・ア・コパイロット
ymd65536
1
130
2025-04-25 GitHub Copilot Agent ライブデモ(スクリプト)
goataka
0
110
オープンソースコントリビュート入門
_katsuma
0
130
Beyond_the_Prompt__Evaluating__Testing__and_Securing_LLM_Applications.pdf
meteatamel
0
110
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
329
21k
Building Better People: How to give real-time feedback that sticks.
wjessup
368
19k
We Have a Design System, Now What?
morganepeng
52
7.6k
How to Ace a Technical Interview
jacobian
276
23k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.7k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
52
2.5k
Docker and Python
trallard
44
3.4k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.8k
VelocityConf: Rendering Performance Case Studies
addyosmani
329
24k
Designing Experiences People Love
moore
142
24k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.3k
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」