SlideShare a Scribd company logo
PhpUnit
Testing Php one unit at a time!
Who am I?
I am Chris Ryan.
Lead Developer for the DWS Recreation group.
Over 15 years of software development experience.
Experience with Web, Desktop, Mobile platforms using different languages.
Fixed some really bad code.
Written some really bad code!
Thrown fits after someone broke something I had already fixed three times.
What is PhpUnit
PhpUnit is a tool for writing and running unit test for Php.
Written by Sebastian Bergman a co-founder of thePHP.cc and a pioneer in the
field of quality assurance in PHP projects.
PhpUnit is a toolset and framework to help you write unit test for your Php
Code. Unit tests should allow you to make fewer mistakes.
PhpUnit’s goals are Easy to learn to write, Easy to write, Easy to read, Easy to
execute, Quick to execute, Isolated.
http://manual.phpunit.de/
Why PhpUnit
Why do unit testing?
Unit testing is one part of test driven development. Even if you are not doing
test driven development unit testing still helps increase the overall quality of
code and reduce the overall error count by testing for expected behaviour.
Why use PhpUnit?
For Php it is a common tool that is well documented and understood by the
industry. It also has a number of extensions available for using with other
testing tools.
Installing PhpUnit
There are a number of different methods to get PhpUnit. Many of these are
documented in PhpUnit’s documentation. Some of the available methods are:
Download
wget https://phar.phpunit.de/phpunit.phar
php phpunit.phar
Pear
pear config-set auto_discover 1
pear install pear.phpunit.de/PHPUnit
Composer
{
"require-dev": {
"phpunit/phpunit": "3.7.*"
}
}
Running PhpUnit
Basic:
phpunit MyTest SomeTests.php
or
phpunit TestDir
Better:
phpunit --bootstrap=TestDir/bootstrap.php --strict TestDir
Code Coverage:
phpunit --coverage-html ./report TestDir
Simplify your command by putting common options in phpunit.xml file. This
allows you to be more consistent with the options you run each time.
Basic Example
tests/DoMathTest.php:
<?php
final class DoMathTest extends PHPUnit_Framework_TestCase {
public function testAdd() {
$obj = new DoMath();
$this->assertEquals(3, $obj->add(1, 2));
}
}
Basic Example
Be Descriptive
final class DoMathTest extends PHPUnit_Framework_TestCase {
public function testAddIntegers() {
$obj = new DoMath();
$this->assertEquals(3, $obj->add(1, 2));
}
public function testAddFloats() {
$obj = new DoMath();
$this->assertEquals(1.3, $obj->add(0.5, 0.8));
}
}
Be Descriptive for --testdox
} Easier to read!
Say what you are testing
/**
* Test the math class
*
* @covers DoMath
*/
final class DoMathTest extends PHPUnit_Framework_TestCase {
/**
* @covers DoMath::add
*/
public function testAddIntegers() {
$obj = new DoMath();
$this->assertEquals(3, $obj->add(1, 2));
}
.
.
.
Use the most specific Assert
/**
* @covers DoMath::add
*/
public function testAddHandlesNull() {
$obj = new DoMath();
// Really Poor assertion
$this->assertEquals(false, is_null($obj->add(null, 1)));
// Poor assertion
$this->assertFalse(is_null($obj->add(null, 1)));
// Good assertion
$this->assertNotNull($obj->add(null, 1));
}
Actually use assertions
/**
* @coversNothing
*/
public function testNoAssertion() {
$obj = new DoMath();
$obj->add(1, 2);
}
How do we know this really worked?
Use --strict
Decouple test code & data
/**
* @dataProvider provider
* @covers DoMath::add
*/
public function testAddWithDataSet($a, $b, $c) {
$obj = new DoMath();
$this->assertEquals($c, $obj->add($a, $b));
}
public function provider() {
return array(
array(2, 3, 5),
array(3, -2, 1),
array(-2, -3, -5)
);
}
Putting it into practice
Unit tests are great but you must:
● Write unit tests…. start with one
● Run the unit tests… automation helps
● Fix code that breaks unit tests… broken code/tests are problems
● Keep adding tests… test driven development
Things to avoid:
● Avoid changing the unit tests and the code at the same time
● Avoid testing dependencies
● Avoid writing tests that do not actually tests anything
Legacy code
● Legacy code can be harder to write for
● It is just as important to have unit tests for legacy code
● Start with one easy test
● Add more tests
● Do not change code and write the test at the same time
It is HARD but is worth the effort!
References
I heavily referenced the following materials to create this presentation.
http://thephp.cc/dates/2012/webexpoprague/phpunit-best-practices
http://phpunit.de/manual/current/en/index.html
All the code from this presentation is in Github.com
https://github.com/chrisryan/phpunit-example
Questions?
View this presentation at http://bit.ly/1eYCfCO
Ad

More Related Content

What's hot (20)

Callback Function
Callback FunctionCallback Function
Callback Function
Roland San Nicolas
 
Phishing Attacks - Are You Ready to Respond?
Phishing Attacks - Are You Ready to Respond?Phishing Attacks - Are You Ready to Respond?
Phishing Attacks - Are You Ready to Respond?
Splunk
 
Web forms in ASP.net
Web forms in ASP.netWeb forms in ASP.net
Web forms in ASP.net
Madhuri Kavade
 
Laravel ppt
Laravel pptLaravel ppt
Laravel ppt
Mayank Panchal
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
kamal kotecha
 
Sessions and cookies
Sessions and cookiesSessions and cookies
Sessions and cookies
www.netgains.org
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
ShahDhruv21
 
API Test Automation
API Test Automation API Test Automation
API Test Automation
SQALab
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
humayunlkhan
 
Php variables
Php variablesPhp variables
Php variables
Ritwik Das
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
Vikash Singh
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
priya_trivedi
 
Writing Clean Code in Swift
Writing Clean Code in SwiftWriting Clean Code in Swift
Writing Clean Code in Swift
Derek Lee
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
Purbarun Chakrabarti
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
Derek Willian Stavis
 
An Introduction To REST API
An Introduction To REST APIAn Introduction To REST API
An Introduction To REST API
Aniruddh Bhilvare
 
Php
PhpPhp
Php
Ajaigururaj R
 
Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Ruby
Ranjith Siji
 
Solid principles
Solid principlesSolid principles
Solid principles
Monica Rodrigues
 
Php introduction
Php introductionPhp introduction
Php introduction
krishnapriya Tadepalli
 

Viewers also liked (15)

Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
Mindfire Solutions
 
PHPUnit - Unit testing
PHPUnit - Unit testingPHPUnit - Unit testing
PHPUnit - Unit testing
Nguyễn Đào Thiên Thư
 
Testes de Performance na Nuvem | TDC2014
Testes de Performance na Nuvem | TDC2014Testes de Performance na Nuvem | TDC2014
Testes de Performance na Nuvem | TDC2014
Júlio de Lima
 
PHPUnit e teste de software
PHPUnit e teste de softwarePHPUnit e teste de software
PHPUnit e teste de software
ricardophp
 
chapters
chapterschapters
chapters
Chandan Chaurasia
 
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
Steve Kamerman
 
Automated php unit testing in drupal 8
Automated php unit testing in drupal 8Automated php unit testing in drupal 8
Automated php unit testing in drupal 8
Jay Friendly
 
PHP Unit y TDD
PHP Unit y TDDPHP Unit y TDD
PHP Unit y TDD
Emergya
 
PHPUnit with CakePHP and Yii
PHPUnit with CakePHP and YiiPHPUnit with CakePHP and Yii
PHPUnit with CakePHP and Yii
madhavi Ghadge
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to hero
Jeremy Cook
 
Automated Testing in WordPress, Really?!
Automated Testing in WordPress, Really?!Automated Testing in WordPress, Really?!
Automated Testing in WordPress, Really?!
Ptah Dunbar
 
PhpUnit - The most unknown Parts
PhpUnit - The most unknown PartsPhpUnit - The most unknown Parts
PhpUnit - The most unknown Parts
Bastian Feder
 
Unit Testing in PHP
Unit Testing in PHPUnit Testing in PHP
Unit Testing in PHP
Radu Murzea
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
varuntaliyan
 
Advanced PHPUnit Testing
Advanced PHPUnit TestingAdvanced PHPUnit Testing
Advanced PHPUnit Testing
Mike Lively
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
Mindfire Solutions
 
Testes de Performance na Nuvem | TDC2014
Testes de Performance na Nuvem | TDC2014Testes de Performance na Nuvem | TDC2014
Testes de Performance na Nuvem | TDC2014
Júlio de Lima
 
PHPUnit e teste de software
PHPUnit e teste de softwarePHPUnit e teste de software
PHPUnit e teste de software
ricardophp
 
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
Steve Kamerman
 
Automated php unit testing in drupal 8
Automated php unit testing in drupal 8Automated php unit testing in drupal 8
Automated php unit testing in drupal 8
Jay Friendly
 
PHP Unit y TDD
PHP Unit y TDDPHP Unit y TDD
PHP Unit y TDD
Emergya
 
PHPUnit with CakePHP and Yii
PHPUnit with CakePHP and YiiPHPUnit with CakePHP and Yii
PHPUnit with CakePHP and Yii
madhavi Ghadge
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to hero
Jeremy Cook
 
Automated Testing in WordPress, Really?!
Automated Testing in WordPress, Really?!Automated Testing in WordPress, Really?!
Automated Testing in WordPress, Really?!
Ptah Dunbar
 
PhpUnit - The most unknown Parts
PhpUnit - The most unknown PartsPhpUnit - The most unknown Parts
PhpUnit - The most unknown Parts
Bastian Feder
 
Unit Testing in PHP
Unit Testing in PHPUnit Testing in PHP
Unit Testing in PHP
Radu Murzea
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
varuntaliyan
 
Advanced PHPUnit Testing
Advanced PHPUnit TestingAdvanced PHPUnit Testing
Advanced PHPUnit Testing
Mike Lively
 
Ad

Similar to PHPUnit (20)

Cursus phpunit
Cursus phpunitCursus phpunit
Cursus phpunit
Nick Belhomme
 
Unit testing for WordPress
Unit testing for WordPressUnit testing for WordPress
Unit testing for WordPress
Harshad Mane
 
Python and test
Python and testPython and test
Python and test
Micron Technology
 
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
Tricode (part of Dept)
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
Mike Lively
 
Zend Framework 2 - PHPUnit
Zend Framework 2 - PHPUnitZend Framework 2 - PHPUnit
Zend Framework 2 - PHPUnit
Tarun Kumar Singhal
 
Unit testing
Unit testingUnit testing
Unit testing
Arthur Purnama
 
Upstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testingUpstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testing
DanWooster1
 
PHPUnit with Magento
PHPUnit with MagentoPHPUnit with Magento
PHPUnit with Magento
Tu Hoang
 
Getting started with PHPUnit
Getting started with PHPUnitGetting started with PHPUnit
Getting started with PHPUnit
Khyati Gala
 
Php unit (eng)
Php unit (eng)Php unit (eng)
Php unit (eng)
Anatoliy Okhotnikov
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnit
James Fuller
 
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven PignataroJoomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Steven Pignataro
 
Test
TestTest
Test
Eddie Kao
 
Behavior & Specification Driven Development in PHP - #OpenWest
Behavior & Specification Driven Development in PHP - #OpenWestBehavior & Specification Driven Development in PHP - #OpenWest
Behavior & Specification Driven Development in PHP - #OpenWest
Joshua Warren
 
Put an end to regression with codeception testing
Put an end to regression with codeception testingPut an end to regression with codeception testing
Put an end to regression with codeception testing
Joe Ferguson
 
A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...
A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...
A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...
kalichargn70th171
 
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...
[ENGLISH] TDC 2015 - PHP  Trail - Tests and PHP Continuous Integration Enviro...[ENGLISH] TDC 2015 - PHP  Trail - Tests and PHP Continuous Integration Enviro...
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...
Bruno Tanoue
 
Back to basics - PHPUnit
Back to basics - PHPUnitBack to basics - PHPUnit
Back to basics - PHPUnit
Sebastian Marek
 
PresentationqwertyuiopasdfghUnittest.pdf
PresentationqwertyuiopasdfghUnittest.pdfPresentationqwertyuiopasdfghUnittest.pdf
PresentationqwertyuiopasdfghUnittest.pdf
kndemo34
 
Unit testing for WordPress
Unit testing for WordPressUnit testing for WordPress
Unit testing for WordPress
Harshad Mane
 
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
Tricode (part of Dept)
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
Mike Lively
 
Upstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testingUpstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testing
DanWooster1
 
PHPUnit with Magento
PHPUnit with MagentoPHPUnit with Magento
PHPUnit with Magento
Tu Hoang
 
Getting started with PHPUnit
Getting started with PHPUnitGetting started with PHPUnit
Getting started with PHPUnit
Khyati Gala
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnit
James Fuller
 
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven PignataroJoomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Steven Pignataro
 
Behavior & Specification Driven Development in PHP - #OpenWest
Behavior & Specification Driven Development in PHP - #OpenWestBehavior & Specification Driven Development in PHP - #OpenWest
Behavior & Specification Driven Development in PHP - #OpenWest
Joshua Warren
 
Put an end to regression with codeception testing
Put an end to regression with codeception testingPut an end to regression with codeception testing
Put an end to regression with codeception testing
Joe Ferguson
 
A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...
A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...
A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...
kalichargn70th171
 
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...
[ENGLISH] TDC 2015 - PHP  Trail - Tests and PHP Continuous Integration Enviro...[ENGLISH] TDC 2015 - PHP  Trail - Tests and PHP Continuous Integration Enviro...
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...
Bruno Tanoue
 
Back to basics - PHPUnit
Back to basics - PHPUnitBack to basics - PHPUnit
Back to basics - PHPUnit
Sebastian Marek
 
PresentationqwertyuiopasdfghUnittest.pdf
PresentationqwertyuiopasdfghUnittest.pdfPresentationqwertyuiopasdfghUnittest.pdf
PresentationqwertyuiopasdfghUnittest.pdf
kndemo34
 
Ad

Recently uploaded (20)

Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
How can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptxHow can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptx
laravinson24
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]
saniaaftab72555
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
How can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptxHow can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptx
laravinson24
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]
saniaaftab72555
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 

