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
コードカバレッジを⾒つつユニットテストを書く
Search
Tomoya Miwa
March 18, 2019
Programming
0
360
コードカバレッジを⾒つつユニットテストを書く
Tomoya Miwa
March 18, 2019
Tweet
Share
More Decks by Tomoya Miwa
See All by Tomoya Miwa
できる!ComposeでCollapsingToolbar
tomoya0x00
0
710
Compose の LazyColumn パフォーマンス改善で取り組んだこと
tomoya0x00
0
1.9k
ComposeのMutableStateってどうやってLocal Unit Testすれば良いの??
tomoya0x00
0
980
意外と簡単?Navigation rail導入のお話
tomoya0x00
0
1.3k
Kotlin Coroutines Flow を触ってみた話し
tomoya0x00
2
740
Android for Carsのお話し
tomoya0x00
1
980
熟成されたアプリのmulti module化(halfway)
tomoya0x00
2
850
multi module へ向けて
tomoya0x00
0
530
Kotlin で DSL を作り始めるまで
tomoya0x00
2
370
Other Decks in Programming
See All in Programming
Rubyとクリエイティブコーディングの輪の広がり / The Growing Circle of Ruby and Creative Coding
chobishiba
1
270
オートマトン学習しろ / Do automata learning
makenowjust
3
130
Modern Angular with the NGRX Signal Store New Rules for Your Architecture @BASTA! 2024 in Mainz
manfredsteyer
PRO
0
160
rails_girls_is_my_gate_to_join_the_ruby_commuinty
maimux2x
0
200
Ruby Parser progress report 2024
yui_knk
2
230
Crafting Cross-Platform Adventures: Building a Game Engine with Kotlin Multiplatform
dwursteisen
0
210
ECMAScript、Web標準の型はどう管理されているか / How ECMAScript and Web standards types are maintained
petamoriken
3
390
LangChainでWebサイトの内容取得やGitHubソースコード取得
shukob
0
160
ドメイン駆動設計を実践するために必要なもの
bikisuke
4
330
いつか使える ObjectSpace / Maybe useful ObjectSpace
euglena1215
2
140
Swiftコードバトル必勝法
toshi0383
0
170
Regular Expressions, REXML, Automata Learning
makenowjust
0
220
Featured
See All Featured
Designing with Data
zakiwarfel
98
5k
What's new in Ruby 2.0
geeforr
340
31k
Rails Girls Zürich Keynote
gr2m
93
13k
Into the Great Unknown - MozCon
thekraken
29
1.4k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
1
55
Pencils Down: Stop Designing & Start Developing
hursman
119
11k
Fontdeck: Realign not Redesign
paulrobertlloyd
80
5.1k
Put a Button on it: Removing Barriers to Going Fast.
kastner
58
3.4k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
663
120k
Raft: Consensus for Rubyists
vanstee
136
6.5k
Building Flexible Design Systems
yeseniaperezcruz
325
38k
The Pragmatic Product Professional
lauravandoore
31
6.2k
Transcript
コードカバレッジを⾒つつ ユニットテストを書く tomoya0x00 Otemachi.apk #02 #otemachi_apk
About me tomoya0x00 Twitter, GitHub, Qiita Android, Embedded system, BLE/BT,
iOS DeNA Co., Ltd. Automotive Business Unit. 前回(Otemachi.apk #01)は⾵邪で登壇できなかった
みなさん、ユニットテストしてますか︖
コードカバレッジは⾒てますか︖
コードカバレッジとは︖
コードカバレッジとは︖ ソースコードのテストされた割合 測定⼿法がいくつかある
測定⼿法、2つだけご紹介
命令網羅(C0) ソースコードの 各⽂ が実⾏されているか fun hoge(x: Int) { println("hoge") if
(x > 0) { println("fuga") } } hoge(x = 1) だけで C0:100%
分岐網羅(C1) ソースコードの 各条件 が実⾏されているか fun hoge(x: Int) { println("hoge") if
(x > 0) { println("fuga") } } hoge(x = 1) と hoge(x = 0) で C1:100%
ユニットテスト + コードカバレッジ
Why?
テストコードが充分か︖ 判断基準の⼀つとして使える
例︓既存コードのリファクタリング
既存コード fun hoge(x: Int): String { if (x >= 3)
return "many" if (x == 2) return "two" if (x == 1) return "one" if (x == 0) return "zero" return "other" }
テストコードを書く @Test fun hogeTest() { assert(hoge(3) == "many") assert(hoge(2) ==
"two") assert(hoge(1) == "one") assert(hoge(0) == "zero") }
このテストコードで充分なのか・・・︖
そこでコードカバレッジ
コードカバレッジを計測する⽅法 Android Studio JaCoCo Other...?
コードカバレッジを計測する⽅法 Android Studio JaCoCo 今回はこちら Other...?
JaCoCoの準備
デフォルト設定だと、 Kotlinが計測対象とならない
JaCoCoの準備 arturdm/jacoco-android-gradle-plugin Issue 37 の JaCoCoの設定 を参考にすればOK ※上記の設定は build variants
も考慮されていてステキ
カバレッジ結果をみてみる
カバレッジ結果をみてみる $ gradlew jacocoDebugReport 実⾏後に、 app/build/reports/jacoco/jacocoDebugReport/html/index.html を開く
None
None
None
None
テストコードを⾜す @Test fun hogeTest() { assert(hoge(3) == "many") assert(hoge(2) ==
"two") assert(hoge(1) == "one") assert(hoge(0) == "zero") assert(hoge(-1) == "other") // ★追加テストコード }
None
無事、カバレッジ100%
リファクタリングも安⼼
Kotlinっぽくしてみる fun hoge(x: Int): String = when { x >=
3 -> "many" x == 2 -> "two" x == 1 -> "one" x == 0 -> "zero" else -> "other" }
None
補⾜ カバレッジ100%だからテストコード完璧、ではない でも、判断基準の⼀つとして使えます カバレッジ100%必達では無い テストコード書きづらいところは、⼿動で確認しても良いんです
ありがとうございました︕