Unit testing php-unit - phing - selenium_v2


Published on

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Unit testing php-unit - phing - selenium_v2

  1. 1. Automated Unit Testing using PHPUnit, Selenium and Phing Tricode Professional Services www.tricode.nl 01-05-2008 Sander van Beek & Patrick van Dissel
  2. 2. “ One test is worth a thousand expert opinions” -- Bill Nye, The Science Guy
  3. 3. Automated Unit Testing <ul><li>Put Angels on Your Shoulders </li></ul><ul><ul><li>Unit Testing </li></ul></ul><ul><li>Use It Before You Build It </li></ul><ul><ul><li>PHPUnit </li></ul></ul><ul><li>Different Makes a Difference </li></ul><ul><ul><li>Selenium </li></ul></ul><ul><li>Automate, Automate, Automate </li></ul><ul><ul><li>Phing </li></ul></ul>
  4. 4. Put Angels on Your Shoulders <ul><li>Unit Testing is a software verification and validation method where the programmer gains confidence that individual units of source code are fit for use </li></ul><ul><li>A unit is the smallest testable part of an application, e.g. a method </li></ul><ul><li>Unit testing is regression testing , making sure made changes are not a step backwards </li></ul>
  5. 5. Unit Testing <ul><li>Without Unit Testing: </li></ul><ul><li>You write code and stick in some print statements to see the value of a few key variables. You run the code, maybe in a debug mode, and look at the results manually, fix problems that come up </li></ul><ul><li>Then you throw away the print statements or exit the debug mode, and move on to the next item </li></ul>
  6. 6. Unit Testing <ul><li>With Unit Testing: </li></ul><ul><li>With Unit Testing you take that familiar process and instead of throwing away debug code, you save it, and continue to run it automatically </li></ul><ul><li>Instead of manually inspecting the interesting variables, you write code to check for specific values in specific situations </li></ul>
  7. 7. Unit Testing <ul><li>Benefits: </li></ul><ul><li>Provides instant feedback </li></ul><ul><ul><li>Code gets exercised repeatedly. As you change and rewrite your code, test cases will check that you haven’t broken existing contracts. You can quickly identify and fix any problems </li></ul></ul><ul><li>Makes your code robust </li></ul><ul><ul><li>Testing helps you think through the behavior of the code, exercising the positive, negative and exceptional cases </li></ul></ul>
  8. 8. Unit Testing <ul><li>Benefits (continued): </li></ul><ul><li>Can be a helpful design tool </li></ul><ul><ul><li>Unit testing can help you achieve a pragmatic and simpler design </li></ul></ul><ul><li>Is a confidence booster </li></ul><ul><ul><li>You’ve tested your code and exercised its behavior for a variety of different conditions: </li></ul></ul><ul><ul><li>this will give you the confidence when faced with new, high pressure tasks on tight deadlines </li></ul></ul><ul><ul><li>You can change anything as long as the tests stay green ! </li></ul></ul>
  9. 9. Unit Testing <ul><li>Benefits (continued): </li></ul><ul><li>Can act as probes when solving problems </li></ul><ul><ul><li>Tests go red when something is wrong. If something is wrong and tests stay green , that means that new test-cases need to be created for that specific situation </li></ul></ul><ul><li>Are reliable documentation </li></ul><ul><ul><li>Unit tests can serve as accurate, reliable documentation When following an easy naming-convention you can even create documentation based on unit tests </li></ul></ul>
  10. 10. Unit Testing <ul><li>Benefits (continued): </li></ul><ul><li>Are a learning aid </li></ul><ul><ul><li>You can write unit tests against an API to facilitate your learning. The tests not only help you understand the behavior of the API but also help you quickly find any incompatible changes that might be introduced later </li></ul></ul>
  11. 11. Unit Testing <ul><li>Limitations: </li></ul><ul><li>Public API only </li></ul><ul><ul><li>Unit tests are focused on the public API only! Protected and private API can be tested, but wrappers are needed to get this to work. It’s a bit more work, but advised for very important/complex methods </li></ul></ul><ul><li>Tests are not created and updated by themselves </li></ul><ul><ul><li>Creating and keeping unit tests up-to-date is not an automatic process. You must to have the discipline to create and update the tests yourself! </li></ul></ul>
  12. 12. Unit Testing <ul><li>Limitations (continued): </li></ul><ul><li>Not everything can be tested </li></ul><ul><ul><li>By default, unit tests are focused on testing behavior of single units. Integration, performance, GUI and user acceptance tests are not the focus or supported by unit testing alone Using unit testing in combination with other testing tools can extend the reach of the tests. More and more extensions are added to unit testing frameworks to support these tools </li></ul></ul>
  13. 13. Unit Testing <ul><li>Some best practices: </li></ul><ul><li>Don't assume the order in which tests within a test case run </li></ul><ul><li>Avoid writing test cases with side effects </li></ul><ul><li>Do not load data from hard-coded locations on a file system </li></ul><ul><li>Keep tests in the same location as the source code </li></ul><ul><li>Name tests properly </li></ul><ul><li>Keep tests small and fast </li></ul><ul><li>Test Behavior, Not Methods </li></ul><ul><li>Apply the “Too Simple to Break” Rule </li></ul><ul><li>Use Object-Oriented Best Practices In Your Test Cases </li></ul>
  14. 14. Use It Before You Build It <ul><li>PHPUnit is a unit testing framework for PHP5 </li></ul><ul><li>It is one of the xUnit family of frameworks that originated with Kent Beck's SUnit for Smalltalk </li></ul><ul><li>Wikipedia has a nice list of unit testing frameworks: http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks </li></ul>
  15. 15. PHPUnit <ul><li>Static Test structure </li></ul>
  16. 16. PHPUnit <ul><li>Runtime Test structure </li></ul>
  17. 17. PHPUnit <ul><li>Basic concepts of xUnit: </li></ul><ul><ul><li>Test fixture The set of preconditions or state needed for a test to succeed. Also known as a test context. </li></ul></ul><ul><ul><li>Test case A single test method within a test suite. The setUp() and tearDown() methods of the test suite are executed before and after each test case, respectively </li></ul></ul><ul><ul><li>Assertion Evaluate the result of the test case </li></ul></ul><ul><ul><li>Test suite A collection of test cases, and A collection of test suites </li></ul></ul>
  18. 18. A simple Test Suite <ul><li>class ArrayTest </li></ul><ul><li>extends PHPUnit_Framework_TestCase </li></ul><ul><li>{ </li></ul><ul><li>public function testNewArrayIsempty() </li></ul><ul><li>{ </li></ul><ul><li>$array = array (); </li></ul><ul><li>$this -> assertEquals ( 0 , count( $array )); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  19. 19. A simple Test Suite <ul><li>class ArrayTest </li></ul><ul><li>extends PHPUnit_Framework_TestCase </li></ul><ul><li>{ </li></ul><ul><li>protected $fixture ; </li></ul><ul><li>protected function setUp() </li></ul><ul><li>{ </li></ul><ul><li>parent ::setUp(); </li></ul><ul><li>$this ->fixture = array (); </li></ul><ul><li>} </li></ul><ul><li>protected function tearDown() </li></ul><ul><li>{ </li></ul><ul><li>parent ::tearDown(); </li></ul><ul><li>$this ->fixture = null; </li></ul><ul><li>} </li></ul><ul><li>public function testNewArrayIsempty() </li></ul><ul><li>{ </li></ul><ul><li>$this ->assertEquals( 0 , count( $this ->fixture )); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  20. 20. Writing tests <ul><li>A test suite is a class that extends from PHPUnit_Framework_TestCase </li></ul><ul><li>A test case is a method within a test suite, which: </li></ul><ul><ul><li>Method name starts with ‘ test ’, or </li></ul></ul><ul><ul><li>Is annotated with ‘ @test ’ </li></ul></ul><ul><li>The setUp() method of a test suite can be overriden and should be used to setup the fixture. setUp() is called before each test case </li></ul><ul><li>The tearDown() method of a test suite can be overriden and should be used to cleanup the fixture. tearDown() is called after each test case </li></ul>
  21. 21. Executing tests <ul><li>DEMO </li></ul><ul><li>From command-line </li></ul><ul><li>From Zend Studio </li></ul><ul><ul><li>Generating a result report </li></ul></ul><ul><ul><li>Code-coverage </li></ul></ul>
  22. 22. Data providers <ul><li>class  DataTest </li></ul><ul><li>extends  PHPUnit_Framework_TestCase </li></ul><ul><li>{ </li></ul><ul><li>/** </li></ul><ul><li>      *  @dataProvider provider </li></ul><ul><li>      */ </li></ul><ul><li>public function  testAdd( $a ,  $b ,  $c ) </li></ul><ul><li>     { </li></ul><ul><li>         $this ->assertEquals( $c ,  $a  +  $b ); </li></ul><ul><li>     } </li></ul><ul><li>     public function   provider() </li></ul><ul><li>     { </li></ul><ul><li>     return array ( </li></ul><ul><li>           array ( 0, 0, 0 ) </li></ul><ul><li>       , array (0, 1, 1) </li></ul><ul><li>        , array (1, 0, 1) </li></ul><ul><li>           , array (1, 1, 3) // fails </li></ul><ul><li>         ); </li></ul><ul><li>     } </li></ul><ul><li>} </li></ul>
  23. 23. Testing exceptions <ul><li>class  ExceptionTest  </li></ul><ul><li>extends  PHPUnit_Framework_TestCase </li></ul><ul><li>{ </li></ul><ul><li>     /** </li></ul><ul><li>      *  @expectedException InvalidArgumentException </li></ul><ul><li>      */ </li></ul><ul><li>     public function  testException() </li></ul><ul><li>     { </li></ul><ul><li>throw new InvalidArgumentException (); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>PHPUnit converts PHP errors, notices and warnings to </li></ul><ul><li>exceptions, respectively: </li></ul><ul><ul><li>PHPUnit_Framework_Error </li></ul></ul><ul><ul><li>PHPUnit_Framework_Error_Notice </li></ul></ul><ul><ul><li>PHPUnit_Framework_Error_Warning </li></ul></ul>
  24. 24. Testing performance <ul><li>class  PerformanceTest  </li></ul><ul><li>extends   PHPUnit_Extensions_PerformanceTestCase </li></ul><ul><li>{ </li></ul><ul><li>public function  testPerformance() </li></ul><ul><li>     { </li></ul><ul><li>         $this -> setMaxRunningTime ( 2 ); // 2 seconds </li></ul><ul><li>         sleep( 1 ); // 1 second </li></ul><ul><li>     } </li></ul><ul><li>} </li></ul>
  25. 25. Testing output <ul><li>class  OutputTest </li></ul><ul><li>extends   PHPUnit_Extensions_OutputTestCase </li></ul><ul><li>{ </li></ul><ul><li>     public function  testExpectFooActualFoo() </li></ul><ul><li>     { </li></ul><ul><li>         $this -> expectOutputString(' foo '); </li></ul><ul><li>         print  ' foo '; </li></ul><ul><li>     }  </li></ul><ul><li>} </li></ul><ul><li>PHP's Output Buffering feature is used to provide the </li></ul><ul><li>functionality that is necessary for this </li></ul>
  26. 26. Advanced usage <ul><li>Code Coverage Analysis </li></ul><ul><li>Grouping of tests </li></ul><ul><ul><li>@group </li></ul></ul><ul><li>Mark tests incomplete </li></ul><ul><li>Mark tests skipped </li></ul><ul><li>Behavior-Driven development </li></ul><ul><ul><li>stories </li></ul></ul><ul><li>Agile documentation </li></ul><ul><ul><li>--story, --tap, --testdox </li></ul></ul><ul><li>Test doubles </li></ul><ul><ul><li>Stubs and Mocks </li></ul></ul><ul><li>Skeleton generation </li></ul><ul><ul><li>from test case to class </li></ul></ul><ul><ul><li>from class to test case </li></ul></ul>
  27. 27. Documentation <ul><li>Want to learn more? </li></ul><ul><ul><li>PHPUnit pocket guide www.phpunit.de /manual/ </li></ul></ul><ul><ul><li>Testing patterns www.xunitpatterns.com </li></ul></ul><ul><li>At the end you will receive: </li></ul><ul><li>A PHPUnit Cheatsheet for version 3.3 </li></ul><ul><li>A Unit Testing best practices document </li></ul><ul><li>A Test-Driven-Development: </li></ul><ul><ul><li>Rhythm reference guide </li></ul></ul><ul><ul><li>Quick reference guid </li></ul></ul>
  28. 28. Selenium Web application automated testing suite Tricode Professional Services www.tricode.nl 24-04-2009 Sander van Beek
  29. 29. Selenium <ul><li>Selenium is a suite of tools to automate web app testing across many platforms. </li></ul><ul><li>Selenium... </li></ul><ul><li>runs in many browsers and operating systems </li></ul><ul><li>can be controlled by many programming languages and testing frameworks. </li></ul>
  30. 30. Selenium <ul><li>Selenium suite has 3 main tools: </li></ul><ul><li>Selenium IDE </li></ul><ul><ul><li>Create tests </li></ul></ul><ul><li>Selenium RC (remote contol) </li></ul><ul><ul><li>Run tests in different browsers </li></ul></ul><ul><li>Selenium Grid </li></ul><ul><ul><li>Scale out, speed up </li></ul></ul>
  31. 31. Selenium
  32. 32. Selenium IDE <ul><li>Firefox plug-in </li></ul><ul><li>Record tests (demo) </li></ul><ul><li>Export them to different formats </li></ul><ul><ul><li>HTML </li></ul></ul><ul><ul><li>C# </li></ul></ul><ul><ul><li>Java </li></ul></ul><ul><ul><li>PHP </li></ul></ul><ul><ul><li>Ruby </li></ul></ul><ul><ul><li>Perl </li></ul></ul><ul><ul><li>Python </li></ul></ul>
  33. 33. Selenium IDE <ul><li>Commands: </li></ul><ul><li>Actions </li></ul><ul><ul><li>open, click, refresh </li></ul></ul><ul><li>Accessors </li></ul><ul><ul><li>Get state and store it </li></ul></ul><ul><li>Assertions </li></ul><ul><ul><li>Verify state. Have 3 modes: </li></ul></ul><ul><ul><li>- assert (halt on error) </li></ul></ul><ul><ul><li>- verify (generate warning on error) </li></ul></ul><ul><ul><li>- waitFor (wait for condition to become true) </li></ul></ul>
  34. 34. Selenium RC <ul><li>Run distributed Selenium tests on one (remote) server </li></ul>
  35. 35. Selenium RC <ul><li>Supported browsers: </li></ul><ul><ul><li>Firefox 2, 3 </li></ul></ul><ul><ul><li>IE 7, 8 </li></ul></ul><ul><ul><li>Safari 2, 3 </li></ul></ul><ul><ul><li>Opera 8, 9 </li></ul></ul><ul><ul><li>Others: partially </li></ul></ul><ul><li>Supported operating systems: </li></ul><ul><ul><li>Windows </li></ul></ul><ul><ul><li>Linux </li></ul></ul><ul><ul><li>OS X, </li></ul></ul><ul><ul><li>Solaris </li></ul></ul><ul><ul><li>Others (as long as they run supported browsers) </li></ul></ul>
  36. 36. Selenium Grid <ul><li>Selenium in parallel on multiple servers (or VM’s) </li></ul>
  37. 37. Selenium Grid <ul><li>http://saucelabs.com/ : Selenium on EC2 </li></ul><ul><li>TestSwarm: http:// ejohn.org/blog/javascript -testing-does-not-scale/ </li></ul>
  38. 38. Questions <ul><li>More info: http://seleniumhq.org/ </li></ul>
  39. 39. Phing An automated build system based on Apache ant Tricode Professional Services www.tricode.nl 14-12-2008 Sander van Beek
  40. 40. Phing <ul><li>PHing Is Not GNU make; </li></ul><ul><li>it's a project build system based on Apache Ant. You can do anything with it that you could do with a traditional build system like GNU make, and its use of simple XML build files and extensible PHP &quot;task&quot; classes make it an easy-to-use and highly flexible build framework. Features include file transformations (e.g. token replacement, XSLT transformation, Smarty template transformations), file system operations, interactive build support, SQL execution, CVS operations, tools for creating PEAR packages, and much more. </li></ul>
  41. 41. Phing <ul><li>Automated build tool </li></ul><ul><li>Types of automated builds: </li></ul><ul><ul><li>On demand </li></ul></ul><ul><ul><li>Scheduled (nightly build) </li></ul></ul><ul><ul><li>Automated (continuous integration) </li></ul></ul>
  42. 42. Phing <ul><li>Why ‘build’ PHP? </li></ul><ul><ul><li>Improve product quality </li></ul></ul><ul><ul><li>Eliminate redundant tasks </li></ul></ul><ul><ul><li>Minimize &quot;bad builds&quot; </li></ul></ul><ul><ul><li>Eliminate dependencies on key personnel </li></ul></ul><ul><ul><li>Have history of builds and releases in order to investigate issues </li></ul></ul><ul><ul><li>Save time and money - because of the reasons listed above </li></ul></ul>
  43. 43. Concepts <ul><li>Targets & taks </li></ul><ul><li>The build file: build.xml </li></ul><ul><ul><li>Project </li></ul></ul><ul><ul><ul><li>Target1 </li></ul></ul></ul><ul><ul><ul><ul><li>Task1 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Task2 </li></ul></ul></ul></ul><ul><ul><ul><li>Target2 </li></ul></ul></ul><ul><ul><ul><ul><li>Task1 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Task2 </li></ul></ul></ul></ul><ul><ul><ul><li>Target3 </li></ul></ul></ul><ul><ul><ul><ul><li>Task1 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Task2 </li></ul></ul></ul></ul><ul><li>Targets can be dependant on each other </li></ul><ul><li>One default target (run when no task is specified on command line) </li></ul>
  44. 44. Task types <ul><li>Core </li></ul><ul><ul><li>PhingCall, input, phpeval, condition, echo, exec </li></ul></ul><ul><li>File system </li></ul><ul><ul><li>Copy, delete, move, mkdir, touch, available, resolvePath </li></ul></ul><ul><li>Transformation </li></ul><ul><ul><li>XsltTask, ReflexiveTask </li></ul></ul><ul><li>Packaging </li></ul><ul><ul><li>pearPackageTask, zipTask, tarTask, dbdeploy, ioncube </li></ul></ul>
  45. 45. Task types <ul><li>Version control </li></ul><ul><ul><li>svnCheckout, svnExport, svnUpdate, svnLastRevision </li></ul></ul><ul><li>Quality assurance </li></ul><ul><ul><li>phpunitTask, zendCodeAnalyser, phpDocumentor, phpCodesniffer, coverageXXX, phpLint </li></ul></ul><ul><li>Filters </li></ul><ul><ul><li>Replacing, stripping comments/whitespaces, line numbering, tidy and more </li></ul></ul><ul><li>Easy to add new tasks </li></ul>
  46. 46. Filesets <ul><li>Can be used in many tasks </li></ul><ul><li><fileset dir=&quot;/etc&quot; > </li></ul><ul><li><include name=&quot;httpd/**&quot; /> </li></ul><ul><li><include name=&quot;php.ini&quot; /> </li></ul><ul><li><exclude name=“doc/**&quot; /> </li></ul><ul><li></fileset> </li></ul><ul><li>**/* <- Ant glob syntax </li></ul>
  47. 47. Property files <ul><li>Create environment specific builds </li></ul><ul><li>E.g. build.properties, c ontains ‘key=value’ lines. E.g: </li></ul><ul><li>database.user=upcpl </li></ul><ul><li>Can be used to replace properties in source code - e.g. ${database.user} </li></ul><ul><li><property file=&quot;build.properties&quot; /> </li></ul><ul><li><reflexive> </li></ul><ul><li><fileset dir=“config&quot;> </li></ul><ul><li><include file=“config.php”/> </li></ul><ul><li></fileset> </li></ul><ul><li><filterchain> </li></ul><ul><li><expandproperties /> </li></ul><ul><li></filterchain> </li></ul><ul><li></reflexive> </li></ul>
  48. 48. Running phine <ul><li>Simply run ‘phing’ in the directory containing build.xml </li></ul><ul><li>Optionally specify a target as the first parameter </li></ul><ul><li>Can also be run automated, e.g. from cruisecontrol or hudson </li></ul>