What is Unit
Testing?
Breaking an application up into chunks (units)
and testing each unit individually.
Friday, November 11, 11
Slide 2
Slide 2 text
What's a “unit”?
Typically a single class, because classes are the
smallest independent pieces of your application.
Friday, November 11, 11
Slide 3
Slide 3 text
How do I test it?
With a “test harness.”
(code that calls the unit being tested and
compares the results to a predicted value)
Friday, November 11, 11
Slide 4
Slide 4 text
• Quality
• No amount of testing has ever put quality into a
product
• Explore unknown code
• "What happens if I poke it here?"
• Reveal misunderstandings
• Confidence
Why Should I Unit Test?
Friday, November 11, 11
Slide 5
Slide 5 text
• Before writing new code
• To illustrate a bug in existing code
• Before trying to refactor untested code
When Should I Write
Unit Tests?
Friday, November 11, 11
Slide 6
Slide 6 text
• PHP version of xUnit testing system
• Standard testing harness for Joomla
• Code: http://www.phpunit.de/
• Manual: http://www.phpunit.de/manual/
current/en/index.html
PHPUnit
Friday, November 11, 11
Slide 7
Slide 7 text
• Test suite files: "...Test.php"
• Testing methods: "test...()"
PHPunit Tests
Friday, November 11, 11
Slide 8
Slide 8 text
• Instantiate class
• Execute method
• Assert results
PHPUnit Testing Procedure
Friday, November 11, 11
Slide 9
Slide 9 text
Testing Assertions
• Locate Failures Easily
• Separate tests
• Detailed message
Friday, November 11, 11
Slide 10
Slide 10 text
• Code that has no unit tests
• Cannot be trusted
Legacy code
Friday, November 11, 11
Slide 11
Slide 11 text
• Discover actual functionality
• Discover new bugs
• Prepare for any code changes
Exploring Legacy code
Friday, November 11, 11
Slide 12
Slide 12 text
JObservable/JObserver
Friday, November 11, 11
Slide 13
Slide 13 text
Write Tests First!!
Developing new
code
Friday, November 11, 11
Slide 14
Slide 14 text
• Small steps toward a solution
• Focus your mind
• Encourage simpler code
• No more code than necessary
• Actually works
Before I write code?? Why?
Friday, November 11, 11
Slide 15
Slide 15 text
• Notifies the observers
• Maintains 2 different lists
• Single Responsibility Principle
• "Every class should have one and
only one responsibility"
Can I Improve JObservable?
Friday, November 11, 11