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

Beyond Unit Testing: The Real Value of Test-Driven Development - PhillyXP

Beyond Unit Testing: The Real Value of Test-Driven Development - PhillyXP

Twenty years after the public introduction of Test-Driven Development (TDD), in Kent Beck's Extreme Programming Explained, it remains a commonly misunderstood and underutilized practice. While there are many who espouse the benefits of TDD, misconceptions about the practice and its perceived learning curve serve to dampen its impact on the software industry.

This talk introduces the misconceptions about TDD, as being just another way to write unit tests. It disputes this belief by refocusing on the many benefits of the practice. The talk will detail the resulting sustainable, high-quality test suites that are a product of TDD. Additionally, it will describe the less visible benefits like software design feedback, and improvements to development structure and developer workflow; all of which are possible through the continued practice of TDD.

If you are a software engineer or a technical leader wanting to learn more about TDD, looking for the motivation to give the practice a try, or are looking for ways to encourage your team(s) to learn the practice, this talk is for you.

Anthony Sciamanna

November 19, 2019
Tweet

More Decks by Anthony Sciamanna

Other Decks in Programming

Transcript

  1. Beyond Unit Testing
    Anthony Sciamanna
    @asciamanna
    anthonysciamanna.com
    The Real Value of Test-Driven Development
    November 19, 2019

    View Slide

  2. • Passionate about Extreme Programming (XP), Lean Software Development, and Modern Agile.
    • Expertise in helping teams and development organizations improve how they work, the quality of their
    code, and their ability to deliver value to their customers.
    • Focusing on technical excellence, continuous improvement, and continuous learning.
    Anthony Sciamanna
    Software Engineer, Technical
    Agile Coach, Blogger & Organizer
    of the PhillyXP user group.
    @asciamanna
    anthonysciamanna.com

    View Slide

  3. TDD Refresher
    @asciamanna
    anthonysciamanna.com
    Image by Industrial Logic Senior Consultant - Tim Ottinger

    View Slide

  4. TDD Refresher
    @asciamanna
    anthonysciamanna.com

    View Slide

  5. TDD Refresher
    @asciamanna
    anthonysciamanna.com

    View Slide

  6. Let’s go back in time!
    @asciamanna
    anthonysciamanna.com

    View Slide

  7. Discovered at Hunter Industries in 2011 by a team
    coached by Woody Zuill
    1994
    A Very Brief History of TDD
    Discovered at Hunter Industries in 2011 by a team
    coached by Woody Zuill
    Kent Beck releases the first version of the SUnit framework
    for Smalltalk
    @asciamanna
    anthonysciamanna.com

    View Slide

  8. Discovered at Hunter Industries in 2011 by a team
    coached by Woody Zuill
    1994
    A Very Brief History of TDD
    Discovered at Hunter Industries in 2011 by a team
    coached by Woody Zuill
    1995
    @asciamanna
    Kent demos TDD to Ward Cunningham at OOPSLA Austin in
    October. Details of the C3 Project start being publicized
    anthonysciamanna.com
    Kent Beck releases the first version of the SUnit framework
    for Smalltalk

    View Slide

  9. Discovered at Hunter Industries in 2011 by a team
    coached by Woody Zuill
    1994
    A Very Brief History of TDD
    Discovered at Hunter Industries in 2011 by a team
    coached by Woody Zuill
    1995
    @asciamanna
    Kent demos TDD to Ward Cunningham at OOPSLA Austin in
    October. Details of the C3 Project start being publicized
    anthonysciamanna.com
    Kent Beck releases the first version of the SUnit framework
    for Smalltalk
    On a flight from Zurich to Atlanta (OOPSLA ‘97) Erich
    Gamma and Kent Beck create JUnit for Java
    1997

    View Slide

  10. Discovered at Hunter Industries in 2011 by a team
    coached by Woody Zuill
    1994
    A Very Brief History of TDD
    Discovered at Hunter Industries in 2011 by a team
    coached by Woody Zuill
    1995
    @asciamanna
    Kent demos TDD to Ward Cunningham at OOPSLA Austin in
    October. Details of the C3 Project start being publicized
    anthonysciamanna.com
    Kent Beck releases the first version of the SUnit framework
    for Smalltalk
    On a flight from Zurich to Atlanta (OOPSLA ‘97) Erich
    Gamma and Kent Beck create JUnit for Java
    1997
    Kent releases Extreme Programming Explained 1999

    View Slide

  11. TDD Is a Testing Practice
    @asciamanna
    anthonysciamanna.com

    View Slide

  12. I don’t care if people write tests first or last.
    As long as they get written.
    @asciamanna


    - Anonymous Tech Lead
    anthonysciamanna.com

    View Slide

  13. @asciamanna
    Ignores Design and Workflow Benefits of TDD
    anthonysciamanna.com

    View Slide

  14. @asciamanna
    Benefits of TDD
    anthonysciamanna.com

    View Slide

  15. @asciamanna
    Benefits of TDD
    Test Suite
    Externally Visible Artifacts
    anthonysciamanna.com

    View Slide

  16. @asciamanna
    Benefits of TDD
    Test Suite
    Externally Visible Artifacts
    anthonysciamanna.com

    View Slide

  17. @asciamanna
    Benefits of TDD
    Test Suite
    Externally Visible Artifacts
    Software
    Design
    anthonysciamanna.com

    View Slide

  18. @asciamanna
    Benefits of TDD
    Test Suite
    Externally Visible Artifacts
    Software
    Design
    Developer
    Workflow
    anthonysciamanna.com

    View Slide

  19. @asciamanna
    Benefits of TDD
    Test Suite
    Software
    Design
    Developer
    Workflow
    Externally Visible Artifacts
    anthonysciamanna.com

    View Slide

  20. @asciamanna
    TDD Triple
    Balance
    Made
    Making
    Maker
    anthonysciamanna.com

    View Slide

  21. @asciamanna
    Benefits of TDD
    Test Suite
    Software
    Design
    Developer
    Workflow
    Externally Visible Artifacts
    anthonysciamanna.com

    View Slide

  22. Test Suite Benefits
    Why we prefer TDD-style tests over Test After Tests

    View Slide

  23. Test Suite
    Quality
    Production
    Code
    Quality
    @asciamanna
    Disclaimer #1
    Test suite quality influences production
    code quality and vice-versa
    anthonysciamanna.com

    View Slide

  24. You Can’t Write Good Tests for Bad Code
    • Wrapping non-testable code in unit tests
    results in brittle, low-quality tests
    • Test-first practices enable writing high
    quality production code and high-quality
    tests
    @asciamanna
    anthonysciamanna.com

    View Slide

  25. @asciamanna
    The TDD Quality Cycle
    anthonysciamanna.com
    Test Suite Production Code
    2. Code under test must be
    testable (loosely coupled)
    Refactoring
    Refactoring
    3. Enables tests to be
    small, precise, fast,
    isolated and independent
    Virtuous
    Cycle
    1. Tests are written first

    View Slide

  26. Microtest
    A short, simple, fast, isolated*,
    test that probes the behavior
    of a single class
    @asciamanna
    *Isolated does not mean we have to mock every dependency
    anthonysciamanna.com

    View Slide

  27. Microtests Don’t
    Access a network
    Access a database
    Access the filesystem
    Depend on other tests
    Require a complex environment to run in
    @asciamanna
    anthonysciamanna.com

    View Slide

  28. S
    O
    S
    @asciamanna
    anthonysciamanna.com

    View Slide

  29. Small
    O
    S
    @asciamanna
    anthonysciamanna.com

    View Slide

  30. Small
    Obvious
    S
    @asciamanna
    anthonysciamanna.com

    View Slide

  31. Small
    Obvious
    Simple
    @asciamanna
    anthonysciamanna.com

    View Slide

  32. Small
    @asciamanna
    ~12 LOC or less per test
    anthonysciamanna.com

    View Slide

  33. @asciamanna
    Obvious
    anthonysciamanna.com

    View Slide

  34. Arrange
    Act
    Assert
    Arrange – Act – Assert
    @asciamanna
    Or
    Given - When - Then
    anthonysciamanna.com

    View Slide

  35. Standardize common test constructs
    Create the object under test in Setup
    Standard name result to be asserted
    Standard name for object under test
    @asciamanna
    anthonysciamanna.com

    View Slide

  36. Simple
    @asciamanna
    Avoid branching
    and flow control
    logic in tests
    anthonysciamanna.com

    View Slide

  37. How To Stop Hating Your Test Suite
    Logic in tests confuses the story of what’s being tested. Test-
    scoped logic is hard to read and errors are easy to miss.
    - Justin Searls, Co-founder Test Double
    @asciamanna
    anthonysciamanna.com

    View Slide

  38. How To Stop Hating Your Test Suite
    Test code is untested code!
    - Justin Searls, Co-founder Test Double
    @asciamanna
    anthonysciamanna.com

    View Slide

  39. @asciamanna
    Test After Test First
    Manual testing often takes
    precedence
    Unit testing at end tends to be
    skipped to hit deadlines
    All code is tested as it is created or
    modified
    Feels like busy work if code is manually
    tested and considered “done”
    The test is the path to changing the
    system
    Code is not influenced by tests to be
    testable
    Code is influenced by the tests to be
    testable
    Tests don’t serve as feedback loops Tests are continuous feedback loops,
    supports refactoring and software design
    Tests don’t get refactored – created at
    end
    Tests are continuously refactored
    Tests are less comprehensive when
    added at end
    Tests are more comprehensive when
    added with each code change
    Coverage as a build gate often required
    (commonly 70% coverage)
    Coverage metrics unnecessary
    anthonysciamanna.com

    View Slide

  40. Software Design Benefits
    Tests guiding quality software design

    View Slide

  41. Disclaimer #2
    @asciamanna
    TDD influences design quality
    anthonysciamanna.com
    But it’s not a silver bullet

    View Slide

  42. @asciamanna
    Disclaimer #2
    TDD alone influences
    design quality
    anthonysciamanna.com
    High Cohesion Loose Coupling
    Small Classes Small Methods

    View Slide

  43. @asciamanna
    Disclaimer #2
    TDD provides a mechanism
    for learning software design
    anthonysciamanna.com
    High Cohesion Loose Coupling
    Small Classes Small Methods

    View Slide

  44. @asciamanna
    Practitioners still need to understand
    the concepts of software design
    Disclaimer #2
    High Cohesion Loose Coupling
    Code Smells Design Patterns
    SOLID
    Refactoring
    Encapsulation
    GRASP
    Connascence
    anthonysciamanna.com

    View Slide

  45. @asciamanna
    Microtests are litmus tests
    for software design quality
    anthonysciamanna.com

    View Slide

  46. Examples
    @asciamanna
    anthonysciamanna.com

    View Slide

  47. Too Much Test Setup
    Too Many Dependencies Too Many Arguments
    Arguments are Too Complex Code Under Test Doing Too Much
    @asciamanna
    anthonysciamanna.com

    View Slide

  48. Multiple ‘Act’ Steps
    Temporal Coupling Exists
    Complicated API
    Client needs to know too
    much about object under test
    @asciamanna
    Too Many Steps to Invoke
    Method Under Test
    anthonysciamanna.com

    View Slide

  49. Too Many Asserts
    Returning too complex of a type
    Code under test doing too much
    @asciamanna
    anthonysciamanna.com

    View Slide

  50. Too Many Test Cases
    Too much branching logic
    Code under test is
    doing too much
    @asciamanna
    anthonysciamanna.com

    View Slide

  51. Client-First Design / Outside-In
    TDD enables software design from a client’s perspective.
    Leads to better designed interfaces between objects
    @asciamanna
    anthonysciamanna.com

    View Slide

  52. Refactoring into design yields a better design than Big Design Up Front
    Requirements Design Code Test
    Waterfall Mindset
    Requirements Test Code
    Design
    (Refactor)
    (Test-Driven) Agile Mindset
    Slide courtesy of Mike Rieser (@MichaelRieser)
    @asciamanna
    anthonysciamanna.com

    View Slide

  53. Developer Workflow Benefits
    Short, fast feedback cycles improve productivity

    View Slide

  54. Nearly instantaneous
    feedback loops
    @asciamanna
    anthonysciamanna.com

    View Slide

  55. Fast Feedback Loops
    key to developer
    productivity & high
    performing teams
    @asciamanna
    anthonysciamanna.com

    View Slide

  56. @asciamanna
    anthonysciamanna.com

    View Slide

  57. Resilient to Interruptions
    @asciamanna
    anthonysciamanna.com

    View Slide

  58. TDD-created Microtests are save states for system functionality
    @asciamanna
    anthonysciamanna.com

    View Slide

  59. @asciamanna
    anthonysciamanna.com

    View Slide

  60. @asciamanna
    anthonysciamanna.com
    BIG PROBLEM

    View Slide

  61. @asciamanna
    anthonysciamanna.com
    BIG PROBLEM
    Great software is created by GREAT TEAMS not heroic, rockstar, 10X
    developers working in isolation

    View Slide

  62. @asciamanna
    anthonysciamanna.com
    BIG PROBLEM
    Software development is a communication game
    Communication is the professional developer’s first order of
    business. Perhaps you thought that ‘getting it working’ was the
    first order of business for a professional developer. I hope by now,
    however, this book has disabused you of that idea.
    - Robert Martin, Clean Code

    View Slide

  63. System is always in a working state
    @asciamanna
    anthonysciamanna.com

    View Slide

  64. Tiny Validations
    @asciamanna
    anthonysciamanna.com

    View Slide

  65. @asciamanna
    anthonysciamanna.com
    GeePaw Hill – TDD & the Lump of Coding Fallacy
    https://www.geepawhill.org/2018/04/14/tdd-the-lump-of-coding-fallacy/

    View Slide

  66. @asciamanna
    anthonysciamanna.com
    GeePaw Hill – TDD & the Lump of Coding Fallacy
    https://www.geepawhill.org/2018/04/14/tdd-the-lump-of-coding-fallacy/

    View Slide

  67. @asciamanna
    anthonysciamanna.com
    GeePaw Hill – TDD & the Lump of Coding Fallacy
    https://www.geepawhill.org/2018/04/14/tdd-the-lump-of-coding-fallacy/

    View Slide

  68. @asciamanna
    anthonysciamanna.com
    GeePaw Hill – TDD & the Lump of Coding Fallacy
    https://www.geepawhill.org/2018/04/14/tdd-the-lump-of-coding-fallacy/

    View Slide

  69. Mental Benefits of TDD
    One of the hardest things to communicate about Test-Driven
    Development is the mental state it puts you in…On the original C3
    project…progress was steady and unhurried; because it was unhurried it
    seemed slow, but looking back on how much we got done, it was clear
    that despite the unhurried feeling progress was really fast.


    Martin Fowler - Afterword - Kent Beck’s TDD By Example
    @asciamanna
    anthonysciamanna.com

    View Slide

  70. Mental Benefits of TDD
    Concrete and visible progress on small incremental goals
    @asciamanna
    anthonysciamanna.com

    View Slide

  71. Mental Benefits of TDD
    Achieving Profluence
    @asciamanna
    anthonysciamanna.com

    View Slide

  72. Mental Benefits of TDD
    Little or no time spent in the
    debugger or on repetitive,
    time consuming manual
    checks
    @asciamanna
    anthonysciamanna.com

    View Slide

  73. Learning TDD will help improve software design, developer
    workflow, increases developer happiness, and it’s a lot of fun!
    https://geepawhill.org
    anthonysciamanna.com
    https://github.com/testdouble/contributing-tests/wiki
    Test Double – Contributing Tests Wiki
    Industrial Logic
    eLearning
    https://www.industriallogic.com/elearning/
    @asciamanna

    View Slide

  74. Learning TDD will help improve software design, developer
    workflow, increases developer happiness, and it’s a lot of fun!
    anthonysciamanna.com @asciamanna
    https://schmonz.com/2018/08/14/lean-agile-edinburgh-
    august-2018-programming-for-nonprogrammers/
    https://pragprog.com/magazines/2011-11/testdriven-development
    Test-Driven Development: A Guide for Non-Programmers by Jeff Langr
    Lean Agile Edinburgh August 2018: Programming For Non-Programmers
    by Amitai Schleier

    View Slide

  75. @asciamanna
    http://bit.ly/phillyxp-beyond-unit-testing
    anthonysciamanna.com

    View Slide

  76. Attributes
    • Pygmy Marmoset - https://www.flickr.com/photos/gopak/39889008421
    • SOS - https://www.flickr.com/photos/itupictures/16654290295
    • Darts - https://www.flickr.com/photos/mike-burns/374198933
    • Quote - Nick Youngson CC BY-SA 3.0
    • Silver Bullet - https://www.flickr.com/photos/moneymetals/34301000331
    • Litmus test - https://commons.wikimedia.org/wiki/File:Blue_and_red_litmus_paper.JPG
    • Self-operating napkin - https://upload.wikimedia.org/wikipedia/commons/8/88/Self-
    operating_napkin_%28Rube_Goldberg_cartoon_with_caption%29.jpg
    • Glass Cockpit - https://upload.wikimedia.org/wikipedia/commons/9/9f/C-5M_Cockpit.jpg
    • Public Domain, https://commons.wikimedia.org/w/index.php?curid=2466026
    • Stack of Papers - https://upload.wikimedia.org/wikipedia/commons/b/b6/FileStack_retouched.jpg
    • El Diablo - https://en.wikipedia.org/wiki/Fire_Ball#/media/File:El_Diablo_Roller_Coaster.jpg
    • Creative Commons license: https://creativecommons.org/licenses/by/2.0/

    View Slide