SlideShare uma empresa Scribd logo
1 de 25
by Silviu Butnariu (sbutnariu@pentalog.fr)
What’s that?
 Unit testing = testing bunches of code, not the whole

application
 PhpUnit was created by Sebastian Bergamann
 It’s part of xUnit family – most used unit testers

 Used for automatic tests – makes the machine do the

work
What are the benefits
of unit testing?
 Uncover bugs easier – good tests go trough all possible

program paths
 Assure high code coverage – measurable code quality

indicator
 Faster than manual testing
 Suitable for teamwork
Installing PhpUnit
 With PEAR:

pear config-set auto_discover 1
pear install phpunit/PHPUnit
 With composer (dependency manager for php); just

add to composer.json:
{ "require-dev": { "phpunit/phpunit": "3.7.*" } }
and update the composer
Basic test
Test result notations
. – success
F – failure
E – error
I – incomplete
S - skipped
Assertions
 Helper functions that compare an expected result with
-

the actual one:
assertEquals()
assertFalse()
assertArrayHasKey()
assertInstanceOf()
assertNotNull()
assertRegExp()
etc.
Dependency injection
 DI is a software pattern that allows the removal of

hard-coded dependencies
 Highly coupled dependencies – bad
 Loose coupled dependencies - awesome
 Improves code readability, reusability
DI guidelines
 Don’t use ‘new’
 Pass dependencies as method parameters
 DI allows real unit testing, by separating modules
 Read the DI best practices 
Mocking objects
 Creating fake objects that act in a predefined and

predictable way
Stubbing methods
 In close relation to mocking
 Stubbing implies faking a method from the mock

object
Static methods
 Avoid static methods

Can’t mock
static calls to
outside classes!
Annotations
 These syntactic metadata are used for specifying
-

some special case behaviors
@covers
@test
@dataProvider
@expectedException
@group
etc.
Data Provider
 Allows to bunch up more test cases into one, thus

reducing code duplicity
Testing exceptions
setUp & tearDown
 setUp(), tearDown() – executed once for each test method
 setUpBeforeClass(), tearDownAfterClass() - called before the first

test of the test case and after the last test
DB testing
 Need extension: pear install phpunit/DbUnit
 Supported dbs: mysql, postgre, oracle, sqlite
 There are 4 stages of db testing:
- set up fixture
- exercise SUT
- verify outcome
- teardown
 Important methods:
- getConnection() - connection data (host,db,pass)
- getDataSet() – defines state of DB before each test
DB testing
 Once setting up the ‘fake’ db connection tests can be

executed to check queries

 The db changes that occur during the tests don’t persist
 Can verify number of rows after insertion, check query

results, etc.

 This is the safest way of fully testing an application

(versus mocking db connection)
Skeleton Generator
 Automatically generates test classes based on

assertions in the code
IDE integration
 Integrated in most ides: Eclipse, NetBeans,

PhpStorm, etc
Code coverage
 Generating statistics with naked PhpUnit
 phpunit -c app/ --coverage-html={foldername}
Code coverage
 FFFFFFF
Documentation
 http://phpunit.de/manual/3.7/en/
Q&A
THANKS !!!

Mais conteúdo relacionado

Mais procurados

Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...Timo Stollenwerk
 
Day1_Apache_JMeter_Overview
Day1_Apache_JMeter_OverviewDay1_Apache_JMeter_Overview
Day1_Apache_JMeter_OverviewSravanthi N
 
Unit testing for WordPress
Unit testing for WordPressUnit testing for WordPress
Unit testing for WordPressHarshad Mane
 
Python testing using mock and pytest
Python testing using mock and pytestPython testing using mock and pytest
Python testing using mock and pytestSuraj Deshmukh
 
Test Driven Development With Python
Test Driven Development With PythonTest Driven Development With Python
Test Driven Development With PythonSiddhi
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnitMindfire Solutions
 
Unit testing using jasmine in Javascript
Unit testing using jasmine in JavascriptUnit testing using jasmine in Javascript
Unit testing using jasmine in JavascriptDeepak More
 
Unit Testing in WordPress
Unit Testing in WordPressUnit Testing in WordPress
Unit Testing in WordPressBarry Kooij
 
Unit testing with NUnit
Unit testing with NUnitUnit testing with NUnit
Unit testing with NUnitkleinron
 
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)Jen Wong
 
