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
Ad

More Related Content

What's hot (20)

TDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not ImplementationsTDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not Implementations
Harshith Shetty
 
Software Testing - Day Two
Software Testing - Day TwoSoftware Testing - Day Two
Software Testing - Day Two
Govardhan Reddy
 
Beyond Unit Testing
Beyond Unit TestingBeyond Unit Testing
Beyond Unit Testing
Søren Lund
 
TDD
TDDTDD
TDD
Dotan Patrich
 
Pair programming and introduction to TDD
Pair programming and introduction to TDDPair programming and introduction to TDD
Pair programming and introduction to TDD
Arati Joshi
 
Testing, black ,white and gray box testing
Testing, black ,white and gray box testingTesting, black ,white and gray box testing
Testing, black ,white and gray box testing
Aamir Shakir
 
Hands-on Experience Model based testing with spec explorer
Hands-on Experience Model based testing with spec explorer Hands-on Experience Model based testing with spec explorer
Hands-on Experience Model based testing with spec explorer
Rachid Kherrazi
 
White box testing
White box testingWhite box testing
White box testing
Neethu Tressa
 
Unit testing
Unit testingUnit testing
Unit testing
princezzlove
 
Put the Tests Before the Code
Put the Tests Before the CodePut the Tests Before the Code
Put the Tests Before the Code
Mike Clement
 
Software testing
Software testingSoftware testing
Software testing
Animesh Dutta
 
[SRD UGM] Sharing Session - Software Testing
[SRD UGM] Sharing Session - Software Testing[SRD UGM] Sharing Session - Software Testing
[SRD UGM] Sharing Session - Software Testing
Ahmad Widardi
 
Unit testing
Unit testing Unit testing
Unit testing
Mani Kanth
 
Gray box testing
Gray box testingGray box testing
Gray box testing
Dasun Eranthika
 
Verification and Validation in Manual Testing
Verification and Validation in Manual TestingVerification and Validation in Manual Testing
Verification and Validation in Manual Testing
Bollapalli Vasundhara
 
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Søren Lund
 
5 black box and grey box testing
5   black box and grey box testing5   black box and grey box testing
5 black box and grey box testing
Yisal Khan
 
Using task models in model-based testing
Using task models in model-based testingUsing task models in model-based testing
Using task models in model-based testing
Rachid Kherrazi
 
Testing the untestable
Testing the untestableTesting the untestable
Testing the untestable
RoyKlein
 
White box testing
White box testing White box testing
White box testing
Mani Kanth
 
TDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not ImplementationsTDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not Implementations
Harshith Shetty
 
Software Testing - Day Two
Software Testing - Day TwoSoftware Testing - Day Two
Software Testing - Day Two
Govardhan Reddy
 
Beyond Unit Testing
Beyond Unit TestingBeyond Unit Testing
Beyond Unit Testing
Søren Lund
 
Pair programming and introduction to TDD
Pair programming and introduction to TDDPair programming and introduction to TDD
Pair programming and introduction to TDD
Arati Joshi
 
Testing, black ,white and gray box testing
Testing, black ,white and gray box testingTesting, black ,white and gray box testing
Testing, black ,white and gray box testing
Aamir Shakir
 
Hands-on Experience Model based testing with spec explorer
Hands-on Experience Model based testing with spec explorer Hands-on Experience Model based testing with spec explorer
Hands-on Experience Model based testing with spec explorer
Rachid Kherrazi
 
Put the Tests Before the Code
Put the Tests Before the CodePut the Tests Before the Code
Put the Tests Before the Code
Mike Clement
 
[SRD UGM] Sharing Session - Software Testing
[SRD UGM] Sharing Session - Software Testing[SRD UGM] Sharing Session - Software Testing
[SRD UGM] Sharing Session - Software Testing
Ahmad Widardi
 
Verification and Validation in Manual Testing
Verification and Validation in Manual TestingVerification and Validation in Manual Testing
Verification and Validation in Manual Testing
Bollapalli Vasundhara
 
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Søren Lund
 
5 black box and grey box testing
5   black box and grey box testing5   black box and grey box testing
5 black box and grey box testing
Yisal Khan
 
Using task models in model-based testing
Using task models in model-based testingUsing task models in model-based testing
Using task models in model-based testing
Rachid Kherrazi
 
Testing the untestable
Testing the untestableTesting the untestable
Testing the untestable
RoyKlein
 
White box testing
White box testing White box testing
White box testing
Mani Kanth
 

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

Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)
Peter Kofler
 
Prime Factors Code Kata - Practicing TDD (2014)
Prime Factors Code Kata - Practicing TDD (2014)Prime Factors Code Kata - Practicing TDD (2014)
Prime Factors Code Kata - Practicing TDD (2014)
Peter Kofler
 
Deliberate Practice (Agile Slovenia 2015)
Deliberate Practice (Agile Slovenia 2015)Deliberate Practice (Agile Slovenia 2015)
Deliberate Practice (Agile Slovenia 2015)
Peter Kofler
 
JUnit Boot Camp (GeeCON 2016)
JUnit Boot Camp (GeeCON 2016)JUnit Boot Camp (GeeCON 2016)
JUnit Boot Camp (GeeCON 2016)
Peter Kofler
 
Codeception: introduction to php testing (v2 - Aberdeen php)
Codeception: introduction to php testing (v2 - Aberdeen php)Codeception: introduction to php testing (v2 - Aberdeen php)
Codeception: introduction to php testing (v2 - Aberdeen php)
Engineor
 
Codeception: introduction to php testing
Codeception: introduction to php testingCodeception: introduction to php testing
Codeception: introduction to php testing
Engineor
 
Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)
Peter Kofler
 
Outside-in Test Driven Development - the London School of TDD
Outside-in Test Driven Development - the London School of TDDOutside-in Test Driven Development - the London School of TDD
Outside-in Test Driven Development - the London School of TDD
Peter Kofler
 
Practical (J)Unit Testing (2009)
Practical (J)Unit Testing (2009)Practical (J)Unit Testing (2009)
Practical (J)Unit Testing (2009)
Peter Kofler
 
UPC Plone Testing Talk
UPC Plone Testing TalkUPC Plone Testing Talk
UPC Plone Testing Talk
Timo Stollenwerk
 
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Andrew Yatsenko
 
Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)
Peter Kofler
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
Xavi Hidalgo
 
Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)
Peter Kofler
 
How to establish ways of working that allows shifting-left of the automation ...
How to establish ways of working that allows shifting-left of the automation ...How to establish ways of working that allows shifting-left of the automation ...
How to establish ways of working that allows shifting-left of the automation ...
Max Barrass
 
Test Driven Development with PHP
Test Driven Development with PHPTest Driven Development with PHP
Test Driven Development with PHP
Rogério Vicente
 
Unit testing (eng)
Unit testing (eng)Unit testing (eng)
Unit testing (eng)
Anatoliy Okhotnikov
 
Test-Driven Development.pptx
Test-Driven Development.pptxTest-Driven Development.pptx
Test-Driven Development.pptx
Tomas561914
 
TDD as if You Meant It (2013)
TDD as if You Meant It (2013)TDD as if You Meant It (2013)
TDD as if You Meant It (2013)
Peter Kofler
 
Pair Programming (2015)
Pair Programming (2015)Pair Programming (2015)
Pair Programming (2015)
Peter Kofler
 
Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)
Peter Kofler
 
Prime Factors Code Kata - Practicing TDD (2014)
Prime Factors Code Kata - Practicing TDD (2014)Prime Factors Code Kata - Practicing TDD (2014)
Prime Factors Code Kata - Practicing TDD (2014)
Peter Kofler
 
Deliberate Practice (Agile Slovenia 2015)
Deliberate Practice (Agile Slovenia 2015)Deliberate Practice (Agile Slovenia 2015)
Deliberate Practice (Agile Slovenia 2015)
Peter Kofler
 
JUnit Boot Camp (GeeCON 2016)
JUnit Boot Camp (GeeCON 2016)JUnit Boot Camp (GeeCON 2016)
JUnit Boot Camp (GeeCON 2016)
Peter Kofler
 
