TDD,BDD and Unit Testing in Ruby @[email protected].
-
Upload
blake-flowers -
Category
Documents
-
view
243 -
download
0
Transcript of TDD,BDD and Unit Testing in Ruby @[email protected].
![Page 2: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/2.jpg)
A T
est
Dri
ven L
ife
Appro
ach
Expect Something big Expect many smaller
things as part of it
![Page 3: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/3.jpg)
A T
est
Dri
ven L
ife
Appro
ach Acceptance Test
Fill in the gaps with Specs, Unit and Integration Testing
![Page 4: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/4.jpg)
Engineering methodologies (waterfall for example..)
Agile (flexible & lightwheight) Methodologies eXtreme Programming
Test Driven Development
Team Agile - All rights
reserved
Putt
ing T
DD
in
Conte
xt
![Page 5: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/5.jpg)
What is a
‘Unit’?
![Page 6: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/6.jpg)
Unit
Of
Work
Production Code
Unit Of Work
Return Value/Exceptio
n
Noticeable State Change
3rd Party call
Public API
Test
![Page 7: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/7.jpg)
Production Code
Unit Of Work
Return Value/Exceptio
n
Public API
Assert Test
![Page 8: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/8.jpg)
![Page 9: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/9.jpg)
Production Code
Unit Of Work
Noticeable State Change
Test
Public API
Ass
ert
![Page 10: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/10.jpg)
![Page 11: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/11.jpg)
Noti
ceable
Sys
tem
Behavi
or
Change
Noticeable by a user
of the public API, at the same level of
the entry point that made the change.
ACT and ASSERT on the same level of APIs.
![Page 12: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/12.jpg)
Production Code
Unit Of Work
3rd Party call
Test
Public API
Asse
rt
3rd Party
Unit Of Work
![Page 13: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/13.jpg)
Production Code
Unit Of Work
3rd Party call
Test
Public API
Asse
rt
3rd Party
Unit Of Work
Mock
![Page 14: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/14.jpg)
![Page 15: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/15.jpg)
Easier to find bugs
Easier to maintain
Easier to understand
Easier to Develop
Team Agile - All rights
reserved
Unit Testing makes your developer lives easier
![Page 16: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/16.jpg)
Not structured
Not Repeatable
Not on all your code
Not easy to do as it should be
A framework is missing
Team Agile - All rights
reserved
All devs have already done Unit testing
![Page 17: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/17.jpg)
Original was for SmallTalk Kent Beck and Erich Gamma
Ported to Various languages and platforms JUnit, CppUnit, DUnit, VBUnit, RUnit, PyUnit, Sunit,
HtmlUnit, …
Good list at www.xprogramming.com
Standard test architecture
Introducing RSpecTeam Agile - All rights
reserved
The xUnit Frameworks
![Page 18: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/18.jpg)
Fram
ew
ork
s
Test-Unit Minitest RSpec
![Page 19: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/19.jpg)
Acc
epta
nce
Fr
am
ew
ork
s
Cucumber Steak Turnip
![Page 20: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/20.jpg)
![Page 21: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/21.jpg)
![Page 22: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/22.jpg)
![Page 23: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/23.jpg)
![Page 24: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/24.jpg)
Demo
![Page 25: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/25.jpg)
Make it FailNo code without a failing test
Make it WorkAs simply as possible
Make it BetterRefactor
Team Agile - All rights
reserved
Test-Driven Development
![Page 26: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/26.jpg)
Dem
o
![Page 27: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/27.jpg)
String
Calculator
Osherove.com/kata
![Page 28: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/28.jpg)
Demo String Calc
![Page 29: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/29.jpg)
Simplicity
Team Agile - All rights
reserved
![Page 30: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/30.jpg)
![Page 31: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/31.jpg)
Sim
plic
ity
take
2 As many assumptions
as you can Then debunk them one by one
![Page 32: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/32.jpg)
Exa
mple
The input will always
be empty - wrong, if I send in a
… The input will always
be one number if it is
not empty - wrong, if I send in …
![Page 33: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/33.jpg)
Common
Questions
![Page 34: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/34.jpg)
Think through the requirement Think about the design
and usability of the API
There’s never time to
write it afterwards You write much less tests (if at all) otherwise
Team Agile - All rights
reserved
Why
wri
te t
he t
est
befo
re t
he c
ode?
![Page 35: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/35.jpg)
Make sure the test does not
have a bug Make sure you’re testing the
right thing Define a small problem to solve
When the test passes, you are
done
If you can’t write that test, you
may Not understand the requirement
Have over designed things (not
testable) Not have a small enough problem
to solve
Team Agile - All rights
reserved
Why
make
it f
ail
firs
t?
![Page 36: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/36.jpg)
Force you to write more tests you hadn’t
considered Simpler solutions are
easier to maintain Better code coverage
of tested code
Team Agile - All rights
reserved
Why
solv
e it
as
sim
ply
as
poss
ible
?
![Page 37: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/37.jpg)
Constantly improve the
design of the code Unit tests act as safety net
Remove duplication,
improve readability and
maintainability You’ll need to when things
change (requirements,
your understanding of
them, other factors..)Team Agile - All rights
reserved
Why
Refa
ctor?
![Page 38: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/38.jpg)
60% of the features you
build won’t be used Hopping between tasks can
leave stuff undone Something working is better
than nothing Meanwhile requirements may
change, you lose less work
(simpler solution, less time).
Small units of work are easier
for people – better life!
Feeling of accomplishment
that stuff is doneTeam Agile - All rights
reserved
Why
work
in
crem
enta
lly?
![Page 39: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/39.jpg)
It’s a much bigger problem
to solve Longer time to “green bar”
feeling You won’t know you have
good code coverage Solution probably more
complex than it could have
been To support all tests at once
Also takes longer to create,
more possibility of undetected bugsTeam Agile - All rights
reserved
Why
not
wri
te m
any
test
s at
once
?
![Page 40: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/40.jpg)
Implement –
7d
Testing – 3d
Fix Bugs – 3d
Testing – 3d
Fix Bugs – 2d
Testing – 1d
Release(19d)
Implement –
14d
Testing – 3d
Fix Bugs – 2d
Testing – 1d
Fix Bugs – 1d
Testing – 1d
Release(22d)
Team Agile - All rights reserved
Sce
nario
![Page 41: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/41.jpg)
Potential “Production
Bugs” reduced by up to
90% - > more stable
product Better designed interfaces API Documentation
Safety net for past and
future Happier customer Happier codersTeam Agile - All rights
reserved
RO
I
![Page 42: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/42.jpg)
Implementat
– 7d
Integration –
7d
Testing – 3d
Fix Bugs – 3d
Testing – 3d
Fix Bugs – 2d
Testing – 1d
Release(26d)
Implement –
14d
Integration –
2d
Testing – 3d
Fix Bugs – 2d
Testing – 1d
Fix Bugs – 1d
Testing – 1d
Release(24d)
Team Agile - All rights reserved
Sce
nario
![Page 43: TDD,BDD and Unit Testing in Ruby @RoyOsheroveroy@osherove.com.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dd95503460f94ace7eb/html5/thumbnails/43.jpg)
Questions?