PHPUnit &  Continuous Integration Continuous Integration <ul><li>An Introduction </li></ul>
PHPUnit & CI <ul><li>What is Unit Testing? </li></ul><ul><li>Why Unit Test? </li></ul><ul><li>How to test </li></ul><ul><l...
What is Unit Testing? <ul><li>In computer programming, Unit Testing is a software verification and validation method in wh...
Why not Unit Test? <ul><li>Writing tests is boring! </li></ul><ul><li>Writing tests makes development take longer </li></u...
Why Unit Test? <ul><li>You can’t prove that a program is bug free </li></ul><ul><li>Reduces the cost of changes; proves so...
How To Test <ul><li>Tests should be repeatable, audited and isolated.  </li></ul><ul><li>Repeatable: easy to run multiple ...
Why PHPUnit? <ul><li>Makes it easy to run tests in a repeatable, audited and isolated fashion. </li></ul><ul><li>Complete ...
Assertions <ul><li>assertArrayHasKey </li></ul><ul><li>assertClassHasAttribute </li></ul><ul><li>assertContains </li></ul>...
<ul><li>Test case classes have a “Test” suffix </li></ul><ul><li>Test methods are prefixed “test” </li></ul>A Basic Example
Running Tests <ul><li>Tests can be run via: </li></ul><ul><ul><li>Command Line </li></ul></ul><ul><ul><li>IDE </li></ul></...
Fixtures <ul><li>Each test may require common operations to set up the environment </li></ul><ul><li>Environment should be...
Fixture Example <ul><li>Database connection and test object instantiation are now done in setUp( ) </li></ul><ul><li>Datab...
Test Organisation <ul><li>PHPUnit allows your to organise your tests into “Suites” </li></ul><ul><li>Suites can include ot...
Advanced Usage <ul><li>Mock Objects & Stubs </li></ul><ul><li>Database Testing </li></ul><ul><li>Skeleton Generation </li>...
Mock Objects & Stubs <ul><li>Mock objects are used as stand ins for genuine objects </li></ul><ul><li>Stubs are replacemen...
Database Testing <ul><li>Put database into a known state before test is run </li></ul><ul><li>Using assertDataSetsEqual( )...
Skeleton Generation <ul><li>PHPUnit is able to generate a class skeleton based on a test case </li></ul><ul><li>PHPUnit is...
Selenium <ul><li>Selenium performs end user testing of web applications on any platform in any browser </li></ul><ul><li>A...
Code Coverage <ul><li>Requires Xdebug so PHPUnit can inspect what code is running </li></ul><ul><li>Tells you how much of ...
Best Practices <ul><li>Write testable code </li></ul><ul><ul><li>No globals </li></ul></ul><ul><ul><li>Don’t create object...
Automating Unit Tests <ul><li>Create a cron job to run tests </li></ul><ul><li>Subversion pre-commit hook </li></ul><ul><l...
Continuous Integration <ul><li>Can automate the running of unit tests </li></ul><ul><li>More developers = more divergence ...
What Else Can CI Do? <ul><li>Generate Documentation </li></ul><ul><li>Check Code Against Coding Standards </li></ul><ul><l...
Continuous Integration for PHP <ul><li>Atlassian Bamboo </li></ul><ul><li>CruiseControl </li></ul><ul><li>phpUnderControl ...
phpUnderControl <ul><li>Available through PEAR on phpunit channel </li></ul><ul><li>Uses Subversion, PHPUnit, Xdebug, PHP_...
Summary <ul><li>Unit Testing: What it is and why we do it </li></ul><ul><li>PHPUnit: How to Unit Test </li></ul><ul><li>Co...
More Information <ul><li>http://www.phpunit.de </li></ul><ul><li>http://phpundercontrol.org / </li></ul><ul><li>Just ask! ...
Upcoming SlideShare
Loading in …5
×

PHPUnit & Continuous Integration: An Introduction

4,523 views

Published on

Published in: Technology, Education

PHPUnit & Continuous Integration: An Introduction

  1. 1. PHPUnit & Continuous Integration Continuous Integration <ul><li>An Introduction </li></ul>
  2. 2. PHPUnit & CI <ul><li>What is Unit Testing? </li></ul><ul><li>Why Unit Test? </li></ul><ul><li>How to test </li></ul><ul><li>Using PHPUnit </li></ul><ul><li>Organising Tests </li></ul><ul><li>Advanced PHPUnit </li></ul><ul><li>Best Practices </li></ul><ul><li>Automated Test Runs </li></ul><ul><li>Continuous Integration </li></ul><ul><li>More features of CI </li></ul>
  3. 3. What is Unit Testing? <ul><li>In computer programming, Unit Testing is a software verification and validation method in which a programmer tests if individual units of source code are fit for use. (Wikipedia) </li></ul><ul><li>A “unit” is the smallest testable part of an application: a function or class method. </li></ul>
  4. 4. Why not Unit Test? <ul><li>Writing tests is boring! </li></ul><ul><li>Writing tests makes development take longer </li></ul><ul><li>My code is perfect and has no bugs </li></ul>
  5. 5. Why Unit Test? <ul><li>You can’t prove that a program is bug free </li></ul><ul><li>Reduces the cost of changes; proves software still works as expected after changes have been made. </li></ul><ul><li>Takes pressure off </li></ul>
  6. 6. How To Test <ul><li>Tests should be repeatable, audited and isolated. </li></ul><ul><li>Repeatable: easy to run multiple times </li></ul><ul><li>Audited: results should be collated and displayed </li></ul><ul><li>Isolated: one test should not affect other tests and should only test one thing </li></ul>
  7. 7. Why PHPUnit? <ul><li>Makes it easy to run tests in a repeatable, audited and isolated fashion. </li></ul><ul><li>Complete port of JUnit 3.8 </li></ul><ul><li>Support integrated into Eclipse, NetBeans & other IDEs </li></ul><ul><li>Integrates with Continuous Integration tools </li></ul><ul><li>Advanced features like: Mock Objects, Database testing, class skeleton generation, Selenium </li></ul><ul><li>Alternative: SimpleTest </li></ul>
  8. 8. Assertions <ul><li>assertArrayHasKey </li></ul><ul><li>assertClassHasAttribute </li></ul><ul><li>assertContains </li></ul><ul><li>assertFalse </li></ul><ul><li>assertFileEquals </li></ul><ul><li>assertFileExists </li></ul><ul><li>assertGreaterThan </li></ul><ul><li>assertLessThan </li></ul><ul><li>assertNotNull </li></ul><ul><li>assertRegExp </li></ul><ul><li>assertSame </li></ul><ul><li>and many more! </li></ul>
  9. 9. <ul><li>Test case classes have a “Test” suffix </li></ul><ul><li>Test methods are prefixed “test” </li></ul>A Basic Example
  10. 10. Running Tests <ul><li>Tests can be run via: </li></ul><ul><ul><li>Command Line </li></ul></ul><ul><ul><li>IDE </li></ul></ul><ul><ul><li>Continuous Integration </li></ul></ul>
  11. 11. Fixtures <ul><li>Each test may require common operations to set up the environment </li></ul><ul><li>Environment should be returned to previous state after the test </li></ul><ul><li>Duplicating code is bad </li></ul><ul><li>Solution: setUp( ) and tearDown( ) </li></ul>
  12. 12. Fixture Example <ul><li>Database connection and test object instantiation are now done in setUp( ) </li></ul><ul><li>Database connection is closed in tearDown( ) </li></ul><ul><li>Connection reuse could pollute tests! </li></ul>
  13. 13. Test Organisation <ul><li>PHPUnit allows your to organise your tests into “Suites” </li></ul><ul><li>Suites can include other suites </li></ul><ul><li>Allows you to test everything in one go </li></ul>
  14. 14. Advanced Usage <ul><li>Mock Objects & Stubs </li></ul><ul><li>Database Testing </li></ul><ul><li>Skeleton Generation </li></ul><ul><li>Selenium </li></ul><ul><li>Code Coverage </li></ul>
  15. 15. Mock Objects & Stubs <ul><li>Mock objects are used as stand ins for genuine objects </li></ul><ul><li>Stubs are replacements for real functions that accept the same input but return a known quantity </li></ul><ul><li>Tests should not be dependant on code not being tested, so we use mock objects and stubs to replace them </li></ul>
  16. 16. Database Testing <ul><li>Put database into a known state before test is run </li></ul><ul><li>Using assertDataSetsEqual( ), the contents of a database tested </li></ul><ul><li>Data sets can be filtered to remove columns such as date, time </li></ul><ul><li>Testing multiple DBs is hard, but possible </li></ul>
  17. 17. Skeleton Generation <ul><li>PHPUnit is able to generate a class skeleton based on a test case </li></ul><ul><li>PHPUnit is also able to do the reverse, generate a test case skeleton based on a class </li></ul>
  18. 18. Selenium <ul><li>Selenium performs end user testing of web applications on any platform in any browser </li></ul><ul><li>Allows you to test that a web application is working as expected </li></ul><ul><li>Technically this is not unit testing, but acceptance testing </li></ul>
  19. 19. Code Coverage <ul><li>Requires Xdebug so PHPUnit can inspect what code is running </li></ul><ul><li>Tells you how much of your code is being tested </li></ul><ul><li>Helps to identify areas in need of testing or new tests that need of testing </li></ul>
  20. 20. Best Practices <ul><li>Write testable code </li></ul><ul><ul><li>No globals </li></ul></ul><ul><ul><li>Don’t create objects in constructors </li></ul></ul><ul><li>Don’t test private methods. Unit testing tests the interface, not the implementation. </li></ul><ul><li>Exploit dependencies between tests with @depends </li></ul>
  21. 21. Automating Unit Tests <ul><li>Create a cron job to run tests </li></ul><ul><li>Subversion pre-commit hook </li></ul><ul><li>Subversion post-commit hook </li></ul>
  22. 22. Continuous Integration <ul><li>Can automate the running of unit tests </li></ul><ul><li>More developers = more divergence </li></ul><ul><li>The longer you go between integrating work, the harder it is </li></ul><ul><li>Continuous Integration constantly integrates work </li></ul><ul><li>Problems are exposed quickly </li></ul>
  23. 23. What Else Can CI Do? <ul><li>Generate Documentation </li></ul><ul><li>Check Code Against Coding Standards </li></ul><ul><li>Generate Metrics </li></ul><ul><ul><li>Code Coverage </li></ul></ul><ul><ul><li>Project Mess Detection </li></ul></ul>
  24. 24. Continuous Integration for PHP <ul><li>Atlassian Bamboo </li></ul><ul><li>CruiseControl </li></ul><ul><li>phpUnderControl </li></ul>
  25. 25. phpUnderControl <ul><li>Available through PEAR on phpunit channel </li></ul><ul><li>Uses Subversion, PHPUnit, Xdebug, PHP_CodeSniffer, PHPDocumentor </li></ul><ul><li>build.xml defines what phpUnderControl will do in the build process </li></ul>
  26. 26. Summary <ul><li>Unit Testing: What it is and why we do it </li></ul><ul><li>PHPUnit: How to Unit Test </li></ul><ul><li>Continuous Integration: How it helps us create high quality code </li></ul>
  27. 27. More Information <ul><li>http://www.phpunit.de </li></ul><ul><li>http://phpundercontrol.org / </li></ul><ul><li>Just ask! </li></ul>

×