World-Class Testing Development Pipeline for Android

61
Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs World-Class Testing Development Pipeline for Android Pedro Vicente Gómez Sánchez Senior Mobile Developer at Karumi [email protected] @pedro_g_s github.com/pedrovgs

Transcript of World-Class Testing Development Pipeline for Android

Page 1: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

World-Class Testing Development Pipeline for AndroidPedro Vicente Gómez SánchezSenior Mobile Developer at Karumi

[email protected]@pedro_g_sgithub.com/pedrovgs

Page 2: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Page 3: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Alberto GrageraTechnical Director

Jorge BarrosoGoogle Developer Expert

Davide MendoliaSenior Full Stack Engineer

Sergio GutierrezSenior Mobile Engineer

Page 4: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Let’s talk about testing!

Page 5: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

This talk starts with some common problems.

Page 6: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

What do we have to test and how?

Page 7: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

What if our code is not testable?

Page 8: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Just because our tests are passing doesn’t mean your code is working.

Page 9: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Any conclusions?

Page 10: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

We are doing something wrong if we can’t trust our tests. We need a solution.

Page 11: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

The Solution: Testable Code + Testing Pipeline

Page 12: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

The Software Architecture used has to hide implementation details.

Page 13: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Dependency Inversion Principle is going to guide your Architecture.

DIP is the most important S.O.L.I.D. principle.

Page 14: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Architecture - Testable Code

Activity

View

Presenter

Use Case

Repository

Repository

Domain Model

Data Source

Data Source

Data Source Implementation

Data Source Implementation

Use Case

Use Case

Page 15: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Architecture is not enough.

Page 16: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Testing Development Pipeline

Page 17: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Your Testing Development Pipeline should define what you are going to test and how.

Page 18: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

What do I want to test?

Page 19: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Test my code implements the business requirements.

Page 20: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Test my API Client, HTTP requests/responses and JSON parsing, are working as expected.

Page 21: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Test the user is watching in the App UI the expected messages.

Page 22: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

In other words…

Page 23: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Test our Business Logic independently of frameworks or libraries.

Test our integration with the API.

Test our application UI.

Page 24: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

These three points define part of the Testing Development Pipeline.

These three points define what we want to test.

Page 25: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

How are we going to test this?

Page 26: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Test my code implements the business requirements.

Page 27: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Think in the test scope and try to check the state of the software at the end of the test execution.

Use the Dependency Inversion Principle to be able to use test doubles to simulate different test

scenarios and adjust the test scope.

Business Logic Tests

Page 28: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Show me the code!

Page 29: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Check the state of the software at the end of

the test execution

Page 30: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Implementation Details are replaced using Test Doubles.

The SUT is the GameBoy, the GBZ80

and the GPU.

Page 31: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Infrastructure needed?

Page 32: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

The usage of test doubles are the key to adjust the test scope.

The Application architecture will help use to hide

implementation details applying the dependency

inversion principle

Page 33: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Now we can test our business logic without depending on any framework

or library. Inside an isolated environment.

The Result

Page 34: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Test my API Client, HTTP requests/responses and JSON parsing, are working as expected.

Page 35: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Start a fake server we have under control checking the API client is working given some

preconfigured scenarios.

API Client tests

Page 36: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Show me the code!

Page 37: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Given some HTTP responses the final

authentication state is correct.

Page 38: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Given a Json file with the response, an http status code, and an

endpoint the parsing is correct.

Page 39: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Infrastructure needed?

Page 40: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Initializes a fake http server where we can configure responses.

Page 41: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Now we can check if our integration with the API Client is working as it

should.

The Result

Page 42: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Test the user is watching in the App UI the expected messages.

Page 43: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

UI Tests

Replace some dependencies using test doubles from the testing application to be able to simulate different

scenarios and check the UI is showing the correct information.

Page 44: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Show me the code!

Page 45: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Code Injected from the test application.

Page 46: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Asserts from the UI point of view.

Page 47: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Asserts from the User Interface point of

view.

Page 48: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Helper methods or different creational patterns to create initial scenarios.

Page 49: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Infrastructure needed?

Page 50: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Configure your dependency injection

library from your Application class.

Page 51: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Mocking Library

Helper Methods

Page 52: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Replace Application’s dependency container

with a custom configuration.

Page 53: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Now we can write UI tests easily.

The Result

Page 54: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

The Testing Development Pipeline is complete now.

Now we know what to test and how.

Page 55: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Now we can trust our tests.

Page 56: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Tools and libraries?

Page 57: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Business Logic Tests

● JUnit.

● Mockito.

Page 58: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

API Client Tests

● JUnit.

● Mockito.

● MockWebServer.

Page 59: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

UI Tests● JUnit.

● Mockito.

● Robolectric.

● Espresso.

● Dagger.

Page 60: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

References● World-Class Testing Development Pipeline for Android blog post series.

● Kata Contacts. GitHub Repository.

● Kata TODO API Client. GitHub Repository.

● Kata Super Heroes. GitHub Repository.

● Android GameBoy Emulator. GitHub Repository.

● Google Testing Blog. Testing state vs interaction.

Page 61: World-Class Testing Development Pipeline for Android

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs

Questions?