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

Love your tests

Tuenti
November 27, 2015

Love your tests

Testing practices and principles used at .Tuenti in order to get better tests and make them easier to write and read.

Tuenti

November 27, 2015
Tweet

More Decks by Tuenti

Other Decks in Technology

Transcript

  1. Love your tests
    @KINISOFTWARE
    MADRID · NOV 27-28 · 2015

    View full-size slide

  2. .Love your tests
    (Story of a healthy addiction - Chapter 2)
    kinisoftware [email protected]

    View full-size slide

  3. We are
    hiring!!

    View full-size slide

  4. Once upon a time…
    2013
    1

    View full-size slide

  5. .Tuenti <3 .Testing
    (Story of a healthy addiction - Chapter 1)

    View full-size slide

  6. • We <3 our customers
    • Confidence
    • Safety Net
    • Refactoring, refactoring, refactoring
    • It is professional, guys :)

    View full-size slide

  7. By
    Martin Fowler

    View full-size slide

  8. Present day…
    2

    View full-size slide

  9. “It’s overwhelmingly easy to write bad
    unit tests that add very little value to a
    project while inflating the cost of code
    changes astronomically.”

    View full-size slide

  10. • Code coverage != Test quality
    • Don’t Repeat Yourself (Globally)
    • Mocks, mocks everywhere
    • Test smells

    View full-size slide

  11. .Tuenti
    Subject Under Test
    3

    View full-size slide

  12. Microservices
    Fresh start
    Java
    Focus on testability

    View full-size slide

  13. Talk is cheap.
    Show me the code.

    View full-size slide

  14. Coding time (1446 tests)
    •Code readability + Naming + Code structure + DSL
    • One (logical) assert per test + Meaningful asserts + Custom
    matchers
    • Exceptions
    • Builders + Mothers
    • Mock providers
    • Testing real things
    • Customize your runners + DI

    View full-size slide

  15. Code readability + Naming + Code
    structure + DSL
    • Unit & Integration folders
    • Tests names
    • AAA
    • @SetUp vs Explicit SetUpTM
    • DSL

    View full-size slide

  16. • Unit & Integration folders

    View full-size slide

  17. Code readability + Naming + Code
    structure + DSL
    • Tests names

    View full-size slide

  18. Code readability + Naming + Code
    structure + DSL
    • AAA

    View full-size slide

  19. Code readability + Naming + Code
    structure + DSL
    • @SetUp vs Explicit SetUpTM

    View full-size slide

  20. Code readability + Naming + Code
    structure + DSL

    View full-size slide

  21. Code readability + Naming + Code
    structure + DSL
    • DSL

    View full-size slide

  22. One (logical) assert per test +
    Meaningful asserts + Custom matchers
    • Assert Equals vs Assert That
    • Actual & Expected
    • One (logical) assert per test
    • Hamcrest & Assert J
    • Custom matchers

    View full-size slide

  23. • Assert Equals vs Assert That / Actual & Expected
    One (logical) assert per test +
    Meaningful asserts + Custom matchers

    View full-size slide

  24. One (logical) assert per test +
    Meaningful asserts + Custom matchers
    • One (logical) assert per test

    View full-size slide

  25. One (logical) assert per test +
    Meaningful asserts + Custom matchers
    • Hamcrest

    View full-size slide

  26. • Assert J

    View full-size slide

  27. One (logical) assert per test +
    Meaningful asserts + Custom matchers
    • Custom matchers

    View full-size slide

  28. Exceptions
    • Try/Catch… really?
    • @Test(expected = ….)
    • @Rule ExpectedException

    View full-size slide

  29. Exceptions
    • @Test(expected = ….)
    • @Rule ExpectedException

    View full-size slide

  30. Builders + Mothers
    • Value Objects are leaf in our classes tree == No mocks here please
    • Building value objects “issue”
    • Builders
    • Mothers
    • Builders + Mothers

    View full-size slide

  31. Builders + Mothers
    • Builders

    View full-size slide

  32. Builders + Mothers

    View full-size slide

  33. Mock providers
    • DRY using mocks globally
    • DSL
    • Builders + Mocks = Mocks providers

    View full-size slide

  34. Testing using real things
    • Mocks, mocks everywhere
    • Sociable Unit Tests
    • Local resources
    • Helper & Utility classes

    View full-size slide

  35. Testing using real things
    • Helper & Utility classes

    View full-size slide

  36. Customize your runners + DI
    • JUnit @RunWith + your platform
    • SetUp and TearDown for expensive resources
    • DI container and “integration mocks”

    View full-size slide

  37. Customize your runners + DI
    • JUnit @RunWith + your platform

    View full-size slide

  38. Customize your runners + DI
    • SetUp and TearDown for expensive resources

    View full-size slide

  39. Customize your runners + DI
    • SetUp and TearDown for expensive resources

    View full-size slide

  40. Customize your runners + DI

    View full-size slide

  41. • Mix JVM Languages (Groovy&Scala rule!)
    • Java 8 fuck yeah!
    • JUnit Lambda
    • Property-based testing
    • Mutation Testing
    Keep an eye on…

    View full-size slide

  42. Reading a lot
    • Effective Unit Testing
    • WEWUT
    • Pragmatic Unit Testing with JUnit
    • GOOS
    • The Art of Unit Testing
    • JUnit in Action
    • xUnit Patterns
    • Refactoring

    View full-size slide