Slide 1

Slide 1 text

Beyond Unit Testing The Real Value of Test-Driven Development Anthony Sciamanna Senior Consultant Industrial Logic June 23, 2022

Slide 2

Slide 2 text

Anthony Sciamanna Technical Agile & Lean Coach, Trainer, Blogger & Organizer of the PhillyXP user group. Website: https://anthonysciamanna.com Email: anthony@industriallogic.com Twitter: @asciamanna https://www.meetup.com/phillyxp/

Slide 3

Slide 3 text

TDD Refresher Image by Industrial Logic Senior Consultant - Tim Ottinger @asciamanna anthonysciamanna.com Add / Update a test Make it work Make it clean

Slide 4

Slide 4 text

A Tale of Two Hats @asciamanna anthonysciamanna.com Adding Functionality Refactoring Writing a failing test or making it pass Small, behavior preserving code improvements You can only wear one hat at a time! https://martinfowler.com/articles/workflowsOfRefactoring/#2hats

Slide 5

Slide 5 text

@asciamanna anthonysciamanna.com TDD Refresher Image courtesy of Bill Wake – TDD, TCR, and Frequent Commits

Slide 6

Slide 6 text

TDD Refresher @asciamanna anthonysciamanna.com

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

TD Refresher A Very Brief History of TDD 1994 Kent Beck releases the first version of the SUnit framework for Smalltalk @asciamanna anthonysciamanna.com

Slide 9

Slide 9 text

TD Refresher A Very Brief History of TDD 1994 1995 Kent demos TDD to Ward Cunningham at OOPSLA Austin in October. Details of the C3 Project start being publicized Kent Beck releases the first version of the SUnit framework for Smalltalk @asciamanna anthonysciamanna.com

Slide 10

Slide 10 text

TD Refresher A Very Brief History of TDD 1994 1995 1997 Kent demos TDD to Ward Cunningham at OOPSLA Austin in October. Details of the C3 Project start being publicized 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 @asciamanna anthonysciamanna.com

Slide 11

Slide 11 text

TD Refresher A Very Brief History of TDD 1994 1995 1997 1999 Kent demos TDD to Ward Cunningham at OOPSLA Austin in October. Details of the C3 Project start being publicized 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 Kent releases Extreme Programming Explained @asciamanna anthonysciamanna.com

Slide 12

Slide 12 text

TDD Is a Testing Practice @asciamanna anthonysciamanna.com

Slide 13

Slide 13 text

@asciamanna anthonysciamanna.com I don’t care if people write tests first or last. As long as they get written. - Anonymous Tech Lead

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Benefits of TDD @asciamanna anthonysciamanna.com

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

You Can’t Write Good Tests for Bad Code • Wrapping untestable code in unit tests results in brittle, low-quality tests, that suffer from false failures • Test-first practices enable writing high quality production code and high- quality tests Markus Spiske for Tech Daily @asciamanna anthonysciamanna.com

Slide 24

Slide 24 text

@asciamanna anthonysciamanna.com

Slide 25

Slide 25 text

The Test Pyramid Microtests / Unit tests Integration / Acceptance tests E2E / UI tests As scope increases, detail should decrease Increase in: • Cost to develop and maintain • Execution time • Possibility of false negatives • Time to identify source of issue @asciamanna anthonysciamanna.com https://anthonysciamanna.com/2019/10/20/avoiding-automated-testing-pitfalls.html More integration More isolation

Slide 26

Slide 26 text

The TDD Quality Cycle 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. We start in the test @asciamanna anthonysciamanna.com

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

FIRST Properties of Microtests https://medium.com/pragmatic-programmers/unit-tests-are-first-fast-isolated-repeatable-self-verifying-and-timely-a83e8070698e Created by Industrial Logic Senior Consultants Tim Ottinger & Brett Schuchert

Slide 30

Slide 30 text

S O S anthonysciamanna.com @asciamanna

Slide 31

Slide 31 text

Small O S anthonysciamanna.com @asciamanna

Slide 32

Slide 32 text

Small Obvious S anthonysciamanna.com @asciamanna

Slide 33

Slide 33 text

Small Obvious Simple anthonysciamanna.com @asciamanna

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Obvious anthonysciamanna.com @asciamanna

Slide 36

Slide 36 text

Arrange Act Assert anthonysciamanna.com @asciamanna Arrange-Act-Assert

Slide 37

Slide 37 text

anthonysciamanna.com @asciamanna

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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 anthonysciamanna.com @asciamanna

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

anthonysciamanna.com @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 Tests implementation details Tests behavior VS.

Slide 43

Slide 43 text

Software Design Benefits Tests guiding quality software design

Slide 44

Slide 44 text

TDD Disclaimer TDD influences design quality But it’s not a magic wand! anthonysciamanna.com @asciamanna

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Examples @asciamanna anthonysciamanna.com

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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) anthonysciamanna.com @asciamanna JIT (re)design design design design design

Slide 56

Slide 56 text

Developer Workflow Benefits Short, fast feedback cycles improve productivity

Slide 57

Slide 57 text

Nearly instantaneous feedback loops anthonysciamanna.com @asciamanna

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

anthonysciamanna.com @asciamanna

Slide 60

Slide 60 text

Resilient to Interruptions anthonysciamanna.com @asciamanna

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

anthonysciamanna.com @asciamanna

Slide 63

Slide 63 text

BIG PROBLEM @asciamanna anthonysciamanna.com

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

BIG PROBLEM @asciamanna anthonysciamanna.com 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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

Tiny Validations anthonysciamanna.com @asciamanna

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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. “ ” anthonysciamanna.com @asciamanna

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

Mental Benefits of TDD @asciamanna anthonysciamanna.com Achieving Profluence

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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 • Magician - Photo by Pierrick VAN-TROOST on Unsplash • 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/