SlideShare a Scribd company logo
A Pragmatic Introduction
to (PHP) Unit Testing
Vienna PHP, March 2015
Peter Kofler, ‘Code Cop’
@codecopkofler
www.code-cop.org
Copyright Peter Kofler, licensed under CC-BY.
Peter Kofler
• Ph.D. (Appl. Math.)
• Professional Software
Developer for 15 years
• “fanatic about code quality”
• I help development teams
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Training
on the
Job?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Yes, some but...
●
only what is already there
●
Trial & Error not popular in production
●
no practice - only production
●
time pressure
I help development teams with
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
●
Professionalism
●
Quality and
Productivity
●
Continuous
Improvement
Mentoring
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
●
Pair Programming
●
Programming
Workshops
●
Deliberate
Practice, e.g.
Coding Dojos
Developing Quality
Software Developers
Agenda
●
Unit Testing
●
PHPUnit
(Coding)
●
Break
●
PHPUnit
(Coding)
●
Retrospective
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Quick Poll
●
Who has heard of PHPUnit? (xUnit)
●
Who has never written a unit test?
●
Who has written
a few?
●
Who writes unit
tests every day?
●
Who does TDD?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
We Make Mistakes
●
at least I do... 
●
number of bugs proportional LoC
– 7 bugs per 1.000 LoC
– 1 bug per 10.000 LoC in critical software
●
Assume you have lots of bugs.
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
TESTING
I FIND YOUR LACK OF TESTS DISTURBING.
What is a
Unit Test?
Unit Test (Micro Test)
●
code written by a developer
●
tests an individual unit
●
isolate each part
●
shows that the individual part is correct
●
sort of living documentation
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Unit of Work
●
single logical functional use case
●
invoked by some public interface
– a single method,
– a whole class or
– multiple classes
●
with one single logical purpose
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
What is it
made of?
Test Class
●
unit test tests the functionality of a unit
●
for various inputs and outputs
●
usually a single test (class/script)
class FooTest extends
PHPUnit_Framework_TestCase
{ … }
contains all test cases for Foo
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Test Methods
●
a test case tests the response of a single
method to a particular set of inputs
●
multiple test cases for a single method
●
test methods should be short, simple
●
tests without test methods are pointless
(public) function testMethod() {…}
/** @test */ function anyName() {…}
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Only one aspect/feature
●
tests work best with lots of small tests
●
tests only fail because of one reason
●
more than 1 assert in test
split into multiple tests→
●
1 test case – 1 method – 1 assertion
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
testHighAndLowInput()
testHighInput()
testLowInput()
Assertions
●
no output from your tests!
●
check expectations programmatically
●
e.g. $this->assertEquals,
$this->assertTrue, …
●
test method without assert is pointless
●
one test method - one assertion
●
test runner reports failure on each test
 Regression Testing
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Fixtures
●
sets up data needed to run tests
●
functions setUp(), tearDown()or
/** @before */, /** @after */
●
Test data is more likely to be wrong
than the tested code!
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
What should
we test?
The Right BICEP
●
Right: Are the results right?
●
B: All the boundary conditions correct?
●
I: Can we check inverse relationships?
●
C: Can we cross-check results
using other means?
●
E: Can we force error conditions?
●
P: Are performance characteristics
within bounds?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
“I write unit tests for one
reason: so my co-workers
don't f*** up my code.”
(David Angry)
A Unit Test
should ...
Focus on Behaviour

e.g. requirements are behaviour

names from (problem) domain

full sentences
(long, descriptive test method names)

expected behaviour should

