Top Banner
TESTING AND TDD John McCaffrey @ J_McCaffrey Wednesday, October 29, 14
60

A Taste of TDD: The basics of TDD, why it is hard and how to do it better

Jun 20, 2015

Download

Software

John McCaffrey

A Taste of TDD: Basic overview of Agile Testing, TDD in practice, Pros/Cons of Test Driven Development, Recent TDD Controversey (DHH, Kent Beck, Martin Fowler), and some strategies for doing it well.

This is a lead in to a full Agile Workshop on Scrum/XP, TDD, and Pairing
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TESTING AND TDD

John McCaffrey@ J_McCaffrey

Wednesday, October 29, 14

Page 2: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

AGENDA• Poll: What are your current testing practices?

•What is TDD?

•Where does it fit in the Agile landscape

• TDD basics

• Pros/Cons/Controversy

•Demo/Q&A

Wednesday, October 29, 14

Page 3: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

ME

• Started Unit testing in Java

• TDD Javascript & Ruby

• Love Testing and TDD

Wednesday, October 29, 14

Page 4: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

YOU

•What are you doing already?

•What are you hoping to learn?

Wednesday, October 29, 14

Page 5: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TDD DEFINED

Wednesday, October 29, 14

Page 6: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TDD DEFINED

• Software Development practice

• Focus on explicit expectations from tests

• Characterized by red/green/refactor

Wednesday, October 29, 14

Page 7: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

RED/GREEN/REFACTOR

Wednesday, October 29, 14

Page 8: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TDD BENEFITS• Correct behavior

• Rapid feedback

• Simpler, easier to understand

• Fully covered by tests

• Living documentation

• Confidence

Wednesday, October 29, 14

Page 9: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TDD == GTD?

• Collect

• Process

• Organize

• Review

• Do it

Wednesday, October 29, 14

Page 10: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

• not just testing in general

•more of a design practice with test benefits

• TDD is not a silver-bullet!

Wednesday, October 29, 14

Page 11: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

WHAT WE WILL COVER• Where Testing and TDD fit in Agile

• General Agile Testing practices

• TDD Practice

• Pros/Cons/Controversy

• Demo/Q&A

Wednesday, October 29, 14

Page 12: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TDD RESOURCES

• Blogs, Books, Workshops

• Confreaks.com

• pluralsight.com

Wednesday, October 29, 14

Page 13: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

WHY IS TESTING SO CRITICAL?

Wednesday, October 29, 14

Page 14: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

DOES OUR CODE...

• function correctly?

• handle expected edge cases and exceptions?

• allow for new features and change?

•make sense to new team members?

Wednesday, October 29, 14

Page 15: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

MO’ PROBLEMS• Code changes create new bugs

• Fear of changing anything leads to worse code

• Estimates are higher, changes take longer

• Bugs, Bugs, Bugs, regression, Bugs, Bugs

• Time between build to feedback is too long

Wednesday, October 29, 14

Page 16: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

WITH TESTS

• Code change is safer

• Component design is clearer

• Feedback is quicker

• Safety and freedom to make the code better

Wednesday, October 29, 14

Page 17: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TESTS ARE FOR EVERYONE

•Managers and Business team

•QA Team

• Support

• dev ops

• Customers

Wednesday, October 29, 14

Page 18: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TESTING TYPES

• Unit

• Functional

• Integration/system testing

• User acceptance

•Manual testing

Wednesday, October 29, 14

Page 19: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

DIFFERENT TYPES OF TESTS

Wednesday, October 29, 14

Page 20: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TDD APPLIED

• You can use TDD and Test-First with all types of testing

• TDD helps you focus on your specific task

• TDD may be challenging for some types of testing

Wednesday, October 29, 14

Page 21: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TESTING & TDD IN THE

AGILE LANDSCAPE

Wednesday, October 29, 14

Page 22: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

FEEDBACK IS KEY

Wednesday, October 29, 14

Page 23: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

AGILE:FEEDBACK AT EVERY LEVEL

Wednesday, October 29, 14

Page 24: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

SHORTEST CYCLE

Wednesday, October 29, 14

Page 25: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

XP PRACTICES

• Planning Game

• On-site Customer

• Small Releases

• Simple design

• Customer Testing

• Metaphor

• Sustainable pace (40hr)

• Common Standards

• Collective Ownership

• Unit Testing

• Refactoring

• Continuous Integration

• Pair Programming

Wednesday, October 29, 14

Page 26: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

XP PRACTICES

• Planning Game

• On-site Customer

• Small Releases

• Simple design

• Customer Testing

• Metaphor

• Sustainable pace (40hr)

• Common Standards

• Collective Ownership

• Unit Testing

• Refactoring

• Continuous Integration

• Pair Programming

Wednesday, October 29, 14

Page 27: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TDD FEEDBACK CYCLE• Code is correct

•Design is workable

• Intention is clear

•Dependencies and collaborators are minimal

• It does what the Developer intended....

Wednesday, October 29, 14

Page 28: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

IF YOU ONLY GET ONE THING OUT OF THIS...

Wednesday, October 29, 14

Page 29: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

FEEDBACK IS KEY

Wednesday, October 29, 14

Page 30: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

•Define acceptance test criteria as team, as part of the story definition

• write out the test steps

• plan to automate them

• update with any test cases that are discovered

IF YOU ONLY GET ONE THING OUT OF THIS...

Wednesday, October 29, 14

Page 31: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TDD WON’T FIX BAD REQUIREMENTS!!

Wednesday, October 29, 14

Page 32: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

THE PAYOFF

Wednesday, October 29, 14

Page 33: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

THE AWESOME

• high coverage

• fast

• informative

• not brittle

• visible to the team

Wednesday, October 29, 14

Page 34: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TDD IN PRACTICE

Wednesday, October 29, 14

Page 35: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TERMINOLOGY• Unit tests

• Setup/Fixture

• Assertion

• Coverage

•Design

• Interface/contract

•Mock/Stub

•DRY

• YAGNI

• Automated tests

• Continuous integration

• Continuous deployment

Wednesday, October 29, 14

Page 36: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TDD WORKFLOW

• write the test, watch it fail

• write “just enough” code to make it pass

• rinse/repeat, improving the tests and code as you go

Wednesday, October 29, 14

Page 37: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

SIMPLE DESIGN

• Passes all the tests

•Minimal Duplication

•Maximizes clarity

• Fewest number of classes or methods

Wednesday, October 29, 14

Page 38: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

SIMPLE DESIGN

• Low Coupling

• High Cohesion

• High Encapsulation

• SOLID

Wednesday, October 29, 14

Page 39: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TDD WORKFLOW

•New code

• Legacy

• Bugs

Wednesday, October 29, 14

Page 40: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

NEW CODE

Wednesday, October 29, 14

Page 41: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

STORY SAMPLE

• As a Standard User I need to see my list of outstanding orders, so I can review and complete them in priority order

Wednesday, October 29, 14

Page 42: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

STORY SAMPLE

• As a Standard User I need to see my list of outstanding orders, so I can review and complete them in priority order

Wednesday, October 29, 14

Page 43: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

STORY SAMPLE

• As a Customer I need to calculate the tip for my bill, so I can save time and include a reasonable amount

Wednesday, October 29, 14

Page 44: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

CODETEST

Wednesday, October 29, 14

Page 45: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TEST

Wednesday, October 29, 14

Page 46: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

CODE

Wednesday, October 29, 14

Page 47: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

LEGACY

• first, do no harm

• get sufficient coverage in place

• confirm existing functionality is correct

• review tests cases with team

Wednesday, October 29, 14

Page 48: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

TEST FIRST,REFACTOR LATER

I’M SERIOUS!!

Wednesday, October 29, 14

Page 49: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

CODE SAMPLE

Wednesday, October 29, 14

Page 50: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

BUGS

• write a test to confirm the bug

• change the code to fix the bug

• the test should confirm the fix

Wednesday, October 29, 14

Page 51: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

PROS/CONS

Wednesday, October 29, 14

Page 52: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

PROS• Focus

• Confidence

• Rapid Feedback

• Clear intention

• Executable documentation

Wednesday, October 29, 14

Page 53: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

CONS• TDD is HARD!!

• Refactoring is an uncertain art

• Some parts are hard to test first

• False sense of security

• Focus more on the tests than the system!

Wednesday, October 29, 14

Page 54: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

ANTI-PATT3RNS• huge setup, reused unnecessarily

• too much going on in one test

• only testing happy-path, not exceptions

• too much boiler-plate, basic tests

• over-mocking: “tests pass, but production is broken”

• not doing the ‘refactor’ part of Red/Green/Refactor

Wednesday, October 29, 14

Page 56: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

Wednesday, October 29, 14

Page 57: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

• Used to love and promote TDD

•Dogmatic TDD is not helpful

• Focusing exclusively on speed and low-coupling

• Shaming others for not following “TDD or Die!”

CONTROVERSY

Wednesday, October 29, 14

Page 58: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

SUMMARY

• Tests are worth the investment

• Everyone on the team should care about them

• TDD is a great way to improve your code and tests

• Find an approach that works best for your team

Wednesday, October 29, 14

Page 60: A Taste of TDD: The basics of TDD, why it is hard and how to do it better

LET’S HAVE SOMEQUESTIONS!

John McCaffrey@ J_McCaffrey

Wednesday, October 29, 14