Upload
railsconf
View
2.382
Download
1
Tags:
Embed Size (px)
Citation preview
Below And Beneath TDD
Test-Last Development and other real world test patterns
Noel Rappin@noelrap
Pathfinder Developmentpathf.com
railsprescriptions.com
http://www.railsrx.com
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Why does testing work?
Why does testing fail?
Rx
TEST
FAIL
CODE
REFACTOR
PASSWhat does it mean for
testing to “work”?
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Improve effectiveness of team and
quality of code
NOT
A perfect guarantee ofcode correctness
Rx
TEST
FAIL
CODE
REFACTOR
PASS
for me, tdd is about confidence &
accountability & responsibility & design &
reusability & portability & flexibility & testing & trustworthiness &
teamwork & initiative & flow & that's all i can
think of at the moment.
-- Kent Beck
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Accurate view of code
Small, incremental steps
Scaffold for change
Focus for development
Rx
TEST
FAIL
CODE
REFACTOR
PASSWhat does it mean for
testing to “fail”?
Rx
TEST
FAIL
CODE
REFACTOR
PASSDecrease efficiency and
code quality
Rx
TEST
FAIL
CODE
REFACTOR
PASS
“It feels like there's a hidden door that everyone who tests has found and
walked through... There are a bunch of us...that would love to join
the party, but don't know where to find
the door.”
Rx
TEST
FAIL
CODE
REFACTOR
PASS
If you are stuck in a window or banging your head against the wall,
this will helpyou get to the door
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Rx
TEST
FAIL
CODE
REFACTOR
PASSTest-Driven
Development
Rx
TEST
FAIL
CODE
REFACTOR
PASS
AKA: Behavior-DrivenDevelopment
or
Test-FirstProgramming
Rx
TEST
FAIL
CODE
REFACTOR
PASS
TDD Code has:
small pieceshigh cohesionloose coupling
minimal side-effects
Rx
TEST
FAIL
CODE
REFACTOR
PASS
☺Accurate view of code
☺Small, incremental steps
☺Scaffold for change
☺Focus for development
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Rx
TEST
FAIL
CODE
PASSLose the refactoring
Rx
TEST
FAIL
CODE
PASSDevelopment, that
happensto have tests
Rx
TEST
FAIL
CODE
PASS
Symptoms:
Increased duplicaton in code.
Long methods
Rx
TEST
FAIL
CODE
PASS
Benefits:
May seem faster at first
Rx
TEST
FAIL
CODE
PASSOver time...
Rx
TEST
FAIL
CODE
PASS
☺Accurate view of code
Small, incremental steps
Scaffold for change
☺Focus for development
Rx
TEST
FAIL
CODE
PASSFix by:
Cleaning up your mess
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Rx
TEST
FAIL
CODE
REFACTOR
PASS
TEST
FAIL
Rx
TEST
FAIL
CODE
REFACTOR
PASS
TEST
FAIL
assert_equal(nil, actual)
actual = 4.314
assert_equal(4.314, actual)
Rx
TEST
FAIL
CODE
REFACTOR
PASS
TEST
FAIL
Data-Driven Testing
or
Using your programas a calculator
Rx
TEST
FAIL
CODE
REFACTOR
PASS
TEST
FAIL
Benefits:
Failed Test Guaranteed
Value may be hard to calculate
Rx
TEST
FAIL
CODE
REFACTOR
PASS
TEST
FAIL
Pitfalls:
Can mask ignoranceof domain
Can lead to worseproblems
Rx
TEST
FAIL
CODE
REFACTOR
PASS
TEST
FAIL
Accurate view of code
☺Small, incremental steps
☺Scaffold for change
☺Focus for development
Rx
TEST
FAIL
CODE
REFACTOR
PASS
TEST
FAIL
assert_equal(nil, actual)
actual = 4.314
assert_equal(4.314, actual)
Rx
CODE REFACTOR
PASS
TEST
actual = 4.314
assert_equal(4.314, actual)
Rx
TEST
CODE REFACTOR
PASS
Test-Next Development
aka
Code-Driven Development
Rx
TEST
CODE REFACTOR
PASS
Symptoms:
No more failing tests
Tweaking tests to match code
Coverage drop
Rx
TEST
CODE REFACTOR
PASS
Benefits:
Useful when exploring
Useful when testingviews
Rx
TEST
CODE REFACTOR
PASS
Pitfalls:
Easily slides into worsepatterns
Hard to avoidcoverage gaps
Rx
TEST
CODE REFACTOR
PASS
Accurate view of code
☺Small, incremental steps
Scaffold for change
Focus for development
Rx
TEST
CODE REFACTOR
PASSTautology Based
Development
Rx
TEST
CODE REFACTOR
PASS
Definition:
Tests that can’tfail
Rx
TEST
CODE REFACTOR
PASS
Symptoms:
Bugs that should have been covered
Mock objectmisuse
Rx
TEST
CODE REFACTOR
PASS
☹Accurate view of code
☺Small, incremental steps
Scaffold for change
☹Focus for development
Rx
TEST
CODE REFACTOR
PASS
Mitigation:
Try to separatecode assertions from application
data
Rx
TEST
CODE REFACTOR
PASS
Rx
TEST
CODE REFACTOR
PASS
CODE
CODE
CODE
CODE
CODE
CODE
Rx
TEST
CODE REFACTOR
PASS
CODE
CODE
CODE
CODE
CODE
CODE
Test-Last Development
Rx
TEST
CODE REFACTOR
PASS
CODE
CODE
CODE
CODE
CODE
CODE
Symptoms:
Significant coverage drop
Uncovered bugs
Harder to write tests
Rx
TEST
CODE REFACTOR
PASS
CODE
CODE
CODE
CODE
CODE
CODE
This is one way in which we talk past each other
Unit tests ≠ TDD
Rx
TEST
CODE REFACTOR
PASS
CODE
CODE
CODE
CODE
CODE
CODE
Pitfall:
Tests get moreexpensive
Lower quality tests
Programmer boredom
Rx
TEST
CODE REFACTOR
PASS
CODE
CODE
CODE
CODE
CODE
CODE
Needed when:
Hitting corner case
Dealing with legacy
Rx
TEST
CODE REFACTOR
PASS
CODE
CODE
CODE
CODE
CODE
CODE
☹Accurate view of code
☹Small, incremental steps
Scaffold for change
☹Focus for development
Rx
TEST
CODE REFACTOR
PASS
CODE
CODE
CODE
CODE
CODE
CODE
Escape by:
Start the TDD process
Don’t look back
Catch up to legacyparts as you touch
them
Rx
TEST
CODE REFACTOR
PASS
CODE
CODE
CODE
CODE
CODE
CODE
Rx
TEST
CODE REFACTOR
Run
CODE
CODE
CODE
CODE
CODE
CODE
Rx
TEST
CODE REFACTOR
Run
CODE
CODE
CODE
CODE
CODE
CODE
Coverage Driven Development
Rx
TEST
CODE REFACTOR
Run
CODE
CODE
CODE
CODE
CODE
CODE
Symptom:
Tests without assertions
Rx
TEST
CODE REFACTOR
Run
CODE
CODE
CODE
CODE
CODE
CODE
Why?
Minimal approach to legacy
Hard to reach code
Rx
TEST
CODE REFACTOR
Run
CODE
CODE
CODE
CODE
CODE
CODE
☹Accurate view of code
☹Small, incremental steps
☹Scaffold for change
☹Focus for development
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Rx
TEST
FAIL
CODE
REFACTOR
PASS
TESTTEST
TESTTEST
Rx
TEST
FAIL
CODE
REFACTOR
PASS
TESTTEST
TESTTEST
Lots of Test First Development
Rx
TEST
FAIL
CODE
REFACTOR
PASS
TESTTEST
TESTTESTSymptom:
Lots of test failures atonce during initial developmment
Rx
TEST
FAIL
CODE
REFACTOR
PASS
TESTTEST
TESTTESTBenefit:
Allows planning for future code
Rx
TEST
FAIL
CODE
REFACTOR
PASS
TESTTEST
TESTTEST Cost:
Loss of focus
Somewhat harder towrite code against
Rx
TEST
FAIL
CODE
REFACTOR
PASS
TESTTEST
TESTTEST
Middle Ground:
RSpec pending
Rx
TEST
FAIL
CODE
REFACTOR
PASS
TESTTEST
TESTTEST☺Accurate view of code
Small, incremental steps
☺Scaffold for change
Focus for development
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Rx
TEST
FAIL
CODE
REFACTOR
PASS
SETUP
Rx
TEST
FAIL
CODE
REFACTOR
PASS
SETUP
Setup Driven Development
Rx
TEST
FAIL
CODE
REFACTOR
PASS
SETUPSymptom:
Large, brittle setup methods
Slow tests
High-level tests
Rx
TEST
FAIL
CODE
REFACTOR
PASS
SETUPCosts:
This is why people hate testing
Rx
TEST
FAIL
CODE
REFACTOR
PASS
SETUPAccurate view of code
☹Small, incremental steps
☺Scaffold for change
Focus for development
Rx
TEST
FAIL
CODE
REFACTOR
PASS
SETUPEscape by:
Mock Objects
Real unit tests
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Another problem:
End to end failures
Lack of focus
Expectation failures
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Cucumber
A potential solution
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Cucumber
Acceptance Test-DrivenDevelopment
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Cucumber
Symptom:
You are writing acceptance tests
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Cucumber
Costs:
Additional layer of test code, indirection
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Cucumber
Benefits:
Increased focus, improved feedback
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Cucumber
☺Accurate view of code
☺Small, incremental steps
☺Scaffold for change
☺Focus for development
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Rx
TEST
FAIL
CODE
REFACTOR
PASS
And So:
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Constant feedbackand
baby steps
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Focus on what comes next
Failure Driven Development
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Good code has
small pieceshigh cohesionloose coupling
minimal side-effects
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Invisible cost of bugs
or
Done vs. “Done Done”
Rx
TEST
FAIL
CODE
REFACTOR
PASSGo write
some tests!
Rx
TEST
FAIL
CODE
REFACTOR
PASS
Rails Test Prescriptions
http://www.railsrx.com
Pathfinder Development
http://www.pathf.comhttp://www.pathf.com/blogs
@noelrap @railsrx
BoF: 9PM Wed Pavillion 9-10