SlideShare a Scribd company logo
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

More Related Content

What's hot (6)

PDF
The DNA of OPNFV
Frank Brockners
 
PDF
2011 JavaOne Fun with EJB 3.1 and OpenEJB
David Blevins
 
PDF
Scaling with Postgres
elliando dias
 
PDF
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Yasuharu Nakano
 
PDF
Railsconf 2010
John Woodell
 
PPTX
Java build tools
Sujit Kumar
 
The DNA of OPNFV
Frank Brockners
 
2011 JavaOne Fun with EJB 3.1 and OpenEJB
David Blevins
 
Scaling with Postgres
elliando dias
 
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Yasuharu Nakano
 
Railsconf 2010
John Woodell
 
Java build tools
Sujit Kumar
 

Similar to PHPUnit & Continuous Integration: An Introduction (20)

ZIP
Test
Eddie Kao
 
PDF
Unit and integration Testing
David Berliner
 
PDF
Unit testing and scaffolding
Valerio Maggio
 
PPT
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
PDF
Agile Java Testing With Open Source Frameworks
Viraf Karai
 
PDF
Unit testing (eng)
Anatoliy Okhotnikov
 
PDF
How To Make Your Testing More Groovy
Craig Smith
 
PDF
Introduction to Automated Testing
Lars Thorup
 
PDF
Introduction to-automated-testing
BestBrains
 
PPT
Automated testing overview
Alex Pop
 
KEY
Developer testing 101: Become a Testing Fanatic
LB Denker
 
PPTX
Test in action – week 1
Yi-Huan Chan
 
PDF
Test Driven Development
ZendCon
 
PDF
Unit Testing Basics
Serhiy Oplakanets
 
PDF
Using The Page Object Pattern
Dante Briones
 
PDF
Cursus phpunit
Nick Belhomme
 
PPTX
Integration and Unit Testing in Java using Test Doubles like mocks and stubs
Rody Middelkoop
 
KEY
Test-Driven Development for TYPO3
Oliver Klee
 
PDF
Testing In Agile
Ari Tanninen
 
PDF
Systematic Unit Testing
scotchfield
 
Test
Eddie Kao
 
Unit and integration Testing
David Berliner
 
Unit testing and scaffolding
Valerio Maggio
 
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
Agile Java Testing With Open Source Frameworks
Viraf Karai
 
Unit testing (eng)
Anatoliy Okhotnikov
 
How To Make Your Testing More Groovy
Craig Smith
 
Introduction to Automated Testing
Lars Thorup
 
Introduction to-automated-testing
BestBrains
 
Automated testing overview
Alex Pop
 
Developer testing 101: Become a Testing Fanatic
LB Denker
 
Test in action – week 1
Yi-Huan Chan
 
Test Driven Development
ZendCon
 
Unit Testing Basics
Serhiy Oplakanets
 
Using The Page Object Pattern
Dante Briones
 
Cursus phpunit
Nick Belhomme
 
Integration and Unit Testing in Java using Test Doubles like mocks and stubs
Rody Middelkoop
 
Test-Driven Development for TYPO3
Oliver Klee
 
Testing In Agile
Ari Tanninen
 
Systematic Unit Testing
scotchfield
 
Ad

Recently uploaded (20)

PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
July Patch Tuesday
Ivanti
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
July Patch Tuesday
Ivanti
 
Ad

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 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
  • 4. 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
  • 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 Can CI 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