2. What is Test?
• Testing
– Definition
• Verification between actual and expected result
– Example
• fibonacci(6) == 8
• Fill <script> string in input box, there is no XSS hole
3. Definition of Test – Unit Test
• Unit Test
– Write code to test your code
– White box testing
– Unit Under Test
• Class
• Function
4. Definition of Test – Unit Test
• Single Failure Point
– Limited Dependency
– Limited to
• Module
• Class
5. Unit Testing Example
• Fibonacci Number
function fib($n) {
if ($n < 3 ) {
return 1;
} else {
return fib($n-1) + fib($n-2);
}
}
6. Unit Testing Example
• Test Code
require_once(__DIR__ . '/fib.php');
class FibTest extends PHPUnit_Framework_TestCase
{
public function testfib() {
$res = fib(6);
$this->assertEquals(8, $res);
}
}
7. Unit Testing Example
• Handler Test Code
class LogHandlerTest extends PHPUnit_Framework_TestCase
{
public function test_generate_ajax_expect_json() {
$handler = new LogHandler();
$res_json = $handler->generate_ajax();
$expect_json = "...";
$this->assertEquals($expect_json, $res_json);
}
}
8. Definition of Test – Integration Test
• Integration Test
– Testing more dependent modules as a group
– Black Box Testing
– Usage Model Testing
• Feature Based Testing
• User Targeted Testing
9. Definition of Test – Integration Test
• Multiple Failure Point
– Dependency
– System Wise
10. Integration Test Example
• Selenium Example
<?php
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
class WebTest extends PHPUnit_Extensions_SeleniumTestCase {
protected function setUp() {
$this->setBrowser('*firefox');
$this->setBrowserUrl('http://www.example.com/');
}
public function testTitle() {
$this->open('http://www.example.com/');
$this->assertTitle('Example WWW Page');
}
}
11. Difference
Unit Testing Integration Test
Speed Fast Relatively Slow
Failure Point Less More
Test Coverage Better Neutral (*)
Failure Path Testing Easier Harder
12. Benefit of Test
• Facilitates Change
– Ensure modules still work correctly after change
– Foundation of change
• Feature change
• Refactoring
13. What’s good Unit Test
• Good Unit Test
– Automated and repeatable
– Reliable and consistent result
– Focus on units
– Anyone/CI should be able to run it
– It should run at the push of a button
– It should run quickly
14. Benefit of Unit Test
• Software Quality Enhancement
– Verification
• Catch bugs in code level
• Faster than integration test
• Quality Enforcing
– Faster bug identification
• Identify the bugs location
15. Benefit of Unit Test
• Documentation
– Living documentation of the module
– Gain a basic understanding of the unit API
16. Benefit of Unit Test
• Code Quality Enhancement
– Foundation of Refactoring
– Test drives better OO design
• Good OO design is essential for unit test
– Testability
• Singleton => Anti-Pattern for testing
17. Hard to do Unit Test?
• Why?
– Too complex
• Violate Single Responsibility Principle
– Breaking encapsulation
– Not program by interface
– Messy hierarchy and responsibility
19. TDD – Red
• Red
– Before actual implementation
– Write a test that fails
20. TDD – Red (Example)
• Red
– Before writing LogHandler implementation
– Write the test code first
class LogHandlerTest extends PHPUnit_Framework_TestCase
{
public function test_generate_ajax_expect_json() {
$handler = new LogHandler();
$res_json = $handler->generate_ajax();
$expect_json = "...";
$this->assertEquals($expect_json, $res_json);
}
}
21. TDD – Green
• Green
– Make the code work
– Using mock/stub for dependency
22. TDD – Green (Example)
• Green (Example)
class LogHander {
private dbmodel;
public function generate_ajax() {
$data = dbmodel->query();
return $this->data_to_json($data);
}
protected function data_to_json($data) {
// processing
...
return json_encode($res);
}
}
23. TDD – Green (Example)
• In Green Example
– Faking in TDD
• We need a “fake” dbmodel
• Can dbmodel->query() return dummy data?
– Think
• How do we fake it?
• Is it a good responsibility for module?
24. TDD – Refactor
• Refactor
– Do Refactoring
– Implement Real dbmodel
– It breaks the test
• Repeat another TDD cycle
25. Benefit of TDD
• Benefit
– Make you think about your design
• Design by Contract
• Modularize/Flexible/Extensible
– Enforce you have tests