• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Test in action – week 1
 

Test in action – week 1

on

  • 974 views

 

Statistics

Views

Total Views
974
Views on SlideShare
974
Embed Views
0

Actions

Likes
0
Downloads
3
Comments
0

0 Embeds 0

No embeds

Accessibility

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 Test in action – week 1 Presentation Transcript

    • Test in Action – Week 1 Introduction Hubert Chan
    • 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
    • Definition of Test – Unit Test• Unit Test – Write code to test your code – White box testing – Unit Under Test • Class • Function
    • 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 { return fib($n-1) + fib($n-2); } }
    • 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); } }
    • 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); } }
    • 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
    • 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 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); } }
    • Difference Unit Testing Integration TestSpeed Fast Relatively SlowFailure Point Less MoreTest Coverage Better Neutral (*)Failure Path Testing Easier Harder
    • Benefit of Test• Facilitates Change – Ensure modules still work correctly after change – Foundation of change • Feature change • Refactoring
    • 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
    • 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
    • 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 design is essential for unit test – Testability • Singleton => Anti-Pattern for testing
    • Hard to do Unit Test?• Why? – Too complex • Violate Single Responsibility Principle – Breaking encapsulation – Not program by interface – Messy hierarchy and responsibility
    • 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 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); } }
    • 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() { $data = dbmodel->query(); return $this->data_to_json($data); } protected function data_to_json($data) { // processing ... return json_encode($res); } }
    • 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?
    • 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 – Enforce you have tests
    • Continuous Integration• Continuous Integration – Automation • Build • Testing – More • Static Code Analysis • Test Coverage • Coding Style Checking
    • 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 – Legacy Code