Slide 1

Slide 1 text

Android Testing

Slide 2

Slide 2 text

About me • Nguyen Truong Giang • Senior Android Engineer / Android Leader @ tiki.vn • Skype: gianguitpp • Email: [email protected] • https://github.com/talenguyen

Slide 3

Slide 3 text

Content • Why test? • Types of tests • When to use? • Practice

Slide 4

Slide 4 text

Why test?

Slide 5

Slide 5 text

• Automation of manual work • Write testable code make a highly modular code

Slide 6

Slide 6 text

Types of tests

Slide 7

Slide 7 text

Test Pyramid https://martinfowler.com/bliki/TestPyramid.html

Slide 8

Slide 8 text

JVM

Slide 9

Slide 9 text

JVM Device

Slide 10

Slide 10 text

JVM Device JVM Unit Test
 (Fastest - Pure Logic)

Slide 11

Slide 11 text

JVM Device Instrumentation Unit Test
 (Logic with Context) JVM Unit Test
 (Fastest - Pure Logic) /test /androidTest

Slide 12

Slide 12 text

JVM Device Instrumentation Unit Test
 (Logic with Context) Instrumentation UI Test JVM Unit Test
 (Fastest - Pure Logic) Non-UI UI /test /androidTest

Slide 13

Slide 13 text

Unit test • Verify simple logic • Functions, methods or entire class • Independent

Slide 14

Slide 14 text

class ValidatorKtTest { @Test fun emailAddress() { assertThat(isEmailAddress("[email protected]")).isTrue() } @Test fun notEmailAddress() { assertThat(isEmailAddress("foo@tiki")).isFalse() } }

Slide 15

Slide 15 text

Integration test • Verify how units interact with each other • Has dependencies • Mock & verify

Slide 16

Slide 16 text

@Test fun `login success then save token to LocalStorage`() { // Setup val mockedApiServices = mock() val mockedLocalStorage = mock() val tested = UserRepository(InstantAppExecutors(), mockedApiServices, mockedLocalStorage) // given val token = "token" `when`(mockedApiServices.login(anyString(), anyString())) .thenReturn(just(ApiResponse.create(Response.success(token)))) // when tested.login("[email protected]", "bar") .observeForever(mock()) // then verify(mockedLocalStorage).setAccessToken(token) }

Slide 17

Slide 17 text

UI (end to end) test • Verify whole system work • User interacts with UI components

Slide 18

Slide 18 text

@Test fun invalidEmail() { // Input invalid email onView(withId(R.id.etUsername)) .perform(replaceText("giang"), closeSoftKeyboard()) // Verify invalid email message is shown onView(withId(R.id.tilUsername)) .check(matches(hasError(R.string.email_is_invalid))) // Verify submit button is disable onView(withId(R.id.btSubmit)) .check(matches(Matchers.not(isEnabled()))) }

Slide 19

Slide 19 text

When to use?

Slide 20

Slide 20 text

• Unit test for simple logic • Integration test to verify business logic • UI test for the rest

Slide 21

Slide 21 text

Practice

Slide 22

Slide 22 text

• https://github.com/tikivn/AndroidTesting • Unit test: Validator • Integration test: UseRepository • get “access-token” from LocalStorage • login success then save “access-token” to LocalStorage

Slide 23

Slide 23 text

• UI test: LoginFragment • test invalid email • test valid email • test login error

Slide 24

Slide 24 text

Thank you

Slide 25

Slide 25 text

We’re hiring • Send CV to [email protected] or [email protected] • https://tuyendung.tiki.vn

Slide 26

Slide 26 text

References • https://developer.android.com/training/testing/ • https://www.linkedin.com/learning/effective-android- testing-for-mobile-developers • https://martinfowler.com/articles/practical-test- pyramid.html • https://academy.realm.io/posts/kau-jake-wharton-testing- robots/