Test your code like a pro - PHPUnit in practice

6,001 views
5,696 views

Published on

The day you realised that you can’t really tell what your code does is the day you stop being an amateur programmer and you turn into a professional developer. During this workshop you will learn about the most famous unit testing framework – PHPUnit, how it can help you gain confidence in your code and what to do (and what to avoid) to make your code testable. We will discuss unit testing best practices and talk about tools that can help you automate the whole process, so it becomes more of a habit then a necessity.

Published in: Technology
0 Comments
16 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,001
On SlideShare
0
From Embeds
0
Number of Embeds
44
Actions
Shares
0
Downloads
90
Comments
0
Likes
16
Embeds 0
No embeds

No notes for slide

Test your code like a pro - PHPUnit in practice

  1. 1. Test your code like a pro PHPUnit in practiceSebastian Marek, Software Architect
  2. 2. Mea Pole living in Sheffieldover 12 years indevelopmentbig fan of processautomationTDD and CIoccasionally contributesto open source software
  3. 3. You?Who are you?What’s you typical environment you work in?What’s your experience in unit testing?What are your expectations?
  4. 4. AgendaPreparations / Setup Asserting exceptionsSimple test case Asserting PHPFixing first failure errorsPHPUnit CLI options Asserting outputAssertions Using data providersphpunit.xml file Generating code coverage report
  5. 5. ResourcesWIFI: phpnw12-phpunithosts file: 192.168.254.55 phpnw12-tutorialhttp://phpnw12-tutorial
  6. 6. Rescue plan$ git clone http://phpnw12-tutorial/phpnw12-tutorial.git$ cd phpnw12-tutorial$ git reset --hard$ git pull origin exercise-<number>
  7. 7. Exercise 1 Simple test caserepository structure src & testsnaming conventions Tutorial.php and TutorialTest.phpextends PHPUnit_Framework_TestCase
  8. 8. Exercise 1 -Simple test case Test first! namespace PhpNw12TestsWorkshop class TutorialTest method testGreetingsReturnWelcomeMessage() $ phpunit tests/Workshop/TutorialTest.php
  9. 9. Exercise 1 -Simple test case Code later! namespace PhpNw12Workshop class Tutorial method greetings() - empty for now $ phpunit tests/Workshop/TutorialTest.php
  10. 10. Exercise 1 -Simple test case Fix the code! method greetings() - implement the functionality $ phpunit tests/Workshop/TutorialTest.php
  11. 11. Exercise 1 -Simple test case Another test @test annotation method GetAttendeesReturnsListOfNames() assertInternalType() $ phpunit tests/Workshop/TutorialTest.php
  12. 12. Exercise 1 -Simple test case Implementation method getAttendees() - return empty array $ phpunit tests/Workshop/TutorialTest.php
  13. 13. Exercise 2 - Fixing the failure Refactoring add $_attendees property method getAttendees() return $_attendees $ phpunit tests/Workshop/TutorialTest.php
  14. 14. Exercise 2 - Fixing the failure Refactoring continues initialize $_attendees in the constructor $ phpunit tests/Workshop/TutorialTest.php
  15. 15. Exercise 2 - Fixing the failure Fix broken code assign default value in the constructor $ phpunit tests/Workshop/TutorialTest.php
  16. 16. Exercise 3PHPUnit CLI options --colors --testdox --debug --filter
  17. 17. Exercise 4 - More assertions Testing booleans - part 1new testtestTutorialHasNoPlacesLeftWhenMoreThen3Attendees()assertFalse()new method arePlacesLeft()$ phpunit tests/Workshop/TutorialTest.php
  18. 18. Exercise 4 - More assertions Testing booleans - part 2 new test testTutorialHasPlacesLeftWhenLessThen3Attendees() assertTrue() $ phpunit tests/Workshop/TutorialTest.php
  19. 19. Exercise 4 - More assertions Testing numbersnew testtestTutorialIsNotFullWhenItNotExceedsMaximumCapacity()assertGreaterThan(), assertLessThan(), assertNotNull()$ phpunit tests/Workshop/TutorialTest.php
  20. 20. Exercise 4 - More assertions Refactor to add functionality new const MAX_CAPACITY new method getNumberOfAttendees() refactor arePlacesLeft() to use the above $ phpunit tests/Workshop/TutorialTest.php
  21. 21. Exercise 4 - More assertions Testing variable types new test testRoomIsAvailable() assertInstanceOf() new class Room new property $_room initialized in the constructor $ phpunit tests/Workshop/TutorialTest.php
  22. 22. Exercise 4 - More assertions Testing arraysnew test testAttendeeGotAddedToTheList()assertContains()new testtestGetAtendeesReturnCorrectNumberOfAttendees()assertCount()$ phpunit tests/Workshop/TutorialTest.php
  23. 23. Exercise 5 - phpunit.xml phpunit.xml.dist vs phpunit.xml
  24. 24. Exercise 6 Asserting exceptionsnew testtestAddAttendeeThrowsExceptionWhenAddingNewPersonToFullTutorial()setExpectedException()new method addAttendee()$ phpunit
  25. 25. Exercise 6 Asserting exceptionsRefactor to add functionality refactor arePlacesLeft() refactor method addAttendee() $ phpunit
  26. 26. Exercise 6Asserting exceptions Using annotations @expectedException @expectedExceptionMessage @expectedExceptionCode $ phpunit
  27. 27. Exercise 6Asserting exceptions Using try...catchcatching exceptions using conventionalmethodsfail()$ phpunit
  28. 28. Exercise 7 Asserting errors Testing PHP errorsnew testtestInitiatingTutorialWithWrongParamThrowsError()PHP errors converted to PHPUnit_Framework_Error$ phpunit
  29. 29. Exercise 7 Asserting errorsTesting PHP warnings/noticesnew method Room::includeDependencies()new testtestIncludeDependenciesThrowsWarningForMissingFiles()PHP warnings converted toPHPUnit_Framework_Error_WarningPHP notices converted to PHPUnit_Framework_Error_Notice$ phpunit
  30. 30. Exercise 8 Asserting outputnew test testDisplayShowsGreetingsMessage()new method displaySummary()expectOutputString()$ phpunit
  31. 31. Exercise 9 Data providersnew providerprovideListOfAttendeesNotExceedingMaximumTutorialCapacity()modify testtestTutorialIsNotFullWhenItExceedsMaximumCapacity()@dataProvider$ phpunit
  32. 32. Exercise 10 Code coverage--coverage-htmlforceCoversAnnotation@covers$ phpunit --coverage-html=/tmp/phpnw12-tutorial
  33. 33. Q&Ahttps://joind.in/talk/view/7069

×