SlideShare ist ein Scribd-Unternehmen logo
1 von 49
Automated Unit Testing Getting Started The 2008 DC PHP Conference June 2nd, 2008
Hello! ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Automated Unit Testing ,[object Object],[object Object],[object Object],[object Object],[object Object]
Automated Unit Testing ,[object Object],[object Object],[object Object],[object Object],[object Object]
What is Unit Testing ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Automated Unit Testing ,[object Object],[object Object],[object Object],[object Object],[object Object]
Why should I unit test? ,[object Object],[object Object],[object Object]
Why should I unit test? ,[object Object],[object Object],[object Object],[object Object],[object Object]
Automated Unit Testing ,[object Object],[object Object],[object Object],[object Object],[object Object]
What Should I Test? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Automated Unit Testing ,[object Object],[object Object],[object Object],[object Object],[object Object]
When do I Test? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
When do I Test? ,[object Object],[object Object],[object Object],[object Object]
Automated Unit Testing ,[object Object],[object Object],[object Object],[object Object],[object Object]
How do I Test ,[object Object],[object Object],[object Object],[object Object]
PHPUnit ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 ));     } } ?>
PHPUnit - Fixtures ,[object Object],[object Object],[object Object],[object Object]
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();
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 ;     } } ?>
PHPUnit - Testing Code ,[object Object],[object Object]
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 ( '/{3}-{2}-{4}/' ,  '123-45-6789' ); ?>
PHPUnit - Testing Code ,[object Object],[object Object],[object Object],[object Object]
PHPUnit - Testing Code <?php require_once  'PHPUnit/Framework.php' ;   class  ExceptionTest  extends  PHPUnit_Framework_TestCase {     public function  testException ()     {          $this -> setExpectedException ( 'Exception' ,  'exception message' ,  100 );     } } ?>
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.
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.
PHPUnit - Test Suites ,[object Object],[object Object],[object Object],[object Object]
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 (); ?>
PHPUnit - Test Suites ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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>
PHPUnit - XML Configuration ,[object Object],[object Object],[object Object]
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
PHPUnit - Annotations ,[object Object],[object Object],[object Object],[object Object],[object Object]
PHPUnit - Annotations ,[object Object],[object Object]
PHPUnit - Annotations ,[object Object],[object Object]
PHPUnit - Annotations ,[object Object],[object Object]
PHPUnit - Skeleton Generator ,[object Object],[object Object],[object Object],[object Object]
PHPUnit - Skeleton Generator ,[object Object]
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.
PHPUnit - Skeleton Generator ,[object Object],[object Object]
PHPUnit - Skeleton Generator ,[object Object],[object Object]
PHPUnit - Skeleton Generator > phpunit --verbose CalculatorTest PHPUnit 3.2.19 by Sebastian Bergmann. CalculatorTest ..... Time: 0 seconds OK (5 tests)
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
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_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.'          );     } } ?>
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.'              );         }     } } ?>
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???

Weitere ähnliche Inhalte

Was ist angesagt?

Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Jacinto Limjap
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage Introduction
Alex Su
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
Introduction To J unit
Introduction To J unitIntroduction To J unit
Introduction To J unit
Olga Extone
 

Was ist angesagt? (20)

Unit test
Unit testUnit test
Unit test
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practices
 
Unit test
Unit testUnit test
Unit test
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
Unit testing with Junit
Unit testing with JunitUnit testing with Junit
Unit testing with Junit
 
Unit testing
Unit testingUnit testing
Unit testing
 
Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1
 
J Unit
J UnitJ Unit
J Unit
 
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage Introduction
 
Embrace Unit Testing
Embrace Unit TestingEmbrace Unit Testing
Embrace Unit Testing
 
Tdd & unit test
Tdd & unit testTdd & unit test
Tdd & unit test
 
Unit testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
 
Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
 
New Features Of Test Unit 2.x
New Features Of Test Unit 2.xNew Features Of Test Unit 2.x
New Features Of Test Unit 2.x
 
Introduction To J unit
Introduction To J unitIntroduction To J unit
Introduction To J unit
 
Junit
JunitJunit
Junit
 

Andere mochten auch

Real time web applications
Real time web applicationsReal time web applications
Real time web applications
Jess Chadwick
 
Continuous Automated Testing - Cast conference workshop august 2014
Continuous Automated Testing - Cast conference workshop august 2014Continuous Automated Testing - Cast conference workshop august 2014
Continuous Automated Testing - Cast conference workshop august 2014
Noah Sussman
 

Andere mochten auch (19)

Automated testing 101
Automated testing 101Automated testing 101
Automated testing 101
 
Real time web applications
Real time web applicationsReal time web applications
Real time web applications
 
What you can do with WordPress Heartbeat API
What you can do with WordPress Heartbeat APIWhat you can do with WordPress Heartbeat API
What you can do with WordPress Heartbeat API
 
Automated Testing & Auto Scaling your Apps with Microsoft & Open Source Techn...
Automated Testing & Auto Scaling your Apps with Microsoft & Open Source Techn...Automated Testing & Auto Scaling your Apps with Microsoft & Open Source Techn...
Automated Testing & Auto Scaling your Apps with Microsoft & Open Source Techn...
 