NUnit Features Presentation
NUnit Features PresentationNUnit Features Presentation
NUnit Features PresentationShir Brass
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to heroJeremy Cook
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentationnicobn
 
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_v2Tricode (part of Dept)
 

Mais procurados (20)

Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...
 
PHPUnit
PHPUnitPHPUnit
PHPUnit
 
Python unittest
Python unittestPython unittest
Python unittest
 
Day1_Apache_JMeter_Overview
Day1_Apache_JMeter_OverviewDay1_Apache_JMeter_Overview
Day1_Apache_JMeter_Overview
 
Python Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit TestingPython Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit Testing
 
Python testing
Python  testingPython  testing
Python testing
 
Unit testing for WordPress
Unit testing for WordPressUnit testing for WordPress
Unit testing for WordPress
 
Python testing using mock and pytest
Python testing using mock and pytestPython testing using mock and pytest
Python testing using mock and pytest
 
Test Driven Development With Python
Test Driven Development With PythonTest Driven Development With Python
Test Driven Development With Python
 
Phpunit testing
Phpunit testingPhpunit testing
Phpunit testing
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
 
Unit testing using jasmine in Javascript
Unit testing using jasmine in JavascriptUnit testing using jasmine in Javascript
Unit testing using jasmine in Javascript
 
Unit Testing in WordPress
Unit Testing in WordPressUnit Testing in WordPress
Unit Testing in WordPress
 
Unit testing with NUnit
Unit testing with NUnitUnit testing with NUnit
Unit testing with NUnit
 
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
 
NUnit Features Presentation
NUnit Features PresentationNUnit Features Presentation
NUnit Features Presentation
 
N Unit Presentation
N Unit PresentationN Unit Presentation
N Unit Presentation
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to hero
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentation
 
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
 

Destaque

IPC 2013 - High Performance PHP with HipHop
IPC 2013 - High Performance PHP with HipHopIPC 2013 - High Performance PHP with HipHop
IPC 2013 - High Performance PHP with HipHopSteve Kamerman
 
About Me
About MeAbout Me
About Mekdxkid
 
Tango argentino
Tango argentinoTango argentino
Tango argentinosarapaol
 
Collaborative Filtering Survey
Collaborative Filtering SurveyCollaborative Filtering Survey
Collaborative Filtering Surveymobilizer1000
 
Composition
CompositionComposition
Compositionkdxkid
 
Weather17th 20th
Weather17th 20thWeather17th 20th
Weather17th 20thamykay16
 
Path To Prosperity Small To Midsize Companies
Path To Prosperity Small To Midsize CompaniesPath To Prosperity Small To Midsize Companies
Path To Prosperity Small To Midsize Companiesstephej2
 
Conditionals 100819134225-phpapp01
Conditionals 100819134225-phpapp01Conditionals 100819134225-phpapp01
Conditionals 100819134225-phpapp01wil_4158
 
Weather 1st 11th
Weather 1st 11thWeather 1st 11th
Weather 1st 11thamykay16
 
Web 1.0,2.0 y 3.0
Web 1.0,2.0 y 3.0Web 1.0,2.0 y 3.0
Web 1.0,2.0 y 3.0rocanela
 
Estado da Arte HTML5
Estado da Arte HTML5Estado da Arte HTML5
Estado da Arte HTML5MCM-IPG
 
Menkes bp dr.ricca
Menkes bp dr.riccaMenkes bp dr.ricca
Menkes bp dr.riccaSufya Anwar
 

Destaque (20)

chapters
chapterschapters
chapters
 
IPC 2013 - High Performance PHP with HipHop
IPC 2013 - High Performance PHP with HipHopIPC 2013 - High Performance PHP with HipHop
IPC 2013 - High Performance PHP with HipHop
 
Zendesk PRO Tips
Zendesk PRO TipsZendesk PRO Tips
Zendesk PRO Tips
 
About Me
About MeAbout Me
About Me
 
Tango argentino
Tango argentinoTango argentino
Tango argentino
 
Collaborative Filtering Survey
Collaborative Filtering SurveyCollaborative Filtering Survey
Collaborative Filtering Survey
 
Gami offshorent - executive overview
Gami   offshorent - executive overviewGami   offshorent - executive overview
Gami offshorent - executive overview
 
Minialbunatoyac
MinialbunatoyacMinialbunatoyac
Minialbunatoyac
 
Composition
CompositionComposition
Composition
 
