Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs Alberto Gragera Technical Director Jorge Barroso Google Developer Expert Davide Mendolia Senior Full Stack Engineer Sergio Gutierrez Senior Mobile Engineer

Slide 4

Slide 4 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs Let’s talk about testing!

Slide 5

Slide 5 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs This talk starts with some common problems.

Slide 6

Slide 6 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs What do we have to test and how?

Slide 7

Slide 7 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs What if our code is not testable?

Slide 8

Slide 8 text

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.

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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.

Slide 11

Slide 11 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs The Solution: Testable Code + Testing Pipeline

Slide 12

Slide 12 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs The Software Architecture used has to hide implementation details.

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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.

Slide 18

Slide 18 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs What do I want to test?

Slide 19

Slide 19 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs Test my code implements the business requirements.

Slide 20

Slide 20 text

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.

Slide 21

Slide 21 text

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.

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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.

Slide 24

Slide 24 text

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.

Slide 25

Slide 25 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs How are we going to test this?

Slide 26

Slide 26 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs Test my code implements the business requirements.

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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.

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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.

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs Given some HTTP responses the final authentication state is correct.

Slide 38

Slide 38 text

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.

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs Initializes a fake http server where we can configure responses.

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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.

Slide 43

Slide 43 text

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.

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs Code Injected from the test application.

Slide 46

Slide 46 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs Asserts from the UI point of view.

Slide 47

Slide 47 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs Asserts from the User Interface point of view.

Slide 48

Slide 48 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs Helper methods or different creational patterns to create initial scenarios.

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs Configure your dependency injection library from your Application class.

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs Replace Application’s dependency container with a custom configuration.

Slide 53

Slide 53 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs Now we can write UI tests easily. The Result

Slide 54

Slide 54 text

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.

Slide 55

Slide 55 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs Now we can trust our tests.

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs Business Logic Tests ● JUnit. ● Mockito.

Slide 58

Slide 58 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs API Client Tests ● JUnit. ● Mockito. ● MockWebServer.

Slide 59

Slide 59 text

Pedro V. Gómez Sánchez - [email protected] - @pedro_g_s - github.com/pedrovgs UI Tests ● JUnit. ● Mockito. ● Robolectric. ● Espresso. ● Dagger.

Slide 60

Slide 60 text

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.

Slide 61

Slide 61 text

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