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
The Importance of Being Tested
Search
Roberto Orgiu
October 21, 2021
Programming
0
350
The Importance of Being Tested
Slides of the talk I gave at Droidcon Berlin 2021
Roberto Orgiu
October 21, 2021
Tweet
Share
More Decks by Roberto Orgiu
See All by Roberto Orgiu
Wellness & Droid
tiwiz
0
84
Behind the curtains
tiwiz
0
28
An Android Dev start to Kotlin MPP
tiwiz
0
120
Fantastic API and where to find them
tiwiz
0
42
Flipping the Koin @ GDG Dev Party
tiwiz
1
36
Flipping the Koin
tiwiz
2
130
Trip into the async world @ NYC Kotlin Meetup
tiwiz
0
70
Trip into the async world
tiwiz
1
92
GraphQL IRL (Android Makers)
tiwiz
0
140
Other Decks in Programming
See All in Programming
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
170
命名をリントする
chiroruxx
1
390
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
210
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
110
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
140
たのしいparse.y
ydah
3
120
第5回日本眼科AI学会総会_AIコンテスト_3位解法
neilsaw
0
170
선언형 UI에서의 상태관리
l2hyunwoo
0
140
Go の GC の不得意な部分を克服したい
taiyow
2
770
Featured
See All Featured
Scaling GitHub
holman
458
140k
Adopting Sorbet at Scale
ufuk
73
9.1k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
170
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
A Tale of Four Properties
chriscoyier
157
23k
Statistics for Hackers
jakevdp
796
220k
Faster Mobile Websites
deanohume
305
30k
Transcript
Roberto Orgiu | Senior Android Engineer @ NYTimes | @_tiwiz
The importance of being tested
What is testing about?
Correctness Functioning
Is everything testable?
class Repository { private val dep = Dependency( ) fun
fetch() = dep.getData( ) }
class Repository ( private val dep: Dependenc y ) {
fun fetch() = dep.getData( ) }
What should I test?
Test the logic, not the code. Fabio Collini (probably?)
fun testCode() { repository.fetch( ) verify(mockDependency).getData( ) }
fun testLogic() { val actualResult = repository.fetch( ) verify(actualResult )
.isEqualTo(expectedResult ) }
Was that unit testing?
Was that unit testing? Is unit testing enough?
cwti.link/twitch
What about integration testing?
What about integration testing? integration tests validate the collaboration and
interaction of a group of units.
My take on integration testing
My take on integration testing •No Android deps •Test fl
ow from start to end •Use Robolectric
class RootFragment : Fragment() { lateinit var repository: Repositor y
lateinit var view: Vie w fun onResume() { val response = repository.fetchThings( ) view.bindResults(response ) } }
class IntegrationTest { private val mock = TestDouble(Service() ) private
val testFragment = RootFragment( ) fun integrationTest() { run(testFragment).verify( ) } } androidTest
What about Network?
What about network? Network is unreliable
What about network? • Retro fi t + OkHttp +
MockWebServer • Ktor + MockEngine
val retrofit = Retrofit.Builder( ) .baseUrl("https://api.github.com/" ) .build( ) This
should come from the outside!
val retrofit = Retrofit.Builder( ) .baseUrl(url ) .build( )
val server = MockWebServer( ) server.enqueue ( MockResponse().setBody("hello, world!" )
) server.start( ) val url = server.url( )
HttpClient(Android) { install(Logging) { … } install(JsonFeature) { … }
} This should come from the outside!
val mockEngine = MockEngine { request - > respond (
content = ByteReadChannel(content) , status = HttpStatusCode.OK , headers = headersOf(ContentType, type ) ) }
What about UI testing?
What about UI testing?
@get:Rule var activityScenarioRule = activityScenarioRule<MyActivity>( ) @Tes t fun changeText()
{ onView(withId(startViewId) ) .perform ( typeText(MESSAGE) , closeSoftKeyboard( ) ) onView(withId(buttonViewId) ) .perform(click() ) onView(withId(targetView) ) .check(matches(withText(MESSAGE)) ) }
None
None
@Composabl e fun SimpleUI() { var clicks by remember {
mutableStateOf(0) } Column { Button ( onClick = { clicks++ } ) { Text(text = "Click me" ) } if (clicks > 0) { Text(text = "$clicks" ) } } }
@get:Rul e val composeTestRule = createComposeRule( ) @Tes t fun
verify_initial_case() { composeTestRule.setContent { SimpleUI( ) } composeTestRule.onNodeWithTag("clicks" ) .assertDoesNotExist( ) }
@get:Rul e val composeTestRule = createComposeRule( ) @Tes t fun
verify_last_case() { composeTestRule.setContent { SimpleUI( ) } composeTestRule.onNodeWithText("Click me" ) .performClick( ) with(composeTestRule.onNodeWithTag("clicks")) { assertIsDisplayed( ) assertTextEquals("1" ) } }
How can I start testing?
Roberto Orgiu | Senior Android Engineer @ NYTimes | @_tiwiz
Thanks for listening. Q&A Time