Advanced automated visual testing with Selenium
Advanced automated visual testing with SeleniumAdvanced automated visual testing with Selenium
Advanced automated visual testing with Selenium
 
Selenium Based Visual Test Automation
Selenium Based Visual Test AutomationSelenium Based Visual Test Automation
Selenium Based Visual Test Automation
 
Testing as a container
Testing as a containerTesting as a container
Testing as a container
 
How to level-up your Selenium tests with Visual Testing #SeleniumCamp
How to level-up your Selenium tests with Visual Testing #SeleniumCampHow to level-up your Selenium tests with Visual Testing #SeleniumCamp
How to level-up your Selenium tests with Visual Testing #SeleniumCamp
 
Automated Testing Environment by Bugzilla, Testopia and Jenkins
Automated Testing Environment by Bugzilla, Testopia and JenkinsAutomated Testing Environment by Bugzilla, Testopia and Jenkins
Automated Testing Environment by Bugzilla, Testopia and Jenkins
 
SeConf2015: Advanced Automated Visual Testing With Selenium
SeConf2015: Advanced Automated Visual Testing With SeleniumSeConf2015: Advanced Automated Visual Testing With Selenium
SeConf2015: Advanced Automated Visual Testing With Selenium
 
Continuous Automated Testing - Cast conference workshop august 2014
Continuous Automated Testing - Cast conference workshop august 2014Continuous Automated Testing - Cast conference workshop august 2014
Continuous Automated Testing - Cast conference workshop august 2014
 
Automated Testing with Agile
Automated Testing with AgileAutomated Testing with Agile
Automated Testing with Agile
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnit
 
Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++
 
Selenium RC: Automated Testing of Modern Web Applications
Selenium RC: Automated Testing of Modern Web ApplicationsSelenium RC: Automated Testing of Modern Web Applications
Selenium RC: Automated Testing of Modern Web Applications
 
Automated Regression Testing for Embedded Systems in Action
Automated Regression Testing for Embedded Systems in ActionAutomated Regression Testing for Embedded Systems in Action
Automated Regression Testing for Embedded Systems in Action
 
Agile Testing Framework - The Art of Automated Testing
Agile Testing Framework - The Art of Automated TestingAgile Testing Framework - The Art of Automated Testing
Agile Testing Framework - The Art of Automated Testing
 
BMW Case Study Analysis
BMW Case Study AnalysisBMW Case Study Analysis
BMW Case Study Analysis
 
Automated Testing
Automated TestingAutomated Testing
Automated Testing
 

Ähnlich wie Automated Unit Testing

Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
varuntaliyan
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
Yi-Huan Chan
 

Ähnlich wie Automated Unit Testing (20)

Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnit
 
Unit testing
Unit testingUnit testing
Unit testing
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentation
 
Unit testing for WordPress
Unit testing for WordPressUnit testing for WordPress
Unit testing for WordPress
 
Code igniter unittest-part1
Code igniter unittest-part1Code igniter unittest-part1
Code igniter unittest-part1
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
 
Unit Testing in PHP
Unit Testing in PHPUnit Testing in PHP
Unit Testing in PHP
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And Drupal
 
PHPUnit
PHPUnitPHPUnit
PHPUnit
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
 
PHPUnit with CakePHP and Yii
PHPUnit with CakePHP and YiiPHPUnit with CakePHP and Yii
PHPUnit with CakePHP and Yii
 
Unit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step TrainingUnit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step Training
 
Phpunit
PhpunitPhpunit
Phpunit
 
Test
TestTest
Test
 
Php tests tips
Php tests tipsPhp tests tips
Php tests tips
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to hero
 
Unit testing
Unit testingUnit testing
Unit testing
 
Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12
 
2010 07-28-testing-zf-apps
2010 07-28-testing-zf-apps2010 07-28-testing-zf-apps
2010 07-28-testing-zf-apps
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 

Automated Unit Testing

  • 1. Automated Unit Testing Getting Started The 2008 DC PHP Conference June 2nd, 2008
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 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.
  • 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. 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.
  • 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 ( '/{3}-{2}-{4}/' ,  '123-45-6789' ); ?>
  • 23.
  • 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. 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. 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.
  • 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.
  • 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.
  • 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.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 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.
  • 41.
  • 42. PHPUnit - Skeleton Generator > phpunit --verbose CalculatorTest PHPUnit 3.2.19 by Sebastian Bergmann. CalculatorTest ..... Time: 0 seconds OK (5 tests)
  • 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. PHPUnit - Incomplete Tests TODO: finish slide
  • 45. PHPUnit - Incomplete Tests Slide TODO: finish slide don't worry...just a little irony
  • 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. 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. Advanced Features Mock Objects Database Testing Selenium RC PHPUnderControl Come see me again! Same room, same day, 3:30 PM
  • 49. Thanks http://phpun.it http://planet.phpunit.de http://digitalsandwich.com http://dev.sellingsource.com Questions???