Codeception: introduction to php testing (v2 - Aberdeen php)
Codeception: introduction to php testing (v2 - Aberdeen php)Codeception: introduction to php testing (v2 - Aberdeen php)
Codeception: introduction to php testing (v2 - Aberdeen php)
Engineor
 
Codeception: introduction to php testing
Codeception: introduction to php testingCodeception: introduction to php testing
Codeception: introduction to php testing
Engineor
 
Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)
Peter Kofler
 
Outside-in Test Driven Development - the London School of TDD
Outside-in Test Driven Development - the London School of TDDOutside-in Test Driven Development - the London School of TDD
Outside-in Test Driven Development - the London School of TDD
Peter Kofler
 
Practical (J)Unit Testing (2009)
Practical (J)Unit Testing (2009)Practical (J)Unit Testing (2009)
Practical (J)Unit Testing (2009)
Peter Kofler
 
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Andrew Yatsenko
 
Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)
Peter Kofler
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
Xavi Hidalgo
 
Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)
Peter Kofler
 
How to establish ways of working that allows shifting-left of the automation ...
How to establish ways of working that allows shifting-left of the automation ...How to establish ways of working that allows shifting-left of the automation ...
How to establish ways of working that allows shifting-left of the automation ...
Max Barrass
 
Test Driven Development with PHP
Test Driven Development with PHPTest Driven Development with PHP
Test Driven Development with PHP
Rogério Vicente
 
Test-Driven Development.pptx
Test-Driven Development.pptxTest-Driven Development.pptx
Test-Driven Development.pptx
Tomas561914
 
TDD as if You Meant It (2013)
TDD as if You Meant It (2013)TDD as if You Meant It (2013)
TDD as if You Meant It (2013)
Peter Kofler
 
Pair Programming (2015)
Pair Programming (2015)Pair Programming (2015)
Pair Programming (2015)
Peter Kofler
 
Ad

More from Peter Kofler (20)

Coding Dojo: Baby Steps Push Challenge (2021)
Coding Dojo: Baby Steps Push Challenge (2021)Coding Dojo: Baby Steps Push Challenge (2021)
Coding Dojo: Baby Steps Push Challenge (2021)
Peter Kofler
 
Coding Dojo: Naming with Dices (2021)
Coding Dojo: Naming with Dices (2021)Coding Dojo: Naming with Dices (2021)
Coding Dojo: Naming with Dices (2021)
Peter Kofler
 
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Peter Kofler
 
Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)
Peter Kofler
 
Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)
Peter Kofler
 
Refactoring the Tennis Kata v2 (2016)
Refactoring the Tennis Kata v2 (2016)Refactoring the Tennis Kata v2 (2016)
Refactoring the Tennis Kata v2 (2016)
Peter Kofler
 
Designing Test Cases for the Gilded Rose Kata v3 (2016)
Designing Test Cases for the Gilded Rose Kata v3 (2016)Designing Test Cases for the Gilded Rose Kata v3 (2016)
Designing Test Cases for the Gilded Rose Kata v3 (2016)
Peter Kofler
 
Coding Dojo: Asynchronous Clock-In (2016)
Coding Dojo: Asynchronous Clock-In (2016)Coding Dojo: Asynchronous Clock-In (2016)
Coding Dojo: Asynchronous Clock-In (2016)
Peter Kofler
 
Mob Programming (2016)
Mob Programming (2016)Mob Programming (2016)
Mob Programming (2016)
Peter Kofler
 
Code Retreat Venice (2016)
Code Retreat Venice (2016)Code Retreat Venice (2016)
Code Retreat Venice (2016)
Peter Kofler
 
Coding Dojo: Data Munging (2016)
Coding Dojo: Data Munging (2016)Coding Dojo: Data Munging (2016)
Coding Dojo: Data Munging (2016)
Peter Kofler
 
Clean Readable Specifications (ETC 2016)
Clean Readable Specifications (ETC 2016)Clean Readable Specifications (ETC 2016)
Clean Readable Specifications (ETC 2016)
Peter Kofler
 
GDCR15 in Las Palmas, Gran Canaria
GDCR15 in Las Palmas, Gran CanariaGDCR15 in Las Palmas, Gran Canaria
GDCR15 in Las Palmas, Gran Canaria
Peter Kofler
 
Designing Test Cases for the Gilded Rose Kata v2 (2015)
Designing Test Cases for the Gilded Rose Kata v2 (2015)Designing Test Cases for the Gilded Rose Kata v2 (2015)
Designing Test Cases for the Gilded Rose Kata v2 (2015)
Peter Kofler
 
Coding Dojo: Bank OCR Outside-In (2015)
Coding Dojo: Bank OCR Outside-In (2015)Coding Dojo: Bank OCR Outside-In (2015)
Coding Dojo: Bank OCR Outside-In (2015)
Peter Kofler
 
Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)
Peter Kofler
 
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Peter Kofler
 
Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)
Peter Kofler
 
Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)
Peter Kofler
 
Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)
Peter Kofler
 
Coding Dojo: Baby Steps Push Challenge (2021)
Coding Dojo: Baby Steps Push Challenge (2021)Coding Dojo: Baby Steps Push Challenge (2021)
Coding Dojo: Baby Steps Push Challenge (2021)
Peter Kofler
 
Coding Dojo: Naming with Dices (2021)
Coding Dojo: Naming with Dices (2021)Coding Dojo: Naming with Dices (2021)
Coding Dojo: Naming with Dices (2021)
Peter Kofler
 
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Peter Kofler
 
Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)
Peter Kofler
 
Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)
Peter Kofler
 
Refactoring the Tennis Kata v2 (2016)
Refactoring the Tennis Kata v2 (2016)Refactoring the Tennis Kata v2 (2016)
Refactoring the Tennis Kata v2 (2016)
Peter Kofler
 
Designing Test Cases for the Gilded Rose Kata v3 (2016)
Designing Test Cases for the Gilded Rose Kata v3 (2016)Designing Test Cases for the Gilded Rose Kata v3 (2016)
Designing Test Cases for the Gilded Rose Kata v3 (2016)
Peter Kofler
 
Coding Dojo: Asynchronous Clock-In (2016)
Coding Dojo: Asynchronous Clock-In (2016)Coding Dojo: Asynchronous Clock-In (2016)
Coding Dojo: Asynchronous Clock-In (2016)
Peter Kofler
 
Mob Programming (2016)
Mob Programming (2016)Mob Programming (2016)
Mob Programming (2016)
Peter Kofler
 
Code Retreat Venice (2016)
Code Retreat Venice (2016)Code Retreat Venice (2016)
Code Retreat Venice (2016)
Peter Kofler
 
Coding Dojo: Data Munging (2016)
Coding Dojo: Data Munging (2016)Coding Dojo: Data Munging (2016)
Coding Dojo: Data Munging (2016)
Peter Kofler
 
Clean Readable Specifications (ETC 2016)
Clean Readable Specifications (ETC 2016)Clean Readable Specifications (ETC 2016)
Clean Readable Specifications (ETC 2016)
Peter Kofler
 
GDCR15 in Las Palmas, Gran Canaria
GDCR15 in Las Palmas, Gran CanariaGDCR15 in Las Palmas, Gran Canaria
GDCR15 in Las Palmas, Gran Canaria
Peter Kofler
 
Designing Test Cases for the Gilded Rose Kata v2 (2015)
Designing Test Cases for the Gilded Rose Kata v2 (2015)Designing Test Cases for the Gilded Rose Kata v2 (2015)
Designing Test Cases for the Gilded Rose Kata v2 (2015)
Peter Kofler
 
Coding Dojo: Bank OCR Outside-In (2015)
Coding Dojo: Bank OCR Outside-In (2015)Coding Dojo: Bank OCR Outside-In (2015)
Coding Dojo: Bank OCR Outside-In (2015)
Peter Kofler
 
Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)
Peter Kofler
 
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Peter Kofler
 
Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)
Peter Kofler
 
Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)
Peter Kofler
 
Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)
Peter Kofler
 
Ad

Recently uploaded (20)

2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 

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