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

Test-Driven Development to avoid test painful

Test-Driven Development to avoid test painful

presented at CakeFest 2019

Kazuki Higashiguchi
PRO

November 09, 2019
Tweet

More Decks by Kazuki Higashiguchi

Other Decks in Technology

Transcript

  1. © - BASE, Inc.
    Test-Driven Development
    to avoid test painful
    CakeFest
    . . - @hgsgtk

    View Slide

  2. © - BASE, Inc.
    Common story
    Programmer
    It works. I’m
    done!
    Production code (but No test code)
    Reviewer

    View Slide

  3. © - BASE, Inc.
    Common story
    Programmer
    Production code (but No test code)
    Reviewer
    Write a test,
    please.

    View Slide

  4. © - BASE, Inc.
    Common story
    Programmer
    It is a pain in the
    neck, but I got it.
    Production code (but No test code)
    Reviewer

    View Slide

  5. © - BASE, Inc.
    Test painful
    https://unsplash.com/photos/ajByNgzbklo

    View Slide

  6. © - BASE, Inc.
    Common story
    Programmer
    It works, but it’s
    difficult to write a test
    code.
    Production code (but Hard-to-Test Code)

    View Slide

  7. © - BASE, Inc.
    Common story
    Programmer
    Hmm
    Production code (but Hard-to-Test Code)

    View Slide

  8. © - BASE, Inc. https://unsplash.com/photos/VBe zj-JHBs
    Test painful

    View Slide

  9. https://unsplash.com/photos/jNrGkkvYk
    TDD solve these
    problems

    View Slide

  10. © - BASE, Inc.
    What this talk is about
    • Unit Test
    • Explain what is a test painful
    • Use Test-Driven Development to solve test
    painful

    View Slide

  11. © - BASE, Inc.
    What this talk is NOT about
    • End-to-End Test
    • PHPUnit basic how-to
    • Test executing environment (ex. CI)

    View Slide

  12. © - BASE, Inc.
    var_dump($hgsgtk);
    : @hgsgtk
    Kazuki Higashiguchi
    BASE BANK, Inc. / Dev Division / Tech Lead
    Software Developer
    CakePHP
    - use for business
    - contributor
    - cakephp/cakephp, cakephp/docs

    View Slide

  13. © - BASE, Inc.
    Agenda
    .Causes of test painful
    .Test-Driven Development
    .How to Use Test-Driven Development
    .Summary

    View Slide

  14. © - BASE, Inc.
    Agenda
    .Causes of test painful
    .Test-Driven Development
    .How to Use Test-Driven Development
    .Summary

    View Slide

  15. © - BASE, Inc.
    Cause
    Cause
    Cause
    Cause
    Cause
    Various causes of test painful
    Programmer
    Testing is terrible!
    Cause

    View Slide

  16. © - BASE, Inc.
    Various causes of test painful
    .No knowledge how to write unit tests
    .Too busy projects
    .No test code in production code
    .Too much bother to write tests
    .Hard-to-Test Code
    .Unmaintainable test codes

    View Slide

  17. . No knowledge how to write tests
    . Too busy projects
    . No test code in production code
    . Too much bother to write tests
    . Hard-to-Test Code
    . Unmaintainable test codes
    - Causes - - Possibly solutions -
    . Learn how to write tests
    . Improve project management
    . Legacy code improvement
    . Write the tests first
    . Testable code design
    . Tests with high readability and
    maintainability

    View Slide

  18. . No knowledge how to write tests
    . Too busy projects
    . No test code in production code
    . Too much bother to write tests
    . Hard-to-Test Code
    . Unmaintainable test codes
    - Causes - - Possibly solutions -
    . Learn how to write tests
    . Improve project management
    . Legacy code improvement
    . Write the tests first
    . Testable code design
    . Tests with high readability and
    maintainability
    FOCUS ON

    View Slide

  19. © - BASE, Inc.
    A case “Too much bother to write tests”
    Programmer
    It is a pain in the
    neck, but I got it.
    Production code (but No test code)
    Reviewer
    Write a test,
    please.

    View Slide

  20. © - BASE, Inc.
    Why does this happen?
    • In most cases, a programmer write test codes
    after writing production codes
    Programmer
    Write
    production codes
    Write
    test codes

    View Slide

  21. © - BASE, Inc.
    Why does this happen?
    • Writing tests may become “optional” process
    Programmer
    Write
    production codes
    Write
    test codes
    It works. I’m
    done!
    But I have to write
    test codes.

    View Slide

  22. © - BASE, Inc.
    Feel test painful
    https://unsplash.com/photos/eQTHhpTkBTY

    View Slide

  23. © - BASE, Inc.
    A case “Hard-to-Test Code”
    Programmer
    It works, but it’s
    difficult to write a test
    code.
    Hard-to-Test Code
    Hmm

    View Slide

  24. © - BASE, Inc.
    Hard-to-Test Code
    “Hard-to-Test Code is one factor that makes it
    difficult to write complete, correct automated
    tests in a cost-effective manner.”
    Meszarosf, Gerard. xUnit Test Patterns: Refactoring Test Code / Chapter . Code Smells

    View Slide

  25. © - BASE, Inc.
    Why does this happen?
    Programmer
    Programmer
    smooth-sailing
    production code
    (in the middle of coding it)
    complete
    everything
    Testable
    design
    In the middle of coding, a programmer doesn’t
    take care testable design

    View Slide

  26. © - BASE, Inc.
    Why does this happen?
    Programmer
    Programmer
    It works!
    production code
    (It’s done)
    complete
    everything
    Hard-to-
    Test Code
    No test code is often difficult to write tests
    But untestable

    View Slide

  27. © - BASE, Inc.
    Feel test painful
    https://unsplash.com/photos/_cbKur I A

    View Slide

  28. © - BASE, Inc.
    Summary “ . Causes of test painful”
    • There are various causes of test painful
    • Focus on two causes
    .Too much bother to write tests
    .Hard-to-Test Code

    View Slide

  29. © - BASE, Inc.
    Agenda
    .Causes of test painful
    .Test-Driven Development
    .How to Use Test-Driven Development
    .Summary

    View Slide

  30. https://unsplash.com/photos/jNrGkkvYk
    TDD

    View Slide

  31. © - BASE, Inc.
    Test-Driven Development (TDD)
    • The goal of TDD is “Clean code that works”
    • A way of managing fear during programming
    • Drive development by automated testing

    View Slide

  32. © - BASE, Inc.
    Don’t write a line of new code
    unless you first have a failing
    automated test
    Eliminate duplication
    TDD Rules
    https://www.eecs.yorku.ca/course_archive/ - /W/ /sectionM/case_studies/money/
    KentBeck_TDD_byexample.pdf

    View Slide

  33. © - BASE, Inc.
    TDD Cycle “Red-Green-Refactor”
    RED
    REFACTOR GREEN
    . Failed test
    . Passed
    . Refactor

    View Slide

  34. © - BASE, Inc.
    TDD Cycle
    .Add a little test
    .Run all tests, and fail
    .Make a little change
    .Run all tests and succeed
    .Refactor to remove duplication
    https://www.eecs.yorku.ca/course_archive/ - /W/ /sectionM/case_studies/money/
    KentBeck_TDD_byexample.pdf

    View Slide

  35. © - BASE, Inc.
    TDD steps
    .Make a to-do list to remind us what all we
    need to do
    .Work on the TDD cycle
    • “Temporary implementation” for changing
    from red to green quickly
    • Add to-do you notices along the way

    View Slide

  36. DEMONSTRATION
    https://unsplash.com/photos/xt tb oa o

    View Slide

  37. by CakePHP .

    View Slide

  38. © - BASE, Inc.
    To-do list
    Article details API
    • Article details API request returns success
    response
    • Article details API returns article resource

    View Slide

  39. © - BASE, Inc.
    . Add a little test
    RED
    REFAC
    TOR
    GREEN
    DEMO

    View Slide

  40. © - BASE, Inc.
    Possibly related to
    Cake\Controller\Exception\MissingActionE
    xception: "Action ArticlesController::view()
    could not be found, or is not accessible."
    . Run all tests, and fail DEMO

    View Slide

  41. © - BASE, Inc.
    . Make a little change
    RED
    REFAC
    TOR
    GREEN
    DEMO

    View Slide

  42. © - BASE, Inc.
    OK ( test, assertion)
    . Run all tests and succeed DEMO

    View Slide

  43. © - BASE, Inc.
    To-do list
    Article details API
    • Article details API request returns success
    response
    • Article details API returns article resource
    DEMO

    View Slide

  44. © - BASE, Inc.
    . Add a little test
    RED
    REFAC
    TOR
    GREEN
    DEMO

    View Slide

  45. © - BASE, Inc.
    Failed asserting that '[]'
    matches JSON string
    “{ }”.
    . Run all tests, and fail DEMO

    View Slide

  46. © - BASE, Inc.
    . Make a little change
    RED
    REFAC
    TOR
    GREEN
    DEMO

    View Slide

  47. © - BASE, Inc.
    OK ( test, assertion)
    . Run all tests and succeed DEMO

    View Slide

  48. © - BASE, Inc.
    . Refactor
    RED
    REFAC
    TOR
    GREEN
    DEMO

    View Slide

  49. © - BASE, Inc.
    . Refactor
    RED
    REFAC
    TOR
    GREEN
    DEMO

    View Slide

  50. © - BASE, Inc.
    To-do list
    Article details API
    • Article details API request returns success
    response
    • Article details API returns article resource
    DEMO

    View Slide

  51. © - BASE, Inc.
    Principles for TDD: “ -As” pattern
    • Use separate code paragraphs for each of the
    As.
    • -As
    • Arrange: variable declaration and initialization
    • Act: invoking the code under test
    • Assert: verify that expectations
    • This pattern makes it easy to revisit test code
    https://integralpath.blogs.com/thinkingoutloud/ / /principles_of_t.html

    View Slide

  52. © - BASE, Inc.
    Summary “ . Test-Driven Development”
    • The goal of TDD is “Clean code that works”
    • TDD cycle “Red-Green-Refactor”
    • Make a todo list and drive TDD cycle

    View Slide

  53. © - BASE, Inc.
    Agenda
    .Causes of test painful
    .Test-Driven Development
    .How to Use Test-Driven Development
    .Summary

    View Slide

  54. . No knowledge how to write tests
    . Too busy projects
    . No test code in production code
    . Too much bother to write tests
    . Hard-to-Test Code
    . Unmaintainable test codes
    - Causes - - Possibly solutions -
    . Learn how to write tests
    . Improve project management
    . Legacy code improvement
    . Write the tests first
    . Testable code design
    . Tests with high readability and
    maintainability
    FOCUS ON

    View Slide

  55. © - BASE, Inc.
    TDD solves two causes of test painful
    TDD
    • Test First
    • TDD Cycle
    Hard-to-Test Code
    Too much bother
    to write tests
    - Causes -

    View Slide

  56. © - BASE, Inc.
    TDD solves the cause “Too much bother to write tests”
    TDD
    • Test First
    • TDD Cycle
    Hard-to-Test Code
    Too much bother
    to write tests
    - Causes -
    Write the tests
    in TDD cycle

    View Slide

  57. © - BASE, Inc.
    Too much bother to write tests
    • In most cases, a programmer write test codes
    after writing production codes
    Programmer
    Write
    production codes
    Write
    test codes
    BEFORE

    View Slide

  58. © - BASE, Inc.
    Too much bother to write tests
    • Write test codes and production codes in TDD
    cycle
    • Write the tests first
    Programmer
    AFTER
    RED
    REFACT
    OR
    GREEN
    Write
    production codes
    Write
    test codes

    View Slide

  59. © - BASE, Inc.
    TDD solves the cause “Hard-to-Test Code”
    TDD
    • Test First
    • TDD Cycle
    Hard-to-Test Code
    Too much bother
    to write tests
    - Causes -
    Listening to the Tests

    View Slide

  60. © - BASE, Inc.
    Listening to the tests
    • TDD is about feedback on the code’s internal
    qualities
    • ex. coupling cohesion of classes, dependencies, effective information
    hiding
    Freeman, Steve. Growing Object-Oriented Software, Guided by Tests
    Chapter . Listening to the Tests

    View Slide

  61. © - BASE, Inc.
    Hard-to-Test Code
    Programmer
    Programmer
    It works!
    production code
    (It’s done)
    complete
    everything
    UnTestable
    design
    No test code is often difficult to write tests
    But untestable
    BEFORE

    View Slide

  62. © - BASE, Inc.
    Hard-to-Test Code
    Programmer
    Programmer
    production codes
    (with test codes)
    complete
    everything
    Testable
    design
    They becomes to be careful to make codes
    testable
    AFTER
    RED
    REFACT
    OR
    GREEN

    View Slide

  63. CASE STUDIES
    https://unsplash.com/photos/oBI NBP Lhg

    View Slide

  64. © - BASE, Inc.
    Can’t replace dependencies
    Bloated class/function
    Case studies

    View Slide

  65. © - BASE, Inc.
    Can’t replace dependencies
    Bloated class/function
    Case studies

    View Slide

  66. © - BASE, Inc.
    Example
    • A method to charge on order
    • Includes communication to external payment
    services

    View Slide

  67. © - BASE, Inc.
    Hard-to-Test Code BEFORE

    View Slide

  68. © - BASE, Inc.
    Hard-to-Test Code BEFORE
    It’s difficult to inject CardCharge class
    We can’t replace the dependency
    (without Magic)

    View Slide

  69. © - BASE, Inc.
    . Write a little test AFTER
    • Consider Dependency
    Injection to write a test
    • It is one of benefits to
    write a test first

    View Slide

  70. © - BASE, Inc.
    Can’t replace dependencies
    Bloated class/function
    Case studies

    View Slide

  71. © - BASE, Inc.
    Example
    • A controller action to handler order charge
    request
    • Save order resource
    • View order page

    View Slide

  72. © - BASE, Inc.
    Hard-to-Test Code BEFORE
    • A controller action

    View Slide

  73. © - BASE, Inc.
    Hard-to-Test Code BEFORE
    • Handle both requests

    View Slide

  74. © - BASE, Inc.
    Hard-to-Test Code BEFORE
    • The business logic in it

    View Slide

  75. © - BASE, Inc.
    Hard-to-Test Code BEFORE
    • Also there are another
    concerns

    View Slide

  76. © - BASE, Inc.
    Hard-to-Test Code BEFORE
    Mixed multiple use cases (request)
    Many procedures
    Hard-to-Test business logic code

    View Slide

  77. © - BASE, Inc.
    Make a to-do list
    • View the order page
    • Redirect when saving an order is succeeded
    • Calculate total amount of an order
    • Save order resource
    • Send charge request to external payment
    service
    • etc
    AFTER

    View Slide

  78. © - BASE, Inc.
    Run TDD cycle for each to-do
    • View the order page
    • Redirect when saving an order is succeeded
    • Calculate total amount of an order
    • Save order resource
    • Send charge request to external payment service
    • etc
    AFTER
    RED
    REFACT
    OR
    GREEN

    View Slide

  79. © - BASE, Inc.
    For instance
    • View the order page
    • Redirect when saving an order is succeeded
    • Calculate total amount of an order
    • Save order resource
    • Send charge request to external payment service
    • etc
    AFTER
    RED
    REFACT
    OR
    GREEN

    View Slide

  80. © - BASE, Inc.
    Calculate total amount of an order
    • This is a important logic, so many test patterns
    are needed
    • It may be better to write it in Entity
    AFTER

    View Slide

  81. © - BASE, Inc.
    . Write a little test AFTER

    View Slide

  82. © - BASE, Inc.
    Business logic in Model AFTER

    View Slide

  83. © - BASE, Inc.
    “Your designs must consist of many highly
    cohesive, loosely coupled components, just to
    make testing easy”
    https://www.eecs.yorku.ca/course_archive/ - /W/ /sectionM/case_studies/money/
    KentBeck_TDD_byexample.pdf
    A benefit of TDD

    View Slide

  84. © - BASE, Inc.
    Summary “ . How to Use Test-Driven Development”
    • TDD solves two causes of test painful
    .Too much bother to write tests
    .Hard-to-Test Code

    View Slide

  85. © - BASE, Inc.
    Agenda
    .Causes of test painful
    .Test-Driven Development
    .How to Use Test-Driven Development
    .Summary

    View Slide

  86. TDD solve test
    painful
    https://unsplash.com/photos/jvBXiynINGE

    View Slide

  87. Test is a design tool
    https://unsplash.com/photos/IClZBVw W A

    View Slide

  88. © - BASE, Inc.
    The solution to the problem of costly tests,
    however, is not to stop testing but instead to get
    better at it.
    Metz, Sandi. Practical Object-Oriented Design / Chapter Designing Cost-Effective Tests

    View Slide

  89. Any Questions?

    View Slide