Your SlideShare is downloading. ×
0
Automated Unit Testing Getting Started The 2008 DC PHP Conference June 2nd, 2008
Hello! <ul><ul><li>Mike Lively </li></ul></ul><ul><ul><li>Lead Developer Selling Source, Inc. </li></ul></ul><ul><ul><li>P...
Automated Unit Testing <ul><ul><li>What is unit testing? </li></ul></ul><ul><ul><li>Why unit test? </li></ul></ul><ul><ul>...
Automated Unit Testing <ul><ul><li>What is unit testing? </li></ul></ul><ul><ul><li>Why unit test? </li></ul></ul><ul><ul>...
What is Unit Testing <ul><ul><li>From Wikipedia: Unit testing is a procedure used to validate that  individual units  of s...
Automated Unit Testing <ul><ul><li>What is unit testing? </li></ul></ul><ul><ul><li>Why unit test? </li></ul></ul><ul><ul>...
Why should I unit test? <ul><ul><li>PHP is being used more and more for business critical applications. </li></ul></ul><ul...
Why should I unit test? <ul><ul><li>Makes integration testing and functional testing less stressful. </li></ul></ul><ul><u...
Automated Unit Testing <ul><ul><li>What is unit testing? </li></ul></ul><ul><ul><li>Why unit test? </li></ul></ul><ul><ul>...
What Should I Test? <ul><ul><li>Options </li></ul></ul><ul><ul><ul><li>Test everything </li></ul></ul></ul><ul><ul><ul><li...
Automated Unit Testing <ul><ul><li>What is unit testing? </li></ul></ul><ul><ul><li>Why unit test? </li></ul></ul><ul><ul>...
When do I Test? <ul><ul><li>Option 1: Test Driven Development </li></ul></ul><ul><ul><ul><li>Tests before code </li></ul><...
When do I Test? <ul><ul><li>Test as soon as you can in your process </li></ul></ul><ul><ul><li>Always create regression te...
Automated Unit Testing <ul><ul><li>What is unit testing? </li></ul></ul><ul><ul><li>Why unit test? </li></ul></ul><ul><ul>...
How do I Test <ul><ul><li>Tests should be independant of the code being tested. </li></ul></ul><ul><ul><li>Tests should be...
PHPUnit <ul><ul><li>Created by Sebastian Bergmann </li></ul></ul><ul><ul><li>JUnit Port </li></ul></ul><ul><ul><li>Very Ri...
PHPUnit - Creating a Test Case <?php require_once( 'PHPUnit/Framework/TestCase.php' ); class  SubStrTest  extends  PHPUnit...
PHPUnit - Fixtures <ul><ul><li>Each test needs an established, predictable environment. </li></ul></ul><ul><ul><li>Tests s...
PHPUnit - Call Order PHPUnit_Framework_TestCase::setUp(); PHPUnit_Framework_TestCase::testMyCode1(); PHPUnit_Framework_Tes...
PHPUnit - Fixtures <?php class  BankAccountTest  extends  PHPUnit_Framework_TestCase {     protected  $ba ;     protected ...
PHPUnit - Testing Code <ul><ul><li>The basis of unit testing is making assertions regarding the expected results of a func...
PHPUnit - Testing Code <?php $this -> assertEquals ( 'o wo' ,  substr ( 'hello world' ,  4 ,  4 )); $this -> assertGreater...
PHPUnit - Testing Code <ul><ul><li>The basis of unit testing is making assertions regarding the expected results of a func...
PHPUnit - Testing Code <?php require_once  'PHPUnit/Framework.php' ;   class  ExceptionTest  extends  PHPUnit_Framework_Te...
PHPUnit - Running a Test Usage: phpunit [switches] UnitTest [UnitTest.php]      --log-graphviz <file>  Log test execution ...
PHPUnit - Running a Test Usage: phpunit [switches] UnitTest [UnitTest.php]      --no-syntax-check      Disable syntax chec...
PHPUnit - Test Suites <ul><ul><li>When you start amassing tests you need a way to organize them </li></ul></ul><ul><ul><li...
PHPUnit - Test Suites <?php if (! defined ( 'PHPUnit_MAIN_METHOD' ))  define ( 'PHPUnit_MAIN_METHOD' ,  'AllTests::main' )...
PHPUnit - Test Suites <ul><ul><li>Using the xml config </li></ul></ul><ul><ul><ul><li>You can specify a directory and a fi...
PHPUnit - XML Configuration Running a list of tests <phpunit>   <testsuite name=&quot;My Test Suite&quot;>     <directory ...
PHPUnit - XML Configuration <ul><ul><li>So, how can I group tests at will when using XML to set up the test suite? </li></...
PHPUnit - XML Configuration <?php require_once( 'PHPUnit/Framework/TestCase.php' ); class  SubStrTest  extends  PHPUnit_Fr...
PHPUnit - Annotations <ul><ul><li>Annotations can be an incredible time saver! </li></ul></ul><ul><ul><li>@test - Marks a ...
PHPUnit - Annotations <ul><ul><li>@test - Marks a particular method in a test case as a test </li></ul></ul><ul><ul><li><?...
PHPUnit - Annotations <ul><ul><li>@dataProvider - Allow running the same tests using different data. </li></ul></ul><ul><u...
PHPUnit - Annotations <ul><ul><li>@expectedException - a convenient shortcut for setExpectedException </li></ul></ul><ul><...
PHPUnit - Skeleton Generator <ul><ul><li>When creating simple tests this can be incredibly useful. </li></ul></ul><ul><ul>...
PHPUnit - Skeleton Generator <ul><ul><li>Result?      /**      * @todo Implement testAdd().      */      public function  ...
PHPUnit - Skeleton Generator > phpunit --verbose CalculatorTest PHPUnit 3.2.19 by Sebastian Bergmann. CalculatorTest I Tim...
PHPUnit - Skeleton Generator <ul><ul><li>Save some more time! </li></ul></ul><ul><ul><li><?php class  Calculator {      /*...
PHPUnit - Skeleton Generator <ul><ul><li>Result </li></ul></ul><ul><ul><li>    /**      * Generated from @assert (0, 0) ==...
PHPUnit - Skeleton Generator > phpunit --verbose CalculatorTest PHPUnit 3.2.19 by Sebastian Bergmann. CalculatorTest ........
PHPUnit - Skeleton Generator @assert (...) == X     > assertEquals(X, method(...)) @assert (...) != X     > assertNotEqual...
PHPUnit - Incomplete Tests TODO: finish slide
PHPUnit - Incomplete Tests Slide TODO: finish slide don't worry...just a little irony
PHPUnit - Incomplete Tests <?php require_once  'PHPUnit/Framework.php' ;   class  SampleTest  extends  PHPUnit_Framework_T...
PHPUnit - SkippedTests <?php require_once  'PHPUnit/Framework.php' ;   class  DatabaseTest  extends  PHPUnit_Framework_Tes...
Advanced Features Mock Objects Database Testing Selenium RC PHPUnderControl Come see me again! Same room, same day, 3:30 PM
Thanks http://phpun.it http://planet.phpunit.de http://digitalsandwich.com http://dev.sellingsource.com Questions???
Upcoming SlideShare
Loading in...5
×

Automated Unit Testing

14,951

Published on

Using PHPUnit to create a unit testing framework.Presented at the 2008 DC PHP Conference

Published in: Technology, Education
2 Comments
11 Likes
Statistics
Notes
No Downloads
Views
Total Views
14,951
On Slideshare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
526
Comments
2
Likes
11
Embeds 0
No embeds

No notes for slide

Transcript of "Automated Unit Testing"

  1. 1. Automated Unit Testing Getting Started The 2008 DC PHP Conference June 2nd, 2008
  2. 2. Hello! <ul><ul><li>Mike Lively </li></ul></ul><ul><ul><li>Lead Developer Selling Source, Inc. </li></ul></ul><ul><ul><li>PHPUnit Contributor </li></ul></ul><ul><ul><li>PHPUnit Database Extension </li></ul></ul><ul><ul><li>A frequent inhabitant of #phpc </li></ul></ul><ul><ul><li>http://digitalsandwich.com </li></ul></ul>
  3. 3. Automated Unit Testing <ul><ul><li>What is unit testing? </li></ul></ul><ul><ul><li>Why unit test? </li></ul></ul><ul><ul><li>What do I test? </li></ul></ul><ul><ul><li>When do I test? </li></ul></ul><ul><ul><li>How do I test it? </li></ul></ul>
  4. 4. Automated Unit Testing <ul><ul><li>What is unit testing? </li></ul></ul><ul><ul><li>Why unit test? </li></ul></ul><ul><ul><li>What do I test? </li></ul></ul><ul><ul><li>When do I test? </li></ul></ul><ul><ul><li>How do I test it? </li></ul></ul>
  5. 5. What is Unit Testing <ul><ul><li>From Wikipedia: Unit testing is a procedure used to validate that individual units of source code are working properly. </li></ul></ul><ul><ul><li>Tests should test as little as possible. </li></ul></ul><ul><ul><ul><li>Each test should validate a single unit. </li></ul></ul></ul><ul><ul><li>Unit testing is just a single aspect of quality assurance. </li></ul></ul><ul><ul><ul><li>Unit Testing </li></ul></ul></ul><ul><ul><ul><li>Functional Testing </li></ul></ul></ul><ul><ul><ul><li>System Tests </li></ul></ul></ul><ul><ul><ul><li>Acceptance Tests </li></ul></ul></ul><ul><ul><ul><li>Code Review </li></ul></ul></ul><ul><ul><ul><li>Etc. </li></ul></ul></ul>
  6. 6. Automated Unit Testing <ul><ul><li>What is unit testing? </li></ul></ul><ul><ul><li>Why unit test? </li></ul></ul><ul><ul><li>What do I test? </li></ul></ul><ul><ul><li>When do I test? </li></ul></ul><ul><ul><li>How do I test it? </li></ul></ul>
  7. 7. Why should I unit test? <ul><ul><li>PHP is being used more and more for business critical applications. </li></ul></ul><ul><ul><li>There are two periods in which bugs will be found: during development and after development. When would you rather find your bugs? </li></ul></ul><ul><ul><li>Regression testing - make it safer to change code. </li></ul></ul>
  8. 8. Why should I unit test? <ul><ul><li>Makes integration testing and functional testing less stressful. </li></ul></ul><ul><ul><li>Provides built in examples of code use. </li></ul></ul><ul><ul><li>Can be used effectively as a design validation tool. </li></ul></ul><ul><ul><ul><li>Code that is hard to test is often hard to reuse. </li></ul></ul></ul><ul><ul><ul><li>Tests will expose difficult to use APIs. </li></ul></ul></ul>
  9. 9. Automated Unit Testing <ul><ul><li>What is unit testing? </li></ul></ul><ul><ul><li>Why unit test? </li></ul></ul><ul><ul><li>What do I test? </li></ul></ul><ul><ul><li>When do I test? </li></ul></ul><ul><ul><li>How do I test it? </li></ul></ul>
  10. 10. What Should I Test? <ul><ul><li>Options </li></ul></ul><ul><ul><ul><li>Test everything </li></ul></ul></ul><ul><ul><ul><li>Test critical code </li></ul></ul></ul><ul><ul><ul><li>Test frequently changing code </li></ul></ul></ul><ul><ul><ul><li>Test buggy code </li></ul></ul></ul><ul><ul><li>Coverage has to be balanced with project timelines </li></ul></ul><ul><ul><li>Unit testing can save time on debugging </li></ul></ul><ul><ul><li>If you can't test everything make sure your testing time is well spent </li></ul></ul>
  11. 11. Automated Unit Testing <ul><ul><li>What is unit testing? </li></ul></ul><ul><ul><li>Why unit test? </li></ul></ul><ul><ul><li>What do I test? </li></ul></ul><ul><ul><li>When do I test? </li></ul></ul><ul><ul><li>How do I test it? </li></ul></ul>
  12. 12. When do I Test? <ul><ul><li>Option 1: Test Driven Development </li></ul></ul><ul><ul><ul><li>Tests before code </li></ul></ul></ul><ul><ul><ul><li>Tests are written to fail, code is written to fix the tests </li></ul></ul></ul><ul><ul><ul><li>Works for some people, but just like cake, some people like it...some people prefer pie. </li></ul></ul></ul><ul><ul><li>Option 2: Test Last </li></ul></ul><ul><ul><ul><li>Tests are written on completed code. </li></ul></ul></ul><ul><ul><ul><li>When projects are rushed this tends to be the testing method </li></ul></ul></ul><ul><ul><ul><li>Tests risk being biased </li></ul></ul></ul><ul><ul><li>Option 3: Test During </li></ul></ul><ul><ul><ul><li>Test problem areas after they are identified </li></ul></ul></ul>
  13. 13. When do I Test? <ul><ul><li>Test as soon as you can in your process </li></ul></ul><ul><ul><li>Always create regression tests for new bugs </li></ul></ul><ul><ul><li>Don't become a victim of testing exhaustion </li></ul></ul><ul><ul><ul><li>start small, get comfortable </li></ul></ul></ul>
  14. 14. Automated Unit Testing <ul><ul><li>What is unit testing? </li></ul></ul><ul><ul><li>Why unit test? </li></ul></ul><ul><ul><li>What do I test? </li></ul></ul><ul><ul><li>When do I test? </li></ul></ul><ul><ul><li>How do I test it? </li></ul></ul>
  15. 15. How do I Test <ul><ul><li>Tests should be independant of the code being tested. </li></ul></ul><ul><ul><li>Tests should be easy to write </li></ul></ul><ul><ul><li>Tests should be easy to run </li></ul></ul><ul><ul><li>Tests should be easy to understand </li></ul></ul>
  16. 16. PHPUnit <ul><ul><li>Created by Sebastian Bergmann </li></ul></ul><ul><ul><li>JUnit Port </li></ul></ul><ul><ul><li>Very Rich Feature Set </li></ul></ul><ul><ul><ul><li>Mock Objects </li></ul></ul></ul><ul><ul><ul><li>Selenium Integration </li></ul></ul></ul><ul><ul><ul><li>Database Testing </li></ul></ul></ul><ul><ul><ul><li>Much Much More </li></ul></ul></ul><ul><ul><li>Alternatives </li></ul></ul><ul><ul><ul><li>SimpleTest </li></ul></ul></ul><ul><ul><ul><li>PHPT </li></ul></ul></ul><ul><ul><ul><li>test-more.php </li></ul></ul></ul><ul><ul><ul><li>lime </li></ul></ul></ul>
  17. 17. PHPUnit - Creating a Test Case <?php require_once( 'PHPUnit/Framework/TestCase.php' ); class  SubStrTest  extends  PHPUnit_Framework_TestCase {     public function  testSubstr ()     {          $this -> assertEquals ( 'o wo' ,  substr ( 'Hello world!' ,  4 ,  4 ));     } } ?>
  18. 18. PHPUnit - Fixtures <ul><ul><li>Each test needs an established, predictable environment. </li></ul></ul><ul><ul><li>Tests should run completely independent of the results of previous tests. </li></ul></ul><ul><ul><li>setUp() - Common setup required for each test in the test case. </li></ul></ul><ul><ul><li>tearDown() - Hide your tracks! </li></ul></ul>
  19. 19. PHPUnit - Call Order PHPUnit_Framework_TestCase::setUp(); PHPUnit_Framework_TestCase::testMyCode1(); PHPUnit_Framework_TestCase::tearDown(); PHPUnit_Framework_TestCase::setUp(); PHPUnit_Framework_TestCase::testMyCode2(); PHPUnit_Framework_TestCase::tearDown();
  20. 20. PHPUnit - Fixtures <?php class  BankAccountTest  extends  PHPUnit_Framework_TestCase {     protected  $ba ;     protected function  setUp ()     {          $this -> ba  = new  BankAccount ;     }     public function  testBalanceIsInitiallyZero ()     {          $this -> assertEquals ( 0 ,  $this -> ba -> getBalance ());     }     protected function  tearDown ()     {          $this -> ba  =  NULL ;     } } ?>
  21. 21. PHPUnit - Testing Code <ul><ul><li>The basis of unit testing is making assertions regarding the expected results of a function. </li></ul></ul><ul><ul><li>assertEquals(), assertSame(), assertLessThan(), assertContains(), the list goes on and on and on and on ... </li></ul></ul>
  22. 22. PHPUnit - Testing Code <?php $this -> assertEquals ( 'o wo' ,  substr ( 'hello world' ,  4 ,  4 )); $this -> assertGreaterThan ( 3 ,  4 ); $this -> assertTrue ( TRUE ); $this -> assertNULL ( NULL ); $this -> assertContains ( 'value1' , array( 'value1' ,  'value2' ,  'value3' )); $this -> assertFileExists ( '/tmp/testfile' ); $this -> assertType ( 'int' ,  20 ); $this -> assertRegExp ( '/d{3}-d{2}-d{4}/' ,  '123-45-6789' ); ?>
  23. 23. PHPUnit - Testing Code <ul><ul><li>The basis of unit testing is making assertions regarding the expected results of a function. </li></ul></ul><ul><ul><li>assertEquals(), assertSame(), assertLessThan(), assertContains(), the list goes on and on and on and on ... </li></ul></ul><ul><ul><li>A significantly fuller list: http://www.phpunit.de/pocket_guide/3.2/en/api.html#api.assert </li></ul></ul><ul><ul><li>Testing for Failure is also important: setExpectedException() </li></ul></ul>
  24. 24. PHPUnit - Testing Code <?php require_once  'PHPUnit/Framework.php' ;   class  ExceptionTest  extends  PHPUnit_Framework_TestCase {     public function  testException ()     {          $this -> setExpectedException ( 'Exception' ,  'exception message' ,  100 );     } } ?>
  25. 25. PHPUnit - Running a Test Usage: phpunit [switches] UnitTest [UnitTest.php]     --log-graphviz <file>  Log test execution in GraphViz markup.   --log-json <file>      Log test execution in JSON format.   --log-tap <file>       Log test execution in TAP format to file.   --log-xml <file>       Log test execution in XML format to file.   --coverage-html <dir>  Generate code coverage report in HTML format.   --coverage-xml <file>  Write code coverage information in XML format.   --test-db-dsn <dsn>    DSN for the test database.   --test-db-log-rev <r>  Revision information for database logging.   --test-db-prefix ...   Prefix that should be stripped from filenames.   --test-db-log-info ... Additional information for database logging.   --filter <pattern>     Filter which tests to run.   --group ...            Only runs tests from the specified group(s).   --exclude-group ...    Exclude tests from the specified group(s).   --loader <loader>      TestSuiteLoader implementation to use.   --repeat <times>       Runs the test(s) repeatedly.   --tap                  Report test execution progress in TAP format.   --testdox              Report test execution progress in TestDox format.
  26. 26. PHPUnit - Running a Test Usage: phpunit [switches] UnitTest [UnitTest.php]     --no-syntax-check      Disable syntax check of test source files.   --stop-on-failure      Stop execution upon first error or failure.   --verbose              Output more verbose information.   --wait                 Waits for a keystroke after each test.   --skeleton             Generate skeleton UnitTest class for Unit in Unit.php.   --help                 Prints this usage information.   --version              Prints the version and exits.   --configuration <file> Read configuration from XML file.   -d key[=value]         Sets a php.ini value.
  27. 27. PHPUnit - Test Suites <ul><ul><li>When you start amassing tests you need a way to organize them </li></ul></ul><ul><ul><li>Test Suites </li></ul></ul><ul><ul><ul><li>Allow you to group test cases together so you can run one, all or a specific area's tests </li></ul></ul></ul><ul><ul><ul><li>Effective but somewhat cumbersome to set up </li></ul></ul></ul>
  28. 28. PHPUnit - Test Suites <?php if (! defined ( 'PHPUnit_MAIN_METHOD' ))  define ( 'PHPUnit_MAIN_METHOD' ,  'AllTests::main' ); require_once  'PHPUnit/Framework.php' ; require_once  'PHPUnit/TextUI/TestRunner.php' ; require_once  'MyTest.php' ; require_once  'MyOtherTest.php' ; class  AllTests {     public static function  main ()     {          PHPUnit_TextUI_TestRunner :: run ( self :: suite ());     }     public static function  suite ()     {          $suite  = new  PHPUnit_Framework_TestSuite ( 'My Test Suite' );         $suite -> addTestSuite ( 'MyTest' );          $suite -> addTestSuite ( 'MyOtherTest' );         return  $suite ;     } } if ( PHPUnit_MAIN_METHOD  ==  'AllTests::main' ) AllTests :: main (); ?>
  29. 29. PHPUnit - Test Suites <ul><ul><li>Using the xml config </li></ul></ul><ul><ul><ul><li>You can specify a directory and a file mask </li></ul></ul></ul><ul><ul><ul><li>Test Case class names must follow specific format </li></ul></ul></ul><ul><ul><ul><li>Groups can still be set up (we'll talk about that soon) </li></ul></ul></ul><ul><ul><ul><li>Not as flexible as test suites </li></ul></ul></ul><ul><ul><ul><li>Incredibly simple to add new tests: Just drop the test in the appropriate directory </li></ul></ul></ul>
  30. 30. PHPUnit - XML Configuration Running a list of tests <phpunit>   <testsuite name=&quot;My Test Suite&quot;>     <directory suffix=&quot;Test.php&quot;>       mytests/directory     </directory>     <file>       mytests/dirtory/fileTest.php     </file>   </testsuite> </phpunit>
  31. 31. PHPUnit - XML Configuration <ul><ul><li>So, how can I group tests at will when using XML to set up the test suite? </li></ul></ul><ul><ul><li>@group test annotation </li></ul></ul><ul><ul><li>--group, --exclude-group command line options </li></ul></ul>
  32. 32. PHPUnit - XML Configuration <?php require_once( 'PHPUnit/Framework/TestCase.php' ); class  SubStrTest  extends  PHPUnit_Framework_TestCase {      /**      * @group builtins      */      public function  testSubstr ()     {          $this -> assertEquals ( 'o wo' ,  substr ( 'Hello world!' ,  4 ,  4 ));     } } ?> >phpunit --configuration tests.xml --group builtins
  33. 33. PHPUnit - Annotations <ul><ul><li>Annotations can be an incredible time saver! </li></ul></ul><ul><ul><li>@test - Marks a particular method in a test case as a test </li></ul></ul><ul><ul><li>@dataProvider - Allow running the same tests using different data. </li></ul></ul><ul><ul><li>@expectedException - a convenient shortcut for setExpectedException </li></ul></ul><ul><ul><li>@assert - Helps in automatically generating tests </li></ul></ul>
  34. 34. PHPUnit - Annotations <ul><ul><li>@test - Marks a particular method in a test case as a test </li></ul></ul><ul><ul><li><?php require_once( 'PHPUnit/Framework/TestCase.php' ); class  SubStrTestAnnote  extends  PHPUnit_Framework_TestCase {      /**      * @test      */      public function  subStr1 ()     {          $this -> assertEquals ( 'o wo' ,  substr ( 'Hello world!' ,  4 ,  4 ));     } } ?> </li></ul></ul>
  35. 35. PHPUnit - Annotations <ul><ul><li>@dataProvider - Allow running the same tests using different data. </li></ul></ul><ul><ul><li><?php class  DataTest  extends  PHPUnit_Framework_TestCase {     public static function  provider ()     {         return array(           array( 0 ,  0 ,  0 ),           array( 0 ,  1 ,  1 ),           array( 1 ,  1 ,  3 )         );     }     /**      * @dataProvider provider      */      public function  testAdd ( $a ,  $b ,  $c )     {          $this -> assertEquals ( $c ,  $a  +  $b );     } } ?> </li></ul></ul>
  36. 36. PHPUnit - Annotations <ul><ul><li>@expectedException - a convenient shortcut for setExpectedException </li></ul></ul><ul><ul><li><?php require_once  'PHPUnit/Framework.php' ;   class  ExceptionTest  extends  PHPUnit_Framework_TestCase {      /**      * @expectedException Exception      */      public function  testException ()     {     } } ?> </li></ul></ul>
  37. 37. PHPUnit - Skeleton Generator <ul><ul><li>When creating simple tests this can be incredibly useful. </li></ul></ul><ul><ul><li><?php class  Calculator {     public function  add ( $a ,  $b )     {         return  $a  +  $b ;     } } ?> </li></ul></ul><ul><ul><li>> phpunit --skeleton Calculator </li></ul></ul><ul><ul><li>PHPUnit 3.2.19 by Sebastian Bergmann. Wrote test class skeleton for &quot;Calculator&quot; to &quot;CalculatorTest.php&quot;. </li></ul></ul>
  38. 38. PHPUnit - Skeleton Generator <ul><ul><li>Result?     /**      * @todo Implement testAdd().      */      public function  testAdd () {          // Remove the following lines when you implement this test.          $this -> markTestIncomplete (            'This test has not been implemented yet.'          );     } </li></ul></ul>
  39. 39. PHPUnit - Skeleton Generator > phpunit --verbose CalculatorTest PHPUnit 3.2.19 by Sebastian Bergmann. CalculatorTest I Time: 0 seconds There was 1 incomplete test: 1) testAdd(CalculatorTest) This test has not been implemented yet. /home/mike/phpunit/CalculatorTest.php:65 OK, but incomplete or skipped tests! Tests: 1, Incomplete: 1.
  40. 40. PHPUnit - Skeleton Generator <ul><ul><li>Save some more time! </li></ul></ul><ul><ul><li><?php class  Calculator {      /**      * @assert (0, 0) == 0      * @assert (1, 2) == 3      * @assert (2, 5) == 7      * @assert (3, 3) == 6      * @assert (4, 8) == 12      */      public function  add ( $a ,  $b )     {         return  $a  +  $b ;     } } ?> </li></ul></ul>
  41. 41. PHPUnit - Skeleton Generator <ul><ul><li>Result </li></ul></ul><ul><ul><li>   /**      * Generated from @assert (0, 0) == 0.      */      public function  testAdd ()     {          $this -> assertEquals (            0 ,            $this -> object -> add ( 0 ,  0 )         );     }      /**      * Generated from @assert (1, 2) == 3.      */      public function  testAdd2 ()     {          $this -> assertEquals (            3 ,            $this -> object -> add ( 1 ,  2 )         );     }     // ... </li></ul></ul>
  42. 42. PHPUnit - Skeleton Generator > phpunit --verbose CalculatorTest PHPUnit 3.2.19 by Sebastian Bergmann. CalculatorTest ..... Time: 0 seconds OK (5 tests)
  43. 43. PHPUnit - Skeleton Generator @assert (...) == X     > assertEquals(X, method(...)) @assert (...) != X     > assertNotEquals(X, method(...)) @assert (...) === X    > assertSame(X, method(...)) @assert (...) !== X    > assertNotSame(X, method(...)) @assert (...) > X      > assertGreaterThan(X, method(...)) @assert (...) >= X     > assertGreaterThanOrEqual(X, method(...)) @assert (...) < X      > assertLessThan(X, method(...)) @assert (...) <= X     > assertLessThanOrEqual(X, method(...)) @assert (...) throws X > @expectedException X
  44. 44. PHPUnit - Incomplete Tests TODO: finish slide
  45. 45. PHPUnit - Incomplete Tests Slide TODO: finish slide don't worry...just a little irony
  46. 46. PHPUnit - Incomplete Tests <?php require_once  'PHPUnit/Framework.php' ;   class  SampleTest  extends  PHPUnit_Framework_TestCase {     public function  testSomething ()     {          // Optional: Test anything here, if you want.          $this -> assertTrue ( TRUE ,  'This should already work.' );          // Stop here and mark this test as incomplete.          $this -> markTestIncomplete (            'This test has not been implemented yet.'          );     } } ?>
  47. 47. PHPUnit - SkippedTests <?php require_once  'PHPUnit/Framework.php' ;   class  DatabaseTest  extends  PHPUnit_Framework_TestCase {     protected function  setUp ()     {         if (! extension_loaded ( 'mysqli' )) {              $this -> markTestSkipped (                'The MySQLi extension is not available.'              );         }     } } ?>
  48. 48. Advanced Features Mock Objects Database Testing Selenium RC PHPUnderControl Come see me again! Same room, same day, 3:30 PM
  49. 49. Thanks http://phpun.it http://planet.phpunit.de http://digitalsandwich.com http://dev.sellingsource.com Questions???
  1. A particular slide catching your eye?

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

×