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

I've done TDD wrong all the time (with livecode)

I've done TDD wrong all the time (with livecode)

Presentation done at ShareIt, with livecode

Repository available here: https://github.com/gixlg/bookstore

Luca Giuberti

November 23, 2023
Tweet

More Decks by Luca Giuberti

Other Decks in Programming

Transcript

  1. © 2022 Thoughtworks
    I’ve done TDD wrong
    all the time
    1

    View full-size slide

  2. © 2022 Thoughtworks
    Test driven development
    2
    Write a
    failing test
    Make the
    test pass
    Refactor
    TDD

    View full-size slide

  3. © 2022 Thoughtworks
    A common story
    3

    View full-size slide

  4. © 2022 Thoughtworks
    The benefit that was promised
    4
    Remove the fear
    of refactoring
    VS
    Unit test Mock

    View full-size slide

  5. © 2022 Thoughtworks
    What are Unit tests and Mocks
    5
    Unit test
    Mock
    Mock
    Mock
    Class
    reference

    View full-size slide

  6. © 2022 Thoughtworks
    Example
    6

    View full-size slide

  7. © 2022 Thoughtworks
    Bookstore
    7
    Has been asked to implement a ethical
    bookstore that that doesn't accept money
    It has two operation:
    ● Withdraw a book
    ● Deposit a book
    Withdraw a book it cost 2 credit points.
    If we have at least 5 credit we get a discount
    of 1 credit point.
    Deposit a book gives 1 credit point.
    If we have 10 or more credit points he will
    receive 1 credit point as extra bonus.

    View full-size slide

  8. © 2022 Thoughtworks 8

    View full-size slide

  9. © 2022 Thoughtworks
    Book store - State pattern
    9
    1-2
    Contributor
    member
    Community
    member
    4-5
    Affiliate
    member
    9-10
    Premium
    member
    Deposit
    Withdraw
    0
    +1 +1
    -2 -1
    +1 +2
    -1
    (Discount) (Discount)
    (Bonus)

    View full-size slide

  10. © 2022 Thoughtworks 10
    What we have now
    ● The tests don’t speak about “What” the application does
    but they are speaking about “How”
    ● Tests are far to be a design tool
    ● Fragile tests
    ● For a few moment we gave up our safety-net

    View full-size slide

  11. © 2022 Thoughtworks 11
    I’ve made a mistake

    View full-size slide

  12. © 2022 Thoughtworks
    Is TDD really helpful?
    12
    … or are we missing something?

    View full-size slide

  13. © 2022 Thoughtworks
    What happened
    13
    Tests/mocks became an obstacle
    to the refactoring
    Write a
    failing test
    Make the
    test pass
    Refactor
    TDD
    We changed our code for some
    implementation details

    View full-size slide

  14. © 2022 Thoughtworks
    What do we do when we write a test?
    14
    We are defining a contract
    that the code will fulfill

    View full-size slide

  15. © 2022 Thoughtworks
    What do we do when we write a test?
    15
    We are defining a contract
    that the code will fulfill
    If it’s tight to implementation
    details it will easily breaks

    View full-size slide

  16. © 2022 Thoughtworks
    What is a Unit test?
    16
    16
    © 2021 Thoughtworks

    View full-size slide

  17. © 2022 Thoughtworks
    Unit test Behaviour
    Not methods or classes
    17

    View full-size slide

  18. © 2022 Thoughtworks
    Tests should be
    coupled to the behavior of code and
    decoupled from the structure of code
    18
    Kent Beck

    View full-size slide

  19. © 2022 Thoughtworks
    TDD is not really about test but more about
    behavior and requirement
    19
    TDD // BDD // ATDD // Module test

    View full-size slide

  20. © 2022 Thoughtworks
    If the requirement doesn’t change
    the test shouldn't change either
    20

    View full-size slide

  21. © 2022 Thoughtworks 21
    21
    Lots of confusion has
    been made around the
    name Unit test
    Coarse grained unit test
    Tests are coupled to the
    behaviours
    Fine grained unit test
    Tests are coupled to the
    implementation

    View full-size slide

  22. © 2022 Thoughtworks
    The Test Diamond
    22
    Unit test
    Integration test
    UI test
    VS
    Fine Grained
    Unit test
    Coarse Grained
    Unit test
    Integration test
    UI test

    View full-size slide

  23. © 2022 Thoughtworks 23
    23
    Combinatory logics
    Write tests for the logic condition
    All possible cases from the
    implementation point of view:
    ● 3 + 1 = 4 (n+1)
    Write all possible scenario
    All possible cases from a requirement
    point of view are:
    ● 2^3 = 9 (2^n)
    Give a free book if: A and (B or C)
    i.e.
    A = Deposit operation
    B = Is user’s birthday
    C = User has visited the shop 3 times in a week
    ⚠ trade-off

    View full-size slide

  24. © 2022 Thoughtworks 24
    What should we do when we write a test?
    Put yourself in the user’s shoes,
    tests should be
    executable specification
    Develop against an interface
    Outside-in

    View full-size slide

  25. © 2022 Thoughtworks
    Are Mocks useless then?
    25
    25
    © 2021 Thoughtworks

    View full-size slide

  26. © 2022 Thoughtworks 26
    Mock
    Mocks are useful for external
    dependency
    We should mock the infrastructure layer
    (through an adapter)
    i.e. DB, Kafka, HTTP call

    View full-size slide

  27. © 2022 Thoughtworks 27
    Mock
    Mocks are useful for isolate
    pieces of the application that
    have complex logics
    or isolate subdomains of our application
    ⚠ trade-off

    View full-size slide

  28. © 2022 Thoughtworks
    I’ve found my balance
    28
    That’s how I’ve removed the
    fear of refactoring
    ● Unit tests based on behaviours
    ● Test against an interface
    ● Do not mock logic, mock the
    infrastructure
    Your balance might be different
    🤸

    View full-size slide

  29. © 2022 Thoughtworks
    References
    29
    Books:
    Videos:
    TDD, Where Did It All Go Wrong
    Ian Cooper
    Outside-in diamond ◆ TDD
    Thomas Pierrain
    Automated tests: You won't find it in a book!
    Jacek Milewski
    TDD and Clean Architecture - Driven by Behaviour
    Valentina Cupać

    View full-size slide

  30. © 2022 Thoughtworks 30
    Luca Giuberti
    Senior Developer Consultant
    lucagiuberti
    @gix_lg
    Thank you

    View full-size slide

  31. © 2022 Thoughtworks
    Questions?
    31
    31

    View full-size slide