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

2011 JavaOne Fun with EJB 3.1 and OpenEJB
2011 JavaOne Fun with EJB 3.1 and OpenEJB2011 JavaOne Fun with EJB 3.1 and OpenEJB
2011 JavaOne Fun with EJB 3.1 and OpenEJBDavid Blevins
 
Scaling with Postgres
Scaling with PostgresScaling with Postgres
Scaling with Postgreselliando dias
 
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx PluginGr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx PluginYasuharu Nakano
 
Java build tools
Java build toolsJava build tools
Java build toolsSujit Kumar
 

What's hot (6)

The DNA of OPNFV
The DNA of OPNFVThe DNA of OPNFV
The DNA of OPNFV
 
2011 JavaOne Fun with EJB 3.1 and OpenEJB
2011 JavaOne Fun with EJB 3.1 and OpenEJB2011 JavaOne Fun with EJB 3.1 and OpenEJB
2011 JavaOne Fun with EJB 3.1 and OpenEJB
 
Scaling with Postgres
Scaling with PostgresScaling with Postgres
Scaling with Postgres
 
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx PluginGr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
 
Railsconf 2010
Railsconf 2010Railsconf 2010
Railsconf 2010
 
Java build tools
Java build toolsJava build tools
Java build tools
 

Similar to PHPUnit & Continuous Integration: An Introduction

How to stuff a 900 pound gorilla into a smartphone
How to stuff a 900 pound gorilla into a smartphoneHow to stuff a 900 pound gorilla into a smartphone
How to stuff a 900 pound gorilla into a smartphoneJoel Maher
 
Continuous Deployment at Disqus (Pylons Minicon)
Continuous Deployment at Disqus (Pylons Minicon)Continuous Deployment at Disqus (Pylons Minicon)
Continuous Deployment at Disqus (Pylons Minicon)zeeg
 
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...TEST Huddle
 
Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...
Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...
Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...TEST Huddle
 
Using The Page Object Pattern
Using The Page Object PatternUsing The Page Object Pattern
Using The Page Object PatternDante Briones
 
Java EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan Gallimore
Java EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan GallimoreJava EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan Gallimore
Java EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan GallimoreJAX London
 
Are Your Tests Really Helping You?
Are Your Tests Really Helping You?Are Your Tests Really Helping You?
Are Your Tests Really Helping You?LB Denker
 
Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!Ivan Ivanov
 
Qt test framework
Qt test frameworkQt test framework
Qt test frameworkICS
 
Practical (J)Unit Testing (2009)
Practical (J)Unit Testing (2009)Practical (J)Unit Testing (2009)
Practical (J)Unit Testing (2009)Peter Kofler
 
Agile Software Testing the Agilogy Way
Agile Software Testing the Agilogy WayAgile Software Testing the Agilogy Way
Agile Software Testing the Agilogy WayJordi Pradel
 
OSGi Applications Testing - André Elia Assad, System Engineer, Cesar
OSGi Applications Testing - André Elia Assad, System Engineer, CesarOSGi Applications Testing - André Elia Assad, System Engineer, Cesar
OSGi Applications Testing - André Elia Assad, System Engineer, Cesarmfrancis
 
Introduction To Continuous Integration
Introduction To Continuous IntegrationIntroduction To Continuous Integration
Introduction To Continuous IntegrationChristopher Read
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Javaguy_davis
 
Enterprise Strength Mobile JavaScript
Enterprise Strength Mobile JavaScriptEnterprise Strength Mobile JavaScript
Enterprise Strength Mobile JavaScriptTroy Miles
 
Testing – With Mock Objects
Testing – With Mock ObjectsTesting – With Mock Objects
Testing – With Mock Objectsemmettwalsh
 

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

How to stuff a 900 pound gorilla into a smartphone
How to stuff a 900 pound gorilla into a smartphoneHow to stuff a 900 pound gorilla into a smartphone
How to stuff a 900 pound gorilla into a smartphone
 
Continuous Deployment at Disqus (Pylons Minicon)
Continuous Deployment at Disqus (Pylons Minicon)Continuous Deployment at Disqus (Pylons Minicon)
Continuous Deployment at Disqus (Pylons Minicon)
 
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...
 
Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...
Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...
Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...
 
Using The Page Object Pattern
Using The Page Object PatternUsing The Page Object Pattern
Using The Page Object Pattern
 
Java EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan Gallimore
Java EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan GallimoreJava EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan Gallimore
Java EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan Gallimore
 
Are Your Tests Really Helping You?
Are Your Tests Really Helping You?Are Your Tests Really Helping You?
Are Your Tests Really Helping You?
 
Testing Angular
Testing AngularTesting Angular
Testing Angular
 
Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!
 
Qt test framework
Qt test frameworkQt test framework
Qt test framework
 
Practical (J)Unit Testing (2009)
Practical (J)Unit Testing (2009)Practical (J)Unit Testing (2009)
Practical (J)Unit Testing (2009)
 
Agile Software Testing the Agilogy Way
Agile Software Testing the Agilogy WayAgile Software Testing the Agilogy Way
Agile Software Testing the Agilogy Way
 
OSGi Applications Testing - André Elia Assad, System Engineer, Cesar
OSGi Applications Testing - André Elia Assad, System Engineer, CesarOSGi Applications Testing - André Elia Assad, System Engineer, Cesar
OSGi Applications Testing - André Elia Assad, System Engineer, Cesar
 
Cpp unit
Cpp unit Cpp unit
Cpp unit
 
Introduction To Continuous Integration
Introduction To Continuous IntegrationIntroduction To Continuous Integration
Introduction To Continuous Integration
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
Enterprise Strength Mobile JavaScript
Enterprise Strength Mobile JavaScriptEnterprise Strength Mobile JavaScript
Enterprise Strength Mobile JavaScript
 
Junit
JunitJunit
Junit
 
TestNGvsJUnit
TestNGvsJUnitTestNGvsJUnit
TestNGvsJUnit
 
Testing – With Mock Objects
Testing – With Mock ObjectsTesting – With Mock Objects
Testing – With Mock Objects
 

Recently uploaded

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
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 Processorsdebabhi2
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
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...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
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
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
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...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

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