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

PHPUnit & Continuous Integration: An Introduction

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