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

More Related Content

PPTX
Unit Testing And Mocking
PDF
Introduction to Unit Testing with PHPUnit
PDF
Mocking in Java with Mockito
PPT
Phpunit testing
PDF
Best node js course
PPT
Unit Test
PPTX
Build RESTful API Using Express JS
PDF
NodeJS for Beginner
Unit Testing And Mocking
Introduction to Unit Testing with PHPUnit
Mocking in Java with Mockito
Phpunit testing
Best node js course
Unit Test
Build RESTful API Using Express JS
NodeJS for Beginner

What's hot (20)

PPTX
NodeJS guide for beginners
PPTX
Clean Code
PDF
Java 8 Lambda Expressions
PDF
How to implement internationalization (i18n) in angular application(multiple ...
PPTX
Web forms in ASP.net
PDF
PPSX
Advanced Web Development in PHP - Understanding REST API
PPTX
Automation - web testing with selenium
PPTX
Testing Spring Boot application in post-JUnit 4 world
PDF
Java 17
PPTX
JavaScript Promises
PPTX
Top 10 RxJs Operators in Angular
PDF
Don't Be Mocked by your Mocks - Best Practices using Mocks
PPTX
Angular 2.0 Dependency injection
PPTX
Clean Pragmatic Architecture - Avoiding a Monolith
PDF
The New JavaScript: ES6
PDF
Flask Introduction - Python Meetup
PPTX
Exception handling in c++
PPTX
Data and time
PDF
Coroutines for Kotlin Multiplatform in Practise
NodeJS guide for beginners
Clean Code
Java 8 Lambda Expressions
How to implement internationalization (i18n) in angular application(multiple ...
Web forms in ASP.net
Advanced Web Development in PHP - Understanding REST API
Automation - web testing with selenium
Testing Spring Boot application in post-JUnit 4 world
Java 17
JavaScript Promises
Top 10 RxJs Operators in Angular
Don't Be Mocked by your Mocks - Best Practices using Mocks
Angular 2.0 Dependency injection
Clean Pragmatic Architecture - Avoiding a Monolith
The New JavaScript: ES6
Flask Introduction - Python Meetup
Exception handling in c++
Data and time
Coroutines for Kotlin Multiplatform in Practise
Ad

Viewers also liked (15)

PPT
Test Driven Development with PHPUnit
PPTX
PHPUnit - Unit testing
PDF
Testes de Performance na Nuvem | TDC2014
ODP
PHPUnit e teste de software
PDF
ODP
IPC 2013 - High Performance PHP with HipHop
PPTX
Automated php unit testing in drupal 8
PDF
PHP Unit y TDD
PPTX
PHPUnit with CakePHP and Yii
PPTX
PHPUnit: from zero to hero
PDF
Automated Testing in WordPress, Really?!
PDF
PhpUnit - The most unknown Parts
PPTX
Unit Testing in PHP
PPT
Unit Testing using PHPUnit
PPT
Advanced PHPUnit Testing
Test Driven Development with PHPUnit
PHPUnit - Unit testing
Testes de Performance na Nuvem | TDC2014
PHPUnit e teste de software
IPC 2013 - High Performance PHP with HipHop
Automated php unit testing in drupal 8
PHP Unit y TDD
PHPUnit with CakePHP and Yii
PHPUnit: from zero to hero
Automated Testing in WordPress, Really?!
PhpUnit - The most unknown Parts
Unit Testing in PHP
Unit Testing using PHPUnit
Advanced PHPUnit Testing
Ad

Similar to PHPUnit (20)

PDF
Leveling Up With Unit Testing - LonghornPHP 2022
ZIP
Test
PPTX
Unit Testng with PHP Unit - A Step by Step Training
PPT
Unit testing
PDF
Leveling Up With Unit Testing - php[tek] 2023
PPT
Automated Unit Testing
PDF
Unit testing in PHP
PDF
Fighting Fear-Driven-Development With PHPUnit
KEY
Developer testing 101: Become a Testing Fanatic
PDF
Cursus phpunit
PPT
Unit testing php-unit - phing - selenium_v2
PDF
PhpUnit Best Practices
PPTX
Unit testing
PDF
PHPUnit with Magento
PDF
Php unit (eng)
KEY
Php Unit With Zend Framework Zendcon09
PDF
Your code are my tests
PDF
Unit and integration Testing
KEY
Developer testing 201: When to Mock and When to Integrate
PDF
Test Automation
Leveling Up With Unit Testing - LonghornPHP 2022
Test
Unit Testng with PHP Unit - A Step by Step Training
Unit testing
Leveling Up With Unit Testing - php[tek] 2023
Automated Unit Testing
Unit testing in PHP
Fighting Fear-Driven-Development With PHPUnit
Developer testing 101: Become a Testing Fanatic
Cursus phpunit
Unit testing php-unit - phing - selenium_v2
PhpUnit Best Practices
Unit testing
PHPUnit with Magento
Php unit (eng)
Php Unit With Zend Framework Zendcon09
Your code are my tests
Unit and integration Testing
Developer testing 201: When to Mock and When to Integrate
Test Automation

Recently uploaded (20)

PDF
System and Network Administration Chapter 2
PDF
AI in Product Development-omnex systems
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
medical staffing services at VALiNTRY
PPTX
Transform Your Business with a Software ERP System
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Essential Infomation Tech presentation.pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
System and Network Administration Chapter 2
AI in Product Development-omnex systems
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
medical staffing services at VALiNTRY
Transform Your Business with a Software ERP System
Upgrade and Innovation Strategies for SAP ERP Customers
How to Migrate SBCGlobal Email to Yahoo Easily
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Essential Infomation Tech presentation.pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
Design an Analysis of Algorithms I-SECS-1021-03
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Wondershare Filmora 15 Crack With Activation Key [2025

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