separation per business module
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Consist of 3 Simple Steps
●
Prepare Input – Arrange
●
Call Method – Act
●
Check Output – Assert
●
Use in form of Given-When-Then
●
No conditional logic ( more test cases)→
●
No loops ( parametrized test)→
/** @dataProvider <static function> */
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Be F.I.R.S.T
●
Fast
●
Isolated (data, sequence)
●
Repeatable
●
Self-verifying
●
Timely
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Test Code Quality
must be equal
Prod. Code Quality
Try PHPUnit yourself
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Coding Dojo Mindset
●
Safe place outside
work
●
We are here to learn
●
Need to slow down
●
Focus on doing it right
●
Collaborative Game
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Assignment
●
Find a pair.
●
Get https://bitbucket.org/pkofler/phpunit-koans
●
Run phpunit – should see no tests
●
Go through the test code
●
assertions commented/ incomplete
●
uncomment the assertions
●
and complete them making tests pass
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
→Practice
Closing Circle
●
What did you learn today?
●
What surprised you today?
●
What will you do
differently in the
future?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Peter Kofler
@codecopkofler
www.code-cop.org
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
CC Images
●
green http://www.flickr.com/photos/43442082@N00/296362882/
●
Hamster http://www.flickr.com/photos/zebrapares/4529836138
●
Bruce http://www.flickr.com/photos/sherpas428/4350620602/
●
pairing http://www.flickr.com/photos/dav/94735395/
●
agenda http://www.flickr.com/photos/24293932@N00/2752221871/
●
hands https://www.flickr.com/photos/ninahiironniemi/497993647/
●
Dojo http://www.flickr.com/photos/49715404@N00/3267627038/
●
wants you http://www.flickr.com/photos/shutter/105497713/
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY

More Related Content

PDF
Pragmatic Introduction to Python Unit Testing (PyDays 2018)
PDF
TDD with Ruby
PDF
Writing Tests with the Unity Test Framework
PDF
Performance profiling and testing of symfony application 2
PPTX
Quality assurance in the early stages of the product
PPT
Testing ppt
PPTX
White box & Black box testing
Pragmatic Introduction to Python Unit Testing (PyDays 2018)
TDD with Ruby
Writing Tests with the Unity Test Framework
Performance profiling and testing of symfony application 2
Quality assurance in the early stages of the product
Testing ppt
White box & Black box testing

What's hot (20)

PDF
TDD - Designing with Expectations, not Implementations
ODP
Software Testing - Day Two
ODP
Beyond Unit Testing
PPTX
PPTX
Pair programming and introduction to TDD
PPT
Testing, black ,white and gray box testing
PDF
Hands-on Experience Model based testing with spec explorer
PPTX
White box testing
PPTX
Unit testing
PPTX
Put the Tests Before the Code
PPT
Software testing
PDF
[SRD UGM] Sharing Session - Software Testing
PPTX
Unit testing
PPT
Gray box testing
PPTX
Verification and Validation in Manual Testing
ODP
Documenting Code - Patterns and Anti-patterns - NLPW 2016
PPTX
5 black box and grey box testing
PDF
Using task models in model-based testing
PPTX
Testing the untestable
PPTX
White box testing
TDD - Designing with Expectations, not Implementations
Software Testing - Day Two
Beyond Unit Testing
Pair programming and introduction to TDD
Testing, black ,white and gray box testing
Hands-on Experience Model based testing with spec explorer
White box testing
Unit testing
Put the Tests Before the Code
Software testing
[SRD UGM] Sharing Session - Software Testing
Unit testing
Gray box testing
Verification and Validation in Manual Testing
Documenting Code - Patterns and Anti-patterns - NLPW 2016
5 black box and grey box testing
Using task models in model-based testing
Testing the untestable
White box testing
Ad

Similar to Pragmatic Introduction to PHP Unit Testing (2015) (20)

PDF
JUnit Boot Camp (GeeCON 2016)
PDF
Practical (J)Unit Testing (2009)
PPTX
Unit Testng with PHP Unit - A Step by Step Training
PDF
Unit testing in PHP
PDF
Code Quality Assurance v4 (2013)
PDF
Systematic Unit Testing
PPTX
Getting Started with Test-Driven Development at PHPtek 2023
PPTX
PHPUnit: from zero to hero
PDF
Leveling Up With Unit Testing - LonghornPHP 2022
KEY
Unit Testing Your Application
PDF
Cursus phpunit
PDF
Test Automation
PDF
Unit testing in PHP
PDF
Test Driven Development
PPT
Automated Unit Testing
PPT
Unit testing php-unit - phing - selenium_v2
PPS
Unit Testing
PDF
Fighting Fear-Driven-Development With PHPUnit
PPTX
Unit Testing talk
JUnit Boot Camp (GeeCON 2016)
Practical (J)Unit Testing (2009)
Unit Testng with PHP Unit - A Step by Step Training
Unit testing in PHP
Code Quality Assurance v4 (2013)
Systematic Unit Testing
Getting Started with Test-Driven Development at PHPtek 2023
PHPUnit: from zero to hero
Leveling Up With Unit Testing - LonghornPHP 2022
Unit Testing Your Application
Cursus phpunit
Test Automation
Unit testing in PHP
Test Driven Development
Automated Unit Testing
Unit testing php-unit - phing - selenium_v2
Unit Testing
Fighting Fear-Driven-Development With PHPUnit
Unit Testing talk
Ad

