SlideShare una empresa de Scribd logo
1 de 36
PHP Unit-Testing Techniques

Stubs, Mocks, Dummies,
and Other Test Doubles
or

We will, we will
MOCK YOU!
Mihail Irintchev
What is Unit-Testing?
unit testing is a method by which individual units of source

code, sets of one or more computer program modules
together with associated control data, usage procedures, and
operating procedures are tested to determine if they are fit for
use... Intuitively, one can view a unit as the smallest
testable part of an application.
http://en.wikipedia.org/wiki/Unit_testing

unit testing is the safe ground I can hold onto when the

deadline is coming, an insurance policy against the
unavoidable changes, a safe harbor for my soul when
everything is shaking, including my belief in whether I am
capable of writing working code at all.

Me
Why is Unit-Testing Important?

?
Why is Unit-Testing Important?
Why is Unit-Testing Important?
Unit tests:


Find problems early



Facilitate change



Simplify integration



Play the role of living documentation



Sometimes aid software design



Save time (and money, and nerves) in the long run

Build a safety layer which creates a warm fuzzy feeling
inside the soul of the developer

What Makes Unit-Tests Work?



Each unit test is independent from the others



Unit tests are ran automatically and frequently



Best used as part of an automated build procedure



They should run fast
Why so fast?
How Do Unit-Tests Become
Independent and Fast?
Through isolation from:


the environment (DB, file system)



other tests



external systems (APIs, web services)



collaborators (other objects)
How is Isolation achieved?

Through the use of test doubles: “an object or procedure that
looks and behaves like its release-intended counterpart, but
is actually a simplified version that reduces the complexity
and facilitates testing”
“Testing-essentials mock objects explained”, Jeff Carouth
Types of Test Doubles







dummy
stub
mock
spy
fake
Warning: Terminology Kills




Dummy, mock, and fake are often used
interchangeably by many authors to refer to a
kind (or many kinds, or a mixed type) of a testing
double
There is a degree of difference, depending of
what extend of real functionality do these test
objects posses and how they implement it, but
what matters is when can each of them can be
useful to you.
Warning: Terminology Really Kills

No Implementation

Full Implementation
“Exploring The Continuum Of Test Doubles”, Mark Seemann
http://msdn.microsoft.com/en-us/magazine/cc163358.aspx
The Mock Turtle

Then the Queen left off, quite out of breath,
and said to Alice,
"Have you seen the Mock Turtle yet?"
"No," said Alice. "I don't even know what a Mock Turtle is."
"It's the thing Mock Turtle Soup is made from,"
said the Queen.
(Alice in Wonderland, chapter 9)
Mock Turtle & Mock Turtle Soup




The Mock Turtle is a fictional character devised
by Lewis Carroll from his popular book Alice's
Adventures in Wonderland. Its name is taken
from a dish that was popular in the Victorian
period, mock turtle soup.
Mock turtle soup is an English soup that was
created in the mid-18th century as a cheaper
imitation of green turtle soup. It often uses brains
and organ meats such as calf's head or a calf's
foot to duplicate the texture and flavour of the
original turtle meat.
Wikipedia
A Word About The Tool-set
Popular unit-testing frameworks for PHP:




PHPUnit
Simpletest
Mockery
The Industry Standard: PHPUnit
Why?


Great documentation



Very powerful (you will see)



Very easy installation/upgrade via pear



Great userbase



Integration with phing



Very passionate and dedicated author:
Sebastian Bergmann
1.

Dummy

A dummy is just a placeholder for a real object,
required by the one you are testing, but not really
used.
Original class
Class under test
The test method
The test method v1.1
2.

Stub

A stub provides “canned responses” to method
calls on the doubled object
Original class
Unit under test
The test method
3.

Mock & Spy

A mock object sets and asserts pre-defined
expectations of the methods it should or should
not receive
A spy records information about what was called
and can be used to verify calls (or the absence of
calls)
Original class
Unit under test
Test #1 – Failure expected
Test #2 – Success expected
Expectations can sometimes
be confusing...
5. Fake

A fake is actually a simpler implementation of a
real object
Wrapping it in one sentence ...

The question is NOT whether you should test or not,
but HOW should you test.
Resources

“PHPUnit Documentation: Chapter 10. Test Doubles” by Sebastian Bergmann
http://phpunit.de/manual/3.7/en/test-doubles.html

“Testing Essentials: Mock Objects Explained” by Jeff Carouth
https://speakerdeck.com/jcarouth/zendcon-2013-testing-essentials-mock-objects-explained
Q&A

?
Thank You!

Contact Info:
m.irintchev@siteground.com
@irintchev

Más contenido relacionado

Destacado

How to test models using php unit testing framework?
How to test models using php unit testing framework?How to test models using php unit testing framework?
How to test models using php unit testing framework?satejsahu
 
Ioc & in direction
Ioc & in directionIoc & in direction
Ioc & in direction育汶 郭
 
2007 5 30 肖镜辉 统计语言模型简介
2007 5 30 肖镜辉 统计语言模型简介2007 5 30 肖镜辉 统计语言模型简介
2007 5 30 肖镜辉 统计语言模型简介xceman
 
PHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くためにPHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くためにYuya Takeyama
 
PHPUnit 入門介紹
PHPUnit 入門介紹PHPUnit 入門介紹
PHPUnit 入門介紹Jace Ju
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentationThanh Robi
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitMichelangelo van Dam
 
Jmeter Performance Testing
Jmeter Performance TestingJmeter Performance Testing
Jmeter Performance TestingAtul Pant
 
B M Social Media Fortune 100
B M Social Media Fortune 100B M Social Media Fortune 100
B M Social Media Fortune 100Burson-Marsteller
 
Di – ioc (ninject)
Di – ioc (ninject)Di – ioc (ninject)
Di – ioc (ninject)ZealousysDev
 

Destacado (12)

How to test models using php unit testing framework?
How to test models using php unit testing framework?How to test models using php unit testing framework?
How to test models using php unit testing framework?
 
Ioc & in direction
Ioc & in directionIoc & in direction
Ioc & in direction
 
2007 5 30 肖镜辉 统计语言模型简介
2007 5 30 肖镜辉 统计语言模型简介2007 5 30 肖镜辉 统计语言模型简介
2007 5 30 肖镜辉 统计语言模型简介
 
IoC with PHP
IoC with PHPIoC with PHP
IoC with PHP
 
PHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くためにPHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くために
 
TDD Course (Spanish)
TDD Course (Spanish)TDD Course (Spanish)
TDD Course (Spanish)
 
PHPUnit 入門介紹
PHPUnit 入門介紹PHPUnit 入門介紹
PHPUnit 入門介紹
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentation
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnit
 
Jmeter Performance Testing
Jmeter Performance TestingJmeter Performance Testing
Jmeter Performance Testing
 
B M Social Media Fortune 100
B M Social Media Fortune 100B M Social Media Fortune 100
B M Social Media Fortune 100
 
Di – ioc (ninject)
Di – ioc (ninject)Di – ioc (ninject)
Di – ioc (ninject)
 

Similar a PHP Unit-Testing With Doubles

5-Ways-to-Revolutionize-Your-Software-Testing
5-Ways-to-Revolutionize-Your-Software-Testing5-Ways-to-Revolutionize-Your-Software-Testing
5-Ways-to-Revolutionize-Your-Software-TestingMary Clemons
 
Interaction testing using mock objects
Interaction testing using mock objectsInteraction testing using mock objects
Interaction testing using mock objectsLim Chanmann
 
Win at life with unit testing
Win at life with unit testingWin at life with unit testing
Win at life with unit testingmarkstory
 
DevDay.lk - Bare Knuckle Web Development
DevDay.lk - Bare Knuckle Web DevelopmentDevDay.lk - Bare Knuckle Web Development
DevDay.lk - Bare Knuckle Web DevelopmentJohannes Brodwall
 
Unit testing Agile OpenSpace
Unit testing Agile OpenSpaceUnit testing Agile OpenSpace
Unit testing Agile OpenSpaceAndrei Savu
 
Google, quality and you
Google, quality and youGoogle, quality and you
Google, quality and younelinger
 
Test Drive Development
Test Drive DevelopmentTest Drive Development
Test Drive Developmentsatya sudheer
 
Exploratory testing
Exploratory testingExploratory testing
Exploratory testingHuib Schoots
 
Testing for the deeplearning folks
Testing for the deeplearning folksTesting for the deeplearning folks
Testing for the deeplearning folksVishwas N
 
Enemy at the gates: vulnerability research in embedded appliances
Enemy at the gates: vulnerability research in embedded appliances Enemy at the gates: vulnerability research in embedded appliances
Enemy at the gates: vulnerability research in embedded appliances Chris Hernandez
 
Monkey runner & Monkey testing
Monkey runner & Monkey testingMonkey runner & Monkey testing
Monkey runner & Monkey testingSWAAM Tech
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flexmichael.labriola
 
I Smell A RAT- Rapid Application Testing
I Smell A RAT- Rapid Application TestingI Smell A RAT- Rapid Application Testing
I Smell A RAT- Rapid Application TestingPeter Presnell
 
Test Driven Development - Workshop
Test Driven Development - WorkshopTest Driven Development - Workshop
Test Driven Development - WorkshopAnjana Somathilake
 
Exploratory Testing in an Agile Context
Exploratory Testing in an Agile ContextExploratory Testing in an Agile Context
Exploratory Testing in an Agile ContextElisabeth Hendrickson
 
DevSecCon Tel Aviv 2018 - Integrated Security Testing by Morgan Roman
DevSecCon Tel Aviv 2018 - Integrated Security Testing by Morgan RomanDevSecCon Tel Aviv 2018 - Integrated Security Testing by Morgan Roman
DevSecCon Tel Aviv 2018 - Integrated Security Testing by Morgan RomanDevSecCon
 

Similar a PHP Unit-Testing With Doubles (20)

Unit Testing
Unit TestingUnit Testing
Unit Testing
 
5-Ways-to-Revolutionize-Your-Software-Testing
5-Ways-to-Revolutionize-Your-Software-Testing5-Ways-to-Revolutionize-Your-Software-Testing
5-Ways-to-Revolutionize-Your-Software-Testing
 
Interaction testing using mock objects
Interaction testing using mock objectsInteraction testing using mock objects
Interaction testing using mock objects
 
Software testing
Software testingSoftware testing
Software testing
 
Win at life with unit testing
Win at life with unit testingWin at life with unit testing
Win at life with unit testing
 
Debugging
DebuggingDebugging
Debugging
 
DevDay.lk - Bare Knuckle Web Development
DevDay.lk - Bare Knuckle Web DevelopmentDevDay.lk - Bare Knuckle Web Development
DevDay.lk - Bare Knuckle Web Development
 
Unit testing Agile OpenSpace
Unit testing Agile OpenSpaceUnit testing Agile OpenSpace
Unit testing Agile OpenSpace
 
Google, quality and you
Google, quality and youGoogle, quality and you
Google, quality and you
 
Test Drive Development
Test Drive DevelopmentTest Drive Development
Test Drive Development
 
Exploratory testing
Exploratory testingExploratory testing
Exploratory testing
 
Testing for the deeplearning folks
Testing for the deeplearning folksTesting for the deeplearning folks
Testing for the deeplearning folks
 
Enemy at the gates: vulnerability research in embedded appliances
Enemy at the gates: vulnerability research in embedded appliances Enemy at the gates: vulnerability research in embedded appliances
Enemy at the gates: vulnerability research in embedded appliances
 
Monkey runner & Monkey testing
Monkey runner & Monkey testingMonkey runner & Monkey testing
Monkey runner & Monkey testing
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
 
I Smell A RAT- Rapid Application Testing
I Smell A RAT- Rapid Application TestingI Smell A RAT- Rapid Application Testing
I Smell A RAT- Rapid Application Testing
 
UnitTestingBasics
UnitTestingBasicsUnitTestingBasics
UnitTestingBasics
 
Test Driven Development - Workshop
Test Driven Development - WorkshopTest Driven Development - Workshop
Test Driven Development - Workshop
 
Exploratory Testing in an Agile Context
Exploratory Testing in an Agile ContextExploratory Testing in an Agile Context
Exploratory Testing in an Agile Context
 
DevSecCon Tel Aviv 2018 - Integrated Security Testing by Morgan Roman
DevSecCon Tel Aviv 2018 - Integrated Security Testing by Morgan RomanDevSecCon Tel Aviv 2018 - Integrated Security Testing by Morgan Roman
DevSecCon Tel Aviv 2018 - Integrated Security Testing by Morgan Roman
 

Último

Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 

Último (20)

Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 

PHP Unit-Testing With Doubles

  • 1. PHP Unit-Testing Techniques Stubs, Mocks, Dummies, and Other Test Doubles or We will, we will MOCK YOU! Mihail Irintchev
  • 2. What is Unit-Testing? unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures are tested to determine if they are fit for use... Intuitively, one can view a unit as the smallest testable part of an application. http://en.wikipedia.org/wiki/Unit_testing unit testing is the safe ground I can hold onto when the deadline is coming, an insurance policy against the unavoidable changes, a safe harbor for my soul when everything is shaking, including my belief in whether I am capable of writing working code at all. Me
  • 3. Why is Unit-Testing Important? ?
  • 4. Why is Unit-Testing Important?
  • 5. Why is Unit-Testing Important? Unit tests:  Find problems early  Facilitate change  Simplify integration  Play the role of living documentation  Sometimes aid software design  Save time (and money, and nerves) in the long run Build a safety layer which creates a warm fuzzy feeling inside the soul of the developer 
  • 6. What Makes Unit-Tests Work?  Each unit test is independent from the others  Unit tests are ran automatically and frequently  Best used as part of an automated build procedure  They should run fast
  • 8. How Do Unit-Tests Become Independent and Fast? Through isolation from:  the environment (DB, file system)  other tests  external systems (APIs, web services)  collaborators (other objects)
  • 9. How is Isolation achieved? Through the use of test doubles: “an object or procedure that looks and behaves like its release-intended counterpart, but is actually a simplified version that reduces the complexity and facilitates testing” “Testing-essentials mock objects explained”, Jeff Carouth
  • 10. Types of Test Doubles      dummy stub mock spy fake
  • 11. Warning: Terminology Kills   Dummy, mock, and fake are often used interchangeably by many authors to refer to a kind (or many kinds, or a mixed type) of a testing double There is a degree of difference, depending of what extend of real functionality do these test objects posses and how they implement it, but what matters is when can each of them can be useful to you.
  • 12. Warning: Terminology Really Kills No Implementation Full Implementation “Exploring The Continuum Of Test Doubles”, Mark Seemann http://msdn.microsoft.com/en-us/magazine/cc163358.aspx
  • 13. The Mock Turtle Then the Queen left off, quite out of breath, and said to Alice, "Have you seen the Mock Turtle yet?" "No," said Alice. "I don't even know what a Mock Turtle is." "It's the thing Mock Turtle Soup is made from," said the Queen. (Alice in Wonderland, chapter 9)
  • 14. Mock Turtle & Mock Turtle Soup   The Mock Turtle is a fictional character devised by Lewis Carroll from his popular book Alice's Adventures in Wonderland. Its name is taken from a dish that was popular in the Victorian period, mock turtle soup. Mock turtle soup is an English soup that was created in the mid-18th century as a cheaper imitation of green turtle soup. It often uses brains and organ meats such as calf's head or a calf's foot to duplicate the texture and flavour of the original turtle meat. Wikipedia
  • 15. A Word About The Tool-set Popular unit-testing frameworks for PHP:    PHPUnit Simpletest Mockery
  • 16. The Industry Standard: PHPUnit Why?  Great documentation  Very powerful (you will see)  Very easy installation/upgrade via pear  Great userbase  Integration with phing  Very passionate and dedicated author: Sebastian Bergmann
  • 17. 1. Dummy A dummy is just a placeholder for a real object, required by the one you are testing, but not really used.
  • 22. 2. Stub A stub provides “canned responses” to method calls on the doubled object
  • 26. 3. Mock & Spy A mock object sets and asserts pre-defined expectations of the methods it should or should not receive A spy records information about what was called and can be used to verify calls (or the absence of calls)
  • 29. Test #1 – Failure expected
  • 30. Test #2 – Success expected
  • 32. 5. Fake A fake is actually a simpler implementation of a real object
  • 33. Wrapping it in one sentence ... The question is NOT whether you should test or not, but HOW should you test.
  • 34. Resources “PHPUnit Documentation: Chapter 10. Test Doubles” by Sebastian Bergmann http://phpunit.de/manual/3.7/en/test-doubles.html “Testing Essentials: Mock Objects Explained” by Jeff Carouth https://speakerdeck.com/jcarouth/zendcon-2013-testing-essentials-mock-objects-explained
  • 35. Q&A ?