Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
A Pragmatic Introduction
to (PHP) Unit Testing
Vienna PHP, March 2015
Peter Kofler, ‘Code Cop’
@codecopkofler
www.code-cop...
Peter Kofler
• Ph.D. (Appl. Math.)
• Professional Software
Developer for 15 years
• “fanatic about code quality”
• I help ...
Training
on the
Job?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Yes, some but...
●
only what is already there
●
Trial & Error not po...
I help development teams with
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
●
Professionalism
●
Quality and
Produc...
Mentoring
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
●
Pair Programming
●
Programming
Workshops
●
Deliberate
Pr...
Developing Quality
Software Developers
Agenda
●
Unit Testing
●
PHPUnit
(Coding)
●
Break
●
PHPUnit
(Coding)
●
Retrospective
PETER KOFLER, CODE-COP.ORG FANATIC ABO...
Quick Poll
●
Who has heard of PHPUnit? (xUnit)
●
Who has never written a unit test?
●
Who has written
a few?
●
Who writes ...
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 c...
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 indiv...
Unit of Work
●
single logical functional use case
●
invoked by some public interface
– a single method,
– a whole class or...
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/s...
Test Methods
●
a test case tests the response of a single
method to a particular set of inputs
●
multiple test cases for a...
Only one aspect/feature
●
tests work best with lots of small tests
●
tests only fail because of one reason
●
more than 1 a...
Assertions
●
no output from your tests!
●
check expectations programmatically
●
e.g. $this->assertEquals,
$this->assertTru...
Fixtures
●
sets up data needed to run tests
●
functions setUp(), tearDown()or
/** @before */, /** @after */
●
Test data is...
What should
we test?
The Right BICEP
●
Right: Are the results right?
●
B: All the boundary conditions correct?
●
I: Can we check inverse relati...
“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 tes...
Consist of 3 Simple Steps
●
Prepare Input – Arrange
●
Call Method – Act
●
Check Output – Assert
●
Use in form of Given-Whe...
Be F.I.R.S.T
●
Fast
●
Isolated (data, sequence)
●
Repeatable
●
Self-verifying
●
Timely
PETER KOFLER, CODE-COP.ORG FANATIC ...
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
●
Colla...
Assignment
●
Find a pair.
●
Get https://bitbucket.org/pkofler/phpunit-koans
●
Run phpunit – should see no tests
●
Go throu...
→Practice
Closing Circle
●
What did you learn today?
●
What surprised you today?
●
What will you do
differently in the
future?
PETER...
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/4...
Upcoming SlideShare
Loading in …5
×

Pragmatic Introduction to PHP Unit Testing (2015)

938 views

Published on

Presentation of Workshop March: A Pragmatic Introduction to Unit Testing with PHP of Vienna PHP User Group

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Pragmatic Introduction to PHP Unit Testing (2015)

  1. 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. 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
  3. 3. Training on the Job?
  4. 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. 5. I help development teams with PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY ● Professionalism ● Quality and Productivity ● Continuous Improvement
  6. 6. Mentoring PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY ● Pair Programming ● Programming Workshops ● Deliberate Practice, e.g. Coding Dojos
  7. 7. Developing Quality Software Developers
  8. 8. Agenda ● Unit Testing ● PHPUnit (Coding) ● Break ● PHPUnit (Coding) ● Retrospective PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  9. 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. 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. 11. TESTING I FIND YOUR LACK OF TESTS DISTURBING.
  12. 12. What is a Unit Test?
  13. 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. 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
  15. 15. What is it made of?
  16. 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. 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. 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. 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. 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
  21. 21. What should we test?
  22. 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. 23. “I write unit tests for one reason: so my co-workers don't f*** up my code.” (David Angry)
  24. 24. A Unit Test should ...
  25. 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. 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. 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. 28. Test Code Quality must be equal Prod. Code Quality
  29. 29. Try PHPUnit yourself PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  30. 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. 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
  32. 32. →Practice
  33. 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. 34. Peter Kofler @codecopkofler www.code-cop.org PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  35. 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

×