More from Peter Kofler (20)

PDF
Coding Dojo: Baby Steps Push Challenge (2021)
PDF
Coding Dojo: Naming with Dices (2021)
PDF
Outside-in Test Driven Development - the London School of TDD
PDF
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
PDF
Coding Dojo Object Calisthenics (2016)
PDF
Brutal Coding Constraints (ITAKE 2017)
PDF
Refactoring the Tennis Kata v2 (2016)
PDF
Designing Test Cases for the Gilded Rose Kata v3 (2016)
PDF
Coding Dojo: Asynchronous Clock-In (2016)
PDF
Mob Programming (2016)
PDF
Code Retreat Venice (2016)
PDF
Coding Dojo: Data Munging (2016)
PDF
Clean Readable Specifications (ETC 2016)
PDF
Extract Method Refactoring Workshop (2016)
PDF
Coding Dojo: Functional Calisthenics (2016)
PDF
Deliberate Practice (Agile Slovenia 2015)
PDF
GDCR15 in Las Palmas, Gran Canaria
PDF
Designing Test Cases for the Gilded Rose Kata v2 (2015)
PDF
Pair Programming (2015)
PDF
Coding Dojo: Bank OCR Outside-In (2015)
Coding Dojo: Baby Steps Push Challenge (2021)
Coding Dojo: Naming with Dices (2021)
Outside-in Test Driven Development - the London School of TDD
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Coding Dojo Object Calisthenics (2016)
Brutal Coding Constraints (ITAKE 2017)
Refactoring the Tennis Kata v2 (2016)
Designing Test Cases for the Gilded Rose Kata v3 (2016)
Coding Dojo: Asynchronous Clock-In (2016)
Mob Programming (2016)
Code Retreat Venice (2016)
Coding Dojo: Data Munging (2016)
Clean Readable Specifications (ETC 2016)
Extract Method Refactoring Workshop (2016)
Coding Dojo: Functional Calisthenics (2016)
Deliberate Practice (Agile Slovenia 2015)
GDCR15 in Las Palmas, Gran Canaria
Designing Test Cases for the Gilded Rose Kata v2 (2015)
Pair Programming (2015)
Coding Dojo: Bank OCR Outside-In (2015)

Recently uploaded (20)

PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PPT
Teaching material agriculture food technology
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Advanced IT Governance
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Sensors and Actuators in IoT Systems using pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Teaching material agriculture food technology
Advanced methodologies resolving dimensionality complications for autism neur...
MYSQL Presentation for SQL database connectivity
Diabetes mellitus diagnosis method based random forest with bat algorithm
Understanding_Digital_Forensics_Presentation.pptx
GamePlan Trading System Review: Professional Trader's Honest Take
Review of recent advances in non-invasive hemoglobin estimation
Advanced IT Governance
Chapter 3 Spatial Domain Image Processing.pdf
Modernizing your data center with Dell and AMD
Spectral efficient network and resource selection model in 5G networks
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
20250228 LYD VKU AI Blended-Learning.pptx
Sensors and Actuators in IoT Systems using pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Machine learning based COVID-19 study performance prediction
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Pragmatic Introduction to PHP Unit Testing (2015)

  • 1. A Pragmatic Introduction to (PHP) Unit Testing Vienna PHP, March 2015 Peter Kofler, ‘Code Cop’ @codecopkofler www.code-cop.org Copyright Peter Kofler, licensed under CC-BY.
  • 2. Peter Kofler • Ph.D. (Appl. Math.) • Professional Software Developer for 15 years • “fanatic about code quality” • I help development teams PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 4. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Yes, some but... ● only what is already there ● Trial & Error not popular in production ● no practice - only production ● time pressure
  • 5. I help development teams with PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY ● Professionalism ● Quality and Productivity ● Continuous Improvement
  • 6. Mentoring PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY ● Pair Programming ● Programming Workshops ● Deliberate Practice, e.g. Coding Dojos
  • 9. Quick Poll ● Who has heard of PHPUnit? (xUnit) ● Who has never written a unit test? ● Who has written a few? ● Who writes unit tests every day? ● Who does TDD? PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 10. We Make Mistakes ● at least I do...  ● number of bugs proportional LoC – 7 bugs per 1.000 LoC – 1 bug per 10.000 LoC in critical software ● Assume you have lots of bugs. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 11. TESTING I FIND YOUR LACK OF TESTS DISTURBING.
  • 12. What is a Unit Test?
  • 13. Unit Test (Micro Test) ● code written by a developer ● tests an individual unit ● isolate each part ● shows that the individual part is correct ● sort of living documentation PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 14. Unit of Work ● single logical functional use case ● invoked by some public interface – a single method, – a whole class or – multiple classes ● with one single logical purpose PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 16. Test Class ● unit test tests the functionality of a unit ● for various inputs and outputs ● usually a single test (class/script) class FooTest extends PHPUnit_Framework_TestCase { … } contains all test cases for Foo PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 17. Test Methods ● a test case tests the response of a single method to a particular set of inputs ● multiple test cases for a single method ● test methods should be short, simple ● tests without test methods are pointless (public) function testMethod() {…} /** @test */ function anyName() {…} PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 18. Only one aspect/feature ● tests work best with lots of small tests ● tests only fail because of one reason ● more than 1 assert in test split into multiple tests→ ● 1 test case – 1 method – 1 assertion PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY testHighAndLowInput() testHighInput() testLowInput()
  • 19. Assertions ● no output from your tests! ● check expectations programmatically ● e.g. $this->assertEquals, $this->assertTrue, … ● test method without assert is pointless ● one test method - one assertion ● test runner reports failure on each test  Regression Testing PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 20. Fixtures ● sets up data needed to run tests ● functions setUp(), tearDown()or /** @before */, /** @after */ ● Test data is more likely to be wrong than the tested code! PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 22. The Right BICEP ● Right: Are the results right? ● B: All the boundary conditions correct? ● I: Can we check inverse relationships? ● C: Can we cross-check results using other means? ● E: Can we force error conditions? ● P: Are performance characteristics within bounds? PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 23. “I write unit tests for one reason: so my co-workers don't f*** up my code.” (David Angry)
  • 25. Focus on Behaviour  e.g. requirements are behaviour  names from (problem) domain  full sentences (long, descriptive test method names)  expected behaviour should  separation per business module PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 26. Consist of 3 Simple Steps ● Prepare Input – Arrange ● Call Method – Act ● Check Output – Assert ● Use in form of Given-When-Then ● No conditional logic ( more test cases)→ ● No loops ( parametrized test)→ /** @dataProvider <static function> */ PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 27. Be F.I.R.S.T ● Fast ● Isolated (data, sequence) ● Repeatable ● Self-verifying ● Timely PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 28. Test Code Quality must be equal Prod. Code Quality
  • 29. Try PHPUnit yourself PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 30. Coding Dojo Mindset ● Safe place outside work ● We are here to learn ● Need to slow down ● Focus on doing it right ● Collaborative Game PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 31. Assignment ● Find a pair. ● Get https://bitbucket.org/pkofler/phpunit-koans ● Run phpunit – should see no tests ● Go through the test code ● assertions commented/ incomplete ● uncomment the assertions ● and complete them making tests pass PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 33. Closing Circle ● What did you learn today? ● What surprised you today? ● What will you do differently in the future? PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 34. Peter Kofler @codecopkofler www.code-cop.org PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 35. CC Images ● green http://www.flickr.com/photos/43442082@N00/296362882/ ● Hamster http://www.flickr.com/photos/zebrapares/4529836138 ● Bruce http://www.flickr.com/photos/sherpas428/4350620602/ ● pairing http://www.flickr.com/photos/dav/94735395/ ● agenda http://www.flickr.com/photos/24293932@N00/2752221871/ ● hands https://www.flickr.com/photos/ninahiironniemi/497993647/ ● Dojo http://www.flickr.com/photos/49715404@N00/3267627038/ ● wants you http://www.flickr.com/photos/shutter/105497713/ PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY