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
49
Dubugging Tips and Tricks for iOS development
elvismetaphor
0
50
Strategies of Facebook LightSpeed project
elvismetaphor
0
80
Background Execution And WorkManager
elvismetaphor
2
480
作為一個跨平台的 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
47
Other Decks in Programming
See All in Programming
What's new in AppKit on macOS 26
1024jp
0
180
React は次の10年を生き残れるか:3つのトレンドから考える
oukayuka
41
16k
Bedrock AgentCore ObservabilityによるAIエージェントの運用
licux
8
470
型で語るカタ
irof
1
880
Gemini CLI のはじめ方
ttnyt8701
1
110
MCPで実現できる、Webサービス利用体験について
syumai
7
2.2k
Vibe Codingの幻想を超えて-生成AIを現場で使えるようにするまでの泥臭い話.ai
fumiyakume
20
9.7k
顧客の画像データをテラバイト単位で配信する 画像サーバを WebP にした際に起こった課題と その対応策 ~継続的な取り組みを添えて~
takutakahashi
4
1.4k
変化を楽しむエンジニアリング ~ いままでとこれから ~
murajun1978
0
590
副作用と戦う PHP リファクタリング ─ ドメインイベントでビジネスロジックを解きほぐす
kajitack
3
500
状態遷移図を書こう / Sequence Chart vs State Diagram
orgachem
PRO
3
310
[DevinMeetupTokyo2025] コード書かせないDevinの使い方
takumiyoshikawa
2
230
Featured
See All Featured
Side Projects
sachag
455
43k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.5k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
5.9k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
21
1.4k
YesSQL, Process and Tooling at Scale
rocio
173
14k
Navigating Team Friction
lara
188
15k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.6k
Making Projects Easy
brettharned
117
6.3k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
1k
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」