0
Test in Action – Week 1      Introduction      Hubert Chan
What is Test?• Testing  – Definition     • Verification between actual and expected result  – Example     • fibonacci(6) =...
Definition of Test – Unit Test• Unit Test  – Write code to test your code  – White box testing  – Unit Under Test     • Cl...
Definition of Test – Unit Test• Single Failure Point  – Limited Dependency  – Limited to     • Module     • Class
Unit Testing Example• Fibonacci Number  function fib($n) {      if ($n < 3 ) {          return 1;      } else {          r...
Unit Testing Example• Test Code  require_once(__DIR__ . /fib.php);  class FibTest extends PHPUnit_Framework_TestCase  {   ...
Unit Testing Example• Handler Test Code  class LogHandlerTest extends PHPUnit_Framework_TestCase  {      public function t...
Definition of Test – Integration Test• Integration Test  – Testing more dependent modules as a group  – Black Box Testing ...
Definition of Test – Integration Test• Multiple Failure Point  – Dependency  – System Wise
Integration Test Example• Selenium Example  <?php  require_once PHPUnit/Extensions/SeleniumTestCase.php;  class WebTest ex...
Difference                       Unit Testing   Integration TestSpeed                  Fast           Relatively SlowFailu...
Benefit of Test• Facilitates Change  – Ensure modules still work correctly after change  – Foundation of change     • Feat...
What’s good Unit Test• Good Unit Test  – Automated and repeatable  – Reliable and consistent result  – Focus on units  – A...
Benefit of Unit Test• Software Quality Enhancement  – Verification     • Catch bugs in code level     • Faster than integr...
Benefit of Unit Test• Documentation  – Living documentation of the module  – Gain a basic understanding of the unit API
Benefit of Unit Test• Code Quality Enhancement  – Foundation of Refactoring  – Test drives better OO design     • Good OO ...
Hard to do Unit Test?• Why?  – Too complex     • Violate Single Responsibility Principle  – Breaking encapsulation  – Not ...
What’s TDD?• Principle  – Red  – Green  – Refactor
TDD – Red• Red  – Before actual implementation  – Write a test that fails
TDD – Red (Example)• Red  – Before writing LogHandler implementation  – Write the test code first  class LogHandlerTest ex...
TDD – Green• Green  – Make the code work  – Using mock/stub for dependency
TDD – Green (Example)• Green (Example)  class LogHander {      private dbmodel;      public function generate_ajax() {    ...
TDD – Green (Example)• In Green Example  – Faking in TDD     • We need a “fake” dbmodel     • Can dbmodel->query() return ...
TDD – Refactor• Refactor  – Do Refactoring  – Implement Real dbmodel  – It breaks the test     • Repeat another TDD cycle
Benefit of TDD• Benefit  – Make you think about your design     • Design by Contract     • Modularize/Flexible/Extensible ...
Continuous Integration• Continuous Integration  – Automation     • Build     • Testing  – More     • Static Code Analysis ...
Continuous Integration• Benefit of Continuous Integration  – Fully automate  – Catch and enforce to fix bugs  – Report
Q&A• How do you think about testing?• Think about  – TDD / Testing is not elixir  – BDD and ATDD  – Real OO design  – Lega...
Upcoming SlideShare
Loading in...5
×

Test in action – week 1

909

Published on

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
909
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Test in action – week 1"

  1. 1. Test in Action – Week 1 Introduction Hubert Chan
  2. 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. 3. Definition of Test – Unit Test• Unit Test – Write code to test your code – White box testing – Unit Under Test • Class • Function
  4. 4. Definition of Test – Unit Test• Single Failure Point – Limited Dependency – Limited to • Module • Class
  5. 5. Unit Testing Example• Fibonacci Number function fib($n) { if ($n < 3 ) { return 1; } else { return fib($n-1) + fib($n-2); } }
  6. 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. 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. 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. 9. Definition of Test – Integration Test• Multiple Failure Point – Dependency – System Wise
  10. 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. 11. Difference Unit Testing Integration TestSpeed Fast Relatively SlowFailure Point Less MoreTest Coverage Better Neutral (*)Failure Path Testing Easier Harder
  12. 12. Benefit of Test• Facilitates Change – Ensure modules still work correctly after change – Foundation of change • Feature change • Refactoring
  13. 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. 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. 15. Benefit of Unit Test• Documentation – Living documentation of the module – Gain a basic understanding of the unit API
  16. 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. 17. Hard to do Unit Test?• Why? – Too complex • Violate Single Responsibility Principle – Breaking encapsulation – Not program by interface – Messy hierarchy and responsibility
  18. 18. What’s TDD?• Principle – Red – Green – Refactor
  19. 19. TDD – Red• Red – Before actual implementation – Write a test that fails
  20. 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. 21. TDD – Green• Green – Make the code work – Using mock/stub for dependency
  22. 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. 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. 24. TDD – Refactor• Refactor – Do Refactoring – Implement Real dbmodel – It breaks the test • Repeat another TDD cycle
  25. 25. Benefit of TDD• Benefit – Make you think about your design • Design by Contract • Modularize/Flexible/Extensible – Enforce you have tests
  26. 26. Continuous Integration• Continuous Integration – Automation • Build • Testing – More • Static Code Analysis • Test Coverage • Coding Style Checking
  27. 27. Continuous Integration• Benefit of Continuous Integration – Fully automate – Catch and enforce to fix bugs – Report
  28. 28. Q&A• How do you think about testing?• Think about – TDD / Testing is not elixir – BDD and ATDD – Real OO design – Legacy Code
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×