Test in action – week 1
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Test in action – week 1

on

  • 1,049 views

 

Statistics

Views

Total Views
1,049
Views on SlideShare
1,049
Embed Views
0

Actions

Likes
1
Downloads
3
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Test in action – week 1 Presentation Transcript

  • 1. Test in Action – Week 1 Introduction Hubert Chan
  • 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 TestSpeed Fast Relatively SlowFailure Point Less MoreTest 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
  • 18. What’s TDD?• Principle – Red – Green – Refactor
  • 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
  • 26. Continuous Integration• Continuous Integration – Automation • Build • Testing – More • Static Code Analysis • Test Coverage • Coding Style Checking
  • 27. Continuous Integration• Benefit of Continuous Integration – Fully automate – Catch and enforce to fix bugs – Report
  • 28. Q&A• How do you think about testing?• Think about – TDD / Testing is not elixir – BDD and ATDD – Real OO design – Legacy Code