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
350
コードカバレッジを⾒つつユニットテストを書く
Tomoya Miwa
March 18, 2019
Tweet
Share
More Decks by Tomoya Miwa
See All by Tomoya Miwa
できる!ComposeでCollapsingToolbar
tomoya0x00
0
540
Compose の LazyColumn パフォーマンス改善で取り組んだこと
tomoya0x00
0
1.6k
ComposeのMutableStateってどうやってLocal Unit Testすれば良いの??
tomoya0x00
0
880
意外と簡単?Navigation rail導入のお話
tomoya0x00
0
1.1k
Kotlin Coroutines Flow を触ってみた話し
tomoya0x00
2
680
Android for Carsのお話し
tomoya0x00
1
940
熟成されたアプリのmulti module化(halfway)
tomoya0x00
2
780
multi module へ向けて
tomoya0x00
0
490
Kotlin で DSL を作り始めるまで
tomoya0x00
2
350
Other Decks in Programming
See All in Programming
Site Reliability Engineering for GMO
pyama86
8
1k
二郎系ラーメンのコールで学ぶ AST 解析
memory1994
PRO
7
1.7k
Build Apps for iOS, Android & Desktop in 100% Kotlin With Compose Multiplatform (mDevCamp 2024)
zsmb
0
320
エンターテイメント業界で利用されるAWS
demuyan
0
210
OpenAPIを中心に考えるAPI開発入門 / Introduction to API Development with a Focus on OpenAPI
seike460
PRO
2
170
TYPO3 v13 – The road to LTS: What's new and new APIs
luisasofie_xoxo
0
200
Scalable Customer Journey Orchestration (CJO)
lewuathe
0
220
R言語の環境構築と基礎 Tokyo.R 112
bob3bob3
0
270
ONE WEDGE_company_guide
1wedge_one
0
470
見た目から始める生産性向上
ikumatadokoro
7
830
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
380
Compose-View Interop in Practice (mDevCamp 2024)
stewemetal
0
130
Featured
See All Featured
No one is an island. Learnings from fostering a developers community.
thoeni
16
2.1k
Git: the NoSQL Database
bkeepers
PRO
422
63k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
30
6k
The Brand Is Dead. Long Live the Brand.
mthomps
49
28k
Making the Leap to Tech Lead
cromwellryan
124
8.5k
Happy Clients
brianwarren
92
6.4k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
9
8.3k
WebSockets: Embracing the real-time Web
robhawkes
59
7k
Put a Button on it: Removing Barriers to Going Fast.
kastner
58
3k
Intergalactic Javascript Robots from Outer Space
tanoku
266
26k
KATA
mclloyd
15
12k
In The Pink: A Labor of Love
frogandcode
138
21k
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%必達では無い テストコード書きづらいところは、⼿動で確認しても良いんです
ありがとうございました︕