PHPUnit

  • 1. PhpUnit Testing Php one unit at a time!
  • 2. Who am I? I am Chris Ryan. Lead Developer for the DWS Recreation group. Over 15 years of software development experience. Experience with Web, Desktop, Mobile platforms using different languages. Fixed some really bad code. Written some really bad code! Thrown fits after someone broke something I had already fixed three times.
  • 3. What is PhpUnit PhpUnit is a tool for writing and running unit test for Php. Written by Sebastian Bergman a co-founder of thePHP.cc and a pioneer in the field of quality assurance in PHP projects. PhpUnit is a toolset and framework to help you write unit test for your Php Code. Unit tests should allow you to make fewer mistakes. PhpUnit’s goals are Easy to learn to write, Easy to write, Easy to read, Easy to execute, Quick to execute, Isolated. http://manual.phpunit.de/
  • 4. Why PhpUnit Why do unit testing? Unit testing is one part of test driven development. Even if you are not doing test driven development unit testing still helps increase the overall quality of code and reduce the overall error count by testing for expected behaviour. Why use PhpUnit? For Php it is a common tool that is well documented and understood by the industry. It also has a number of extensions available for using with other testing tools.
  • 5. Installing PhpUnit There are a number of different methods to get PhpUnit. Many of these are documented in PhpUnit’s documentation. Some of the available methods are: Download wget https://phar.phpunit.de/phpunit.phar php phpunit.phar Pear pear config-set auto_discover 1 pear install pear.phpunit.de/PHPUnit Composer { "require-dev": { "phpunit/phpunit": "3.7.*" } }
  • 6. Running PhpUnit Basic: phpunit MyTest SomeTests.php or phpunit TestDir Better: phpunit --bootstrap=TestDir/bootstrap.php --strict TestDir Code Coverage: phpunit --coverage-html ./report TestDir Simplify your command by putting common options in phpunit.xml file. This allows you to be more consistent with the options you run each time.
  • 7. Basic Example tests/DoMathTest.php: <?php final class DoMathTest extends PHPUnit_Framework_TestCase { public function testAdd() { $obj = new DoMath(); $this->assertEquals(3, $obj->add(1, 2)); } }
  • 9. Be Descriptive final class DoMathTest extends PHPUnit_Framework_TestCase { public function testAddIntegers() { $obj = new DoMath(); $this->assertEquals(3, $obj->add(1, 2)); } public function testAddFloats() { $obj = new DoMath(); $this->assertEquals(1.3, $obj->add(0.5, 0.8)); } }
  • 10. Be Descriptive for --testdox } Easier to read!
  • 11. Say what you are testing /** * Test the math class * * @covers DoMath */ final class DoMathTest extends PHPUnit_Framework_TestCase { /** * @covers DoMath::add */ public function testAddIntegers() { $obj = new DoMath(); $this->assertEquals(3, $obj->add(1, 2)); } . . .
  • 12. Use the most specific Assert /** * @covers DoMath::add */ public function testAddHandlesNull() { $obj = new DoMath(); // Really Poor assertion $this->assertEquals(false, is_null($obj->add(null, 1))); // Poor assertion $this->assertFalse(is_null($obj->add(null, 1))); // Good assertion $this->assertNotNull($obj->add(null, 1)); }
  • 13. Actually use assertions /** * @coversNothing */ public function testNoAssertion() { $obj = new DoMath(); $obj->add(1, 2); } How do we know this really worked?
  • 15. Decouple test code & data /** * @dataProvider provider * @covers DoMath::add */ public function testAddWithDataSet($a, $b, $c) { $obj = new DoMath(); $this->assertEquals($c, $obj->add($a, $b)); } public function provider() { return array( array(2, 3, 5), array(3, -2, 1), array(-2, -3, -5) ); }
  • 16. Putting it into practice Unit tests are great but you must: ● Write unit tests…. start with one ● Run the unit tests… automation helps ● Fix code that breaks unit tests… broken code/tests are problems ● Keep adding tests… test driven development Things to avoid: ● Avoid changing the unit tests and the code at the same time ● Avoid testing dependencies ● Avoid writing tests that do not actually tests anything
  • 17. Legacy code ● Legacy code can be harder to write for ● It is just as important to have unit tests for legacy code ● Start with one easy test ● Add more tests ● Do not change code and write the test at the same time It is HARD but is worth the effort!
  • 18. References I heavily referenced the following materials to create this presentation. http://thephp.cc/dates/2012/webexpoprague/phpunit-best-practices http://phpunit.de/manual/current/en/index.html All the code from this presentation is in Github.com https://github.com/chrisryan/phpunit-example
  • 19. Questions? View this presentation at http://bit.ly/1eYCfCO