pragmatic notdogmatictdd
Embed Size (px)
DESCRIPTION
Agile 2012TRANSCRIPT

Joseph W. Yoder
TDD & Testing Best Practices Page - 1
Pragmatic, Not Dogmatic TDD:
Rethinking How We Test
Joseph W. Yoder
The Refactory, Inc.
http://www.refactory.com
Slide - 2 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Bio Joseph Yoder (Founder and Architect, The Refactory;
Hillside Board President; Long Term ACM Member)
pattern enthusiast, author of Big Ball of Mud;
programs adaptive software,
runs a development company,
consults top companies on software
needs, agile enthusiast,
amateur photographer,
motorcycle enthusiast,
enjoys dancing samba!!!

Joseph W. Yoder
TDD & Testing Best Practices Page - 2
Slide - 3 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Slide - 4 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Test-Driven Development Cycle

Joseph W. Yoder
TDD & Testing Best Practices Page - 3
Slide - 5 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Add a Test design class interface +
define expected behavior
Make Test Pass create actual behavior +
most simple solution
Refactor clean implemented code
+ adjust class design
Slide - 6 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Classic Test-Driven Development
Re(Write) a test
Write production code
Clean up code (Refactor)
Check if test fails
Check all tests succeed
test fails
all tests succeed
test succeeds
1 o
r m
ore
te
sts
fail
Ready to Release?
Ship it!!!
Start
Short Sprints
First

Joseph W. Yoder
TDD & Testing Best Practices Page - 4
Slide - 7 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
TDD Promises
1 Tests help you build the right thing
2 Tests guide development
3 Tests keep you focused
4 Tests allow you to change code safely and quickly
5 Tests ensure what you build works
6 You will end up with quality code
7 You will end up with a good design
Slide - 8 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Question: Do tests help you build the right things?

Joseph W. Yoder
TDD & Testing Best Practices Page - 5
Slide - 9 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Common Misperceptions
Tests verify program correctness
3.1415926535…
Tests enable and encourage well-designed code
Slide - 10 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Understanding
Tests

Joseph W. Yoder
TDD & Testing Best Practices Page - 6
Slide - 11 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Test Target → the thing
we are trying to test. T
Action → changes environment or the Test Target.
Assertion → comparison of
expected vs observable outcome of the action on the Test Target.
Slide - 12 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
T T’
Test → a sequence
of at least one action and one assertion

Joseph W. Yoder
TDD & Testing Best Practices Page - 7
Slide - 13 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Good Test Outline
1. Set up
2. Declare the expected results
3. Exercise the test
4. Get the actual results
5. Assert that the actual results match the expected results
Slide - 14 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Testing Questions
What is a good size for a test?
a class?
a set of classes?
a set of interacting functions/methods?
What is important to test?

Joseph W. Yoder
TDD & Testing Best Practices Page - 8
Slide - 15 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
What to Test
Significant scenarios of use, not isolated methods.
The difficult parts: Complex interactions, intricate algorithms, tricky business logic
Required system qualities
Performance, scalability, throughput, security...
How services respond to normal and exceptional invocations.
Slide - 16 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
What Not to Test
Tests should add value, not just be an exercise.
Do not test:
setters and getters
(unless they have side effects or are very complex)
every boundary condition; only test those with significant business value
every exception; only those likely to occur or that will cause catastrophic problems.

Joseph W. Yoder
TDD & Testing Best Practices Page - 9
Slide - 17 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Question: Do tests help you build the right things? Answer: Yes. But only if you have the right tests, not superficial tests.
Slide - 18 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Ten Commandments of Testing
1. Test single complete scenarios
2. Do not create dependencies between tests
3. Only verify a single thing in each assertion
4. Respect class encapsulation
5. Test limit values and boundaries
6. Test expected exceptional scenarios
7. Test interactions with other objects
8. When you find a bug, write a test to show it
9. Do not duplicate application logic in tests
10. Keep your test code clean

Joseph W. Yoder
TDD & Testing Best Practices Page - 10
Slide - 19 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Question: Do tests guide development and keep you focused?
Slide - 20 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Two Faces of Testing
Defining tests helps limit scope and increases focus
Tests force you to implement functionality instead of jumping around and tweaking stuff
But you may be missing the bigger picture…
Might need to consider how current functionality affects the rest of the system

Joseph W. Yoder
TDD & Testing Best Practices Page - 11
Slide - 21 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Thinking Fast vs. Slow
Fast thinking: decisions based on intuition, biases, ingrained patterns, and emotions
Slow thinking: Reasoning, logical thinking
Slide - 22 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Take Time For Both
Slow thinking
Pairing and discussion options are why you want to implement something a certain way
Sketching, noodling, design spikes
Fast thinking
Fast turns of coding, testing and quick fixes… (Red/Green)

Joseph W. Yoder
TDD & Testing Best Practices Page - 12
Slide - 23 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Common Belief
You are not practicing TDD correctly unless you write tests first, before writing any code that is tested.
You are not doing TDD!
You must create your tests first!
Slide - 24 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Dogmatic
Synonyms: assertive, bullheaded, categorical, definite, despotic, determined, dictative, doctrinaire, domineering, downright, fanatical, intolerant, narrow-minded, one-sided, overbearing, peremptory, stubborn, tyrannical, unequivocal
Antonyms: amenable, flexible, manageable
Blindly following the TDD doctrine says you … “Must” write your test first!!!

Joseph W. Yoder
TDD & Testing Best Practices Page - 13
Slide - 25 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Is it OK to write tests after you write production code?
Is this cheating?
Does this result in bad code?
Does it result in a bad design?
Does it reinforce “slacker” tendencies to not write tests?
Slide - 26 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Pragmatic
Synonyms: businesslike, common, commonsense, down-to-earth, easy, efficient, hard, hardboiled, hardheaded, logical, matter-of-fact, mundane, no-nonsense, practical , rational, realistic, sane, sensible, sober, unfantastic, unidealistic, unsentimental
Antonyms: idealistic, unrealistic
Sometimes it is more practical and logical to write your tests after you write the code!!!

Joseph W. Yoder
TDD & Testing Best Practices Page - 14
Slide - 27 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Start
Re(Write) a test
Write production code
Clean up code (Refactor)
Check if test fails
Check all tests succeed
test fails
all tests succeed
test succeeds
1 o
r m
ore
te
sts
fail
Ready to Release?
Ship it!!!
Common Practice Tests don’t always get written first.
Slide - 28 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
A Pragmatic Testing Cycle
Write some production code
Check if test fails
Check all tests succeed
all tests succeed
1 or more tests fail
Tests verify that code works as expected. Tests don’t always guide what code you write.
Re(write) a test
Clean up code (Refactor)
Ready to Release?
Ship it!!!

Joseph W. Yoder
TDD & Testing Best Practices Page - 15
Slide - 29 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Pragmatic Testing Cycle
Alternate between writing test code and production in small steps.
Use feedback from tests to verify code (integrate often).
Don’t worry whether the chicken or the egg comes first. Sometimes development guides testing.
Do what yields the most value!
Slide - 30 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Question: Do tests guide development and keep you focused? Answer: Yes if you have the right tests. But tests do not tell you if you built it right. Sometimes you need to take a broader look.

Joseph W. Yoder
TDD & Testing Best Practices Page - 16
Slide - 31 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Slide - 32 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Question: Do tests allow you to change code safely and quickly?

Joseph W. Yoder
TDD & Testing Best Practices Page - 17
Slide - 33 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Test-Driven
Development Refactoring
Slide - 34 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Common Wisdom
Work refactoring into your daily routine.
“In almost all cases, I’m opposed to setting aside time for refactoring. In my view refactoring is not an activity you set aside time to do. Refactoring is something you do all the time in little bursts.”—Martin Fowler

Joseph W. Yoder
TDD & Testing Best Practices Page - 18
Slide - 35 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Testing is Key!!!
Slide - 36 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Two Refactoring Types*
Floss Refactorings—frequent, small changes, intermingled with other programming (daily health)
Root canal refactorings—infrequent, protracted refactoring, during which programmers do nothing else (major repair)
* Emerson Murphy-Hill and Andrew Black in “Refactoring Tools: Fitness for Purpose” http://web.cecs.pdx.edu/~black/publications/IEEESoftwareRefact.pdf

Joseph W. Yoder
TDD & Testing Best Practices Page - 19
Slide - 37 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Sometimes it is easier to throw away tests, change the design of your production code, and then write new tests.
Slide - 38 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
The design of test code
needs to evolve, just like
production code!

Joseph W. Yoder
TDD & Testing Best Practices Page - 20
Slide - 39 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
….if your tests slow down your ability to make the changes you want to make in production code, rethink whether you want to keep all your tests.
Slide - 40 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Refactoring
Production Code
Test bar should
continue green
But tests can also have smells!

Joseph W. Yoder
TDD & Testing Best Practices Page - 21
Slide - 41 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Possible Duplicate Code
Slide - 42 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Create Test Hierarchy
So, Refactor the Tests!
Pull Up Common
Parts

Joseph W. Yoder
TDD & Testing Best Practices Page - 22
Slide - 43 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Additional Test
Refactorings
Tests have Refactorings
that are specific to test
code …
Slide - 44 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Replace actions & assertions
with simpler equivalent ones: Add Assertion Explanation
Introduce Assertion Method
Simplify Test Scenario
Separate Action from Assertion
Decompose Assertion
Test Method
Refactorings

Joseph W. Yoder
TDD & Testing Best Practices Page - 23
Slide - 45 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Reorganize actions and assertions
to reduce duplication: Add Fixture
Introduce Initialization Method
Inline Initialization Method
Join Incremental Tests
Split Test
Join Similar Tests with Distinct Data
Test Class
Refactorings
Slide - 46 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Question: Do tests allow you to change code safely and quickly? Answer: Yes and no. It takes courage to throw away and rewrite your tests.

Joseph W. Yoder
TDD & Testing Best Practices Page - 24
Slide - 47 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Classic Test-Driven Development Rhythm
User story-by-story: Write the simplest test
Run the test and fail
Write the simplest code that will pass the test
Run the test and pass
Repeat until a “story” is tested and implemented
“Design happens between the keystrokes”
Slide - 48 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Question: Do tests help with quality code and good design?

Joseph W. Yoder
TDD & Testing Best Practices Page - 25
Slide - 49 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Requirements Envisioning
(days/weeks/...)
Architecture Envisioning
(days/weeks/…)
Iteration 0: Envisioning
Iteration Modeling (hours)
Model Storming (minutes)
Fast TDD(hours)
Iteration n: Development
a little bit of modeling then a lot of coding
Conceptual Modeling
Another View of Test-Driven Development
Slide - 50 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Spike Solutions
If some technical difficulty threatens to hold up the system's development,
Or you are not sure how to solve a particular problem…
“Put a pair of developers on the problem for a week or two to reduce the potential risk”

Joseph W. Yoder
TDD & Testing Best Practices Page - 26
Slide - 51 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Pragmatic Testing Questions
What are the most important kinds of testing you should focus on?
Who should write them?
Who run them?
When are they run?
How are they run?
Slide - 52 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Different Kinds of Tests Unit Tests – Tests classes and components
Integration Tests – Tests code integration
Smoke Tests – Quick tests of core functionality
Performance Tests – Test system under load
Regression Tests – Tests it is still working
Acceptance Tests – Requirements testing
System Tests – Test all parts together
Common to only focus on Unit Tests in TDD!!!

Joseph W. Yoder
TDD & Testing Best Practices Page - 27
Slide - 53 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Tests Can Overlap…
Integration Tests Acceptance
Tests (Functional and qualities)
Quality Scenario
Unit Tests
Smoke Tests
Slide - 54 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
TDD should be more than…
Developing unit tests along with code
Only checking in tested code with unit tests + defining some customer acceptance

Joseph W. Yoder
TDD & Testing Best Practices Page - 28
Slide - 55 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Other Techniques for Improving Quality
Steve McConnell
http://kev.inburke.com/kevin/the-best-ways-to-find-bugs-in-your-code/
Slide - 56 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Combine and Conquer
The average is 40% for any one technique…
No one approach is adequate
Combining techniques gives you much higher quality

Joseph W. Yoder
TDD & Testing Best Practices Page - 29
Slide - 57 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Question: Do tests help with quality code and good design? Answer: They can but … you need more than tests.
Slide - 58 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Identify some requirements
Create some acceptance criteria
Construct based on the tests
Test to verify the requirements
Deliver some working software
The Agile Dream …

Joseph W. Yoder
TDD & Testing Best Practices Page - 30
Slide - 59 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Agile Design Values
Core values:
Design Simplicity
Communication
Teamwork
Trust
Satisfying stakeholder needs
Keep learning
Lots of Testing!!!
Slide - 60 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Tests written & must pass before checking in production code
Pragmatic Test-Driven Development (core process)
Write some production code
Check if test fails
Check all tests succeed
all tests succeed
1 or more tests fail
Re(write) a test
Clean up code (Refactor)
Ready to Release?
Ship it!!! Short Sprints

Joseph W. Yoder
TDD & Testing Best Practices Page - 31
Slide - 61 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
TDD Challenges 1. Lot’s of Unit Tests with classic TDD is
not enough to guarantee good design… Dogmatic TDD can focus on functionality and features rather than design
2. Deception that incremental test mode will give you an inductive way to prove it works…probably more deductive and example driven…
All squirrels I see in Illinois are brown, therefore all squirrels are brown….
3. TDD or unit tests can hurt the evolution/refactoring phase….“Oh no! Changes might break the tests”
4. Not enough Quality Tests are written. Focus often is on function and missing quality.
5. Testing Patterns of Collaboration are important, Unit tests on class might not be good enough. Setting up Mocks doesn’t really test collaborations.
Slide - 62 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Pragmatic Test Driven Development Is…
Practical…
Focus on how testing best fits into and enhances your current software development practices.
Thinking carefully about how best to validate that your software meets its requirements.
Developing the optimum set of tests to feel confident your system keeps working, not just Unit Testing!
Including both slow and fast thinking with your tests.

Joseph W. Yoder
TDD & Testing Best Practices Page - 32
Slide - 63 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Keep It Focused
Keep It Fresh
Expand your Horizons
…Be Practical
Slide - 64 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Where Did All The Time Go?
Muito Obrigado!!!
Twitter: @metayoda
Twitter: @rebeccawb
http://adaptiveobjectmodel.com/2012/01/what-is-pragmatic-tdd/
http://refactory.com/training/test-driven-development

Joseph W. Yoder
TDD & Testing Best Practices Page - 33
Slide - 65 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Minimum Bar for Pragmatic TDD
All production code must have tests validating it.
Think about tests early in each iteration and how to validate the next release.
Core tests include important aspect of your software, what you really care about.
Include both slow and fast thinking with your tests.
Slide - 66 Pragmatic Test Driven Development – Copyright © 2012 Joseph W. Yoder The Refactory, Inc.
Ten Tenets of Testing
1. Test single complete scenarios
2. Do not create dependencies between tests
3. Only verify a single thing in each assertion
4. Respect class encapsulation
5. Test limit values and boundaries
6. Test expected exceptional scenarios
7. Test interactions with other objects
8. When you find a bug, write a test to show it
9. Do not duplicate application logic in tests
10. Keep your test code clean