Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Test-Driven Development

Android Test-Driven Development

Test-Driven Development (TDD) is a software development process where you write failing tests before writing the code to make it pass. This can be a common practice in some technology circles, but we’ve seen slow adoption on Android. Because it’s not embedded in the Android culture, it can be hard to learn how to get started with TDD. In this talk, we’ll go over the basics of TDD, how you can apply it at varying levels in an Android architecture, and what steps to take if you’re working with legacy code. You should walk away from this talk with the tools you need to get started exploring practicing TDD on your own.

Victoria Gonda

October 15, 2019
Tweet

More Decks by Victoria Gonda

Other Decks in Programming

Transcript

  1. Android Test-Driven
    Development
    Victoria Gonda

    View Slide

  2. Hello, I'm Victoria!
    • Android Engineer at Buffer
    • Author at RayWenderlich.com
    • Hedgehog and cat owner

    View Slide

  3. Android Test-Driven
    Development by Tutorials
    A book that teaches you to
    write sustainable, testable
    apps, as well as to apply
    testing strategies to legacy
    projects via Espresso and
    UI tests, code coverage
    and refactoring.

    View Slide

  4. 1.
    What is TDD
    Test-Driven Development
    basics

    View Slide

  5. What to test?
    ▪ Do test code that:
    ▪ Contains logic
    ▪ Is fragile
    ▪ Will change

    View Slide

  6. What to not test?
    ▪ Don't test code that:
    ▪ Is not yours
    ▪ Is generated
    ▪ Is throwaway

    View Slide

  7. What is TDD
    ▪ Write a failing test
    ▪ Write code to make it
    pass
    ▪ Refactor

    View Slide

  8. Why use TDD?
    ▪ Write intentionally
    ▪ Automatically document
    ▪ Keep maintainable code
    ▪ Have confidence in your
    code
    ▪ Develop faster
    ▪ Higher test coverage

    View Slide

  9. 2.
    The Pyramid
    How many tests should I have?

    View Slide

  10. Testing Pyramid
    ▪ 10% UI Tests
    ▪ 20% Integration
    Tests
    ▪ 70% Unit Tests

    View Slide

  11. Unit Tests
    ▪ Fast
    ▪ Focused
    ▪ Mock dependencies

    View Slide

  12. Integration Tests
    ▪ Slower
    ▪ Test parts working
    together

    View Slide

  13. UI Tests
    ▪ Slowest
    ▪ Running full app

    View Slide

  14. 3.
    Tools
    What can you use for testing?

    View Slide

  15. Architecture
    ▪ Important for
    testability
    ▪ Design with tests in
    mind

    View Slide

  16. Mockito
    ▪ "Tasty mocking
    framework for unit
    tests in Java
    ▪ Make "fake"
    dependencies

    View Slide

  17. Robolectric
    ▪ "Robolectric is a
    framework that brings
    fast and reliable unit
    tests to Android."
    ▪ Android sandbox, off
    device

    View Slide

  18. Espresso
    ▪ UI Testing
    ▪ "Use Espresso to
    write concise,
    beautiful, and reliable
    Android UI tests."

    View Slide

  19. MockWebServer
    ▪ Stub network
    responses
    ▪ "A scriptable web
    server for testing
    HTTP clients."

    View Slide

  20. 4.
    Legacy apps
    What if it's not a new app?

    View Slide

  21. Understand your
    boundaries
    ▪ Get familiar with
    architecture
    ▪ Think about
    constraints

    View Slide

  22. High-Level Tests
    ▪ Usually Espresso
    tests
    ▪ Mock out network

    View Slide


  23. ...break the app up into components
    and refactor things a component at a
    time.

    View Slide

  24. Refactoring
    ▪ Make code more
    testable
    ▪ Helped by high-level
    tests

    View Slide

  25. Refactor Tests
    ▪ As code changes,
    your tests will too

    View Slide

  26. Handling Test Data
    ▪ Tests also need to be
    maintained

    View Slide

  27. TDD is hard
    ▪ It takes practice
    ▪ You'll make mistakes

    View Slide

  28. Android Test-Driven
    Development by Tutorials
    A book that teaches you to
    write sustainable, testable
    apps, as well as to apply
    testing strategies to legacy
    projects via Espresso and
    UI tests, code coverage
    and refactoring.

    View Slide

  29. Thanks!
    @TTGonda
    VictoriaGonda.com

    View Slide