Weather17th 20th
Weather17th 20thWeather17th 20th
Weather17th 20th
 
Path To Prosperity Small To Midsize Companies
Path To Prosperity Small To Midsize CompaniesPath To Prosperity Small To Midsize Companies
Path To Prosperity Small To Midsize Companies
 
Conditionals 100819134225-phpapp01
Conditionals 100819134225-phpapp01Conditionals 100819134225-phpapp01
Conditionals 100819134225-phpapp01
 
Weather 1st 11th
Weather 1st 11thWeather 1st 11th
Weather 1st 11th
 
Web 1.0,2.0 y 3.0
Web 1.0,2.0 y 3.0Web 1.0,2.0 y 3.0
Web 1.0,2.0 y 3.0
 
Twitter
TwitterTwitter
Twitter
 
Estado da Arte HTML5
Estado da Arte HTML5Estado da Arte HTML5
Estado da Arte HTML5
 
Menkes bp dr.ricca
Menkes bp dr.riccaMenkes bp dr.ricca
Menkes bp dr.ricca
 
Stop motion
Stop motionStop motion
Stop motion
 
Steaua bucuresti
Steaua bucurestiSteaua bucuresti
Steaua bucuresti
 
Lecturi
LecturiLecturi
Lecturi
 

Semelhante a Unit Testing in PHP

Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit TestingMike Lively
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitJames Fuller
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnitvaruntaliyan
 
PHPUnit with CakePHP and Yii
PHPUnit with CakePHP and YiiPHPUnit with CakePHP and Yii
PHPUnit with CakePHP and Yiimadhavi Ghadge
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2Yi-Huan Chan
 
Leveling Up With Unit Testing - php[tek] 2023
Leveling Up With Unit Testing - php[tek] 2023Leveling Up With Unit Testing - php[tek] 2023
Leveling Up With Unit Testing - php[tek] 2023Mark Niebergall
 
Getting started with PHPUnit
Getting started with PHPUnitGetting started with PHPUnit
Getting started with PHPUnitKhyati Gala
 
Unit Testing from Setup to Deployment
Unit Testing from Setup to DeploymentUnit Testing from Setup to Deployment
Unit Testing from Setup to DeploymentMark Niebergall
 
J unit presentation
J unit presentationJ unit presentation
J unit presentationPriya Sharma
 
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 tek12Michelangelo van Dam
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestSeb Rose
 

Semelhante a Unit Testing in PHP (20)

Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnit
 
Zend Framework 2 - PHPUnit
Zend Framework 2 - PHPUnitZend Framework 2 - PHPUnit
Zend Framework 2 - PHPUnit
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
 
Unit testing
Unit testingUnit testing
Unit testing
 
PHPUnit with CakePHP and Yii
PHPUnit with CakePHP and YiiPHPUnit with CakePHP and Yii
PHPUnit with CakePHP and Yii
 
Php unit (eng)
Php unit (eng)Php unit (eng)
Php unit (eng)
 
Automated tests to a REST API
Automated tests to a REST APIAutomated tests to a REST API
Automated tests to a REST API
 
Junit_.pptx
Junit_.pptxJunit_.pptx
Junit_.pptx
 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
 
Leveling Up With Unit Testing - php[tek] 2023
Leveling Up With Unit Testing - php[tek] 2023Leveling Up With Unit Testing - php[tek] 2023
Leveling Up With Unit Testing - php[tek] 2023
 
Getting started with PHPUnit
Getting started with PHPUnitGetting started with PHPUnit
Getting started with PHPUnit
 
Unit Testing from Setup to Deployment
Unit Testing from Setup to DeploymentUnit Testing from Setup to Deployment
Unit Testing from Setup to Deployment
 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
Unit testing
Unit testingUnit testing
Unit testing
 
Python unit testing
Python unit testingPython unit testing
Python 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
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 

Mais de Radu Murzea

The World of StackOverflow
The World of StackOverflowThe World of StackOverflow
The World of StackOverflowRadu Murzea
 
SymfonyCon 2015 - A symphony of developers
SymfonyCon 2015 - A symphony of developersSymfonyCon 2015 - A symphony of developers
SymfonyCon 2015 - A symphony of developersRadu Murzea
 
The World of PHP PSR Standards
The World of PHP PSR StandardsThe World of PHP PSR Standards
The World of PHP PSR StandardsRadu Murzea
 
PHP 7 - A look at the future
PHP 7 - A look at the futurePHP 7 - A look at the future
PHP 7 - A look at the futureRadu Murzea
 
Hack programming language
Hack programming languageHack programming language
Hack programming languageRadu Murzea
 
Hack Programming Language
Hack Programming LanguageHack Programming Language
Hack Programming LanguageRadu Murzea
 
HipHop Virtual Machine
HipHop Virtual MachineHipHop Virtual Machine
HipHop Virtual MachineRadu Murzea
 

Mais de Radu Murzea (7)

The World of StackOverflow
The World of StackOverflowThe World of StackOverflow
The World of StackOverflow
 
SymfonyCon 2015 - A symphony of developers
SymfonyCon 2015 - A symphony of developersSymfonyCon 2015 - A symphony of developers
SymfonyCon 2015 - A symphony of developers
 
The World of PHP PSR Standards
The World of PHP PSR StandardsThe World of PHP PSR Standards
The World of PHP PSR Standards
 
PHP 7 - A look at the future
PHP 7 - A look at the futurePHP 7 - A look at the future
PHP 7 - A look at the future
 
Hack programming language
Hack programming languageHack programming language
Hack programming language
 
Hack Programming Language
Hack Programming LanguageHack Programming Language
Hack Programming Language
 
HipHop Virtual Machine
HipHop Virtual MachineHipHop Virtual Machine
HipHop Virtual Machine
 

Último

Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 

Último (20)

Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 

Unit Testing in PHP

  • 1. by Silviu Butnariu (sbutnariu@pentalog.fr)
  • 2. What’s that?  Unit testing = testing bunches of code, not the whole application  PhpUnit was created by Sebastian Bergamann  It’s part of xUnit family – most used unit testers  Used for automatic tests – makes the machine do the work
  • 3. What are the benefits of unit testing?  Uncover bugs easier – good tests go trough all possible program paths  Assure high code coverage – measurable code quality indicator  Faster than manual testing  Suitable for teamwork
  • 4. Installing PhpUnit  With PEAR: pear config-set auto_discover 1 pear install phpunit/PHPUnit  With composer (dependency manager for php); just add to composer.json: { "require-dev": { "phpunit/phpunit": "3.7.*" } } and update the composer
  • 6. Test result notations . – success F – failure E – error I – incomplete S - skipped
  • 7. Assertions  Helper functions that compare an expected result with - the actual one: assertEquals() assertFalse() assertArrayHasKey() assertInstanceOf() assertNotNull() assertRegExp() etc.
  • 8. Dependency injection  DI is a software pattern that allows the removal of hard-coded dependencies  Highly coupled dependencies – bad  Loose coupled dependencies - awesome  Improves code readability, reusability
  • 9. DI guidelines  Don’t use ‘new’  Pass dependencies as method parameters  DI allows real unit testing, by separating modules  Read the DI best practices 
  • 10. Mocking objects  Creating fake objects that act in a predefined and predictable way
  • 11. Stubbing methods  In close relation to mocking  Stubbing implies faking a method from the mock object
  • 12. Static methods  Avoid static methods Can’t mock static calls to outside classes!
  • 13. Annotations  These syntactic metadata are used for specifying - some special case behaviors @covers @test @dataProvider @expectedException @group etc.
  • 14. Data Provider  Allows to bunch up more test cases into one, thus reducing code duplicity
  • 16. setUp & tearDown  setUp(), tearDown() – executed once for each test method  setUpBeforeClass(), tearDownAfterClass() - called before the first test of the test case and after the last test
  • 17. DB testing  Need extension: pear install phpunit/DbUnit  Supported dbs: mysql, postgre, oracle, sqlite  There are 4 stages of db testing: - set up fixture - exercise SUT - verify outcome - teardown  Important methods: - getConnection() - connection data (host,db,pass) - getDataSet() – defines state of DB before each test
  • 18. DB testing  Once setting up the ‘fake’ db connection tests can be executed to check queries  The db changes that occur during the tests don’t persist  Can verify number of rows after insertion, check query results, etc.  This is the safest way of fully testing an application (versus mocking db connection)
  • 19. Skeleton Generator  Automatically generates test classes based on assertions in the code
  • 20. IDE integration  Integrated in most ides: Eclipse, NetBeans, PhpStorm, etc
  • 21. Code coverage  Generating statistics with naked PhpUnit  phpunit -c app/ --coverage-html={foldername}
  • 24. Q&A