Test your code like a pro - PHPUnit in practice

  • 3,865 views
Uploaded 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 …

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.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,865
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
63
Comments
0
Likes
12

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Test your code like a pro PHPUnit in practiceSebastian Marek, Software Architect
  • 2. Mea Pole living in Sheffieldover 12 years indevelopmentbig fan of processautomationTDD and CIoccasionally contributesto open source software
  • 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. 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. ResourcesWIFI: phpnw12-phpunithosts file: 192.168.254.55 phpnw12-tutorialhttp://phpnw12-tutorial
  • 6. Rescue plan$ git clone http://phpnw12-tutorial/phpnw12-tutorial.git$ cd phpnw12-tutorial$ git reset --hard$ git pull origin exercise-<number>
  • 7. Exercise 1 Simple test caserepository structure src & testsnaming conventions Tutorial.php and TutorialTest.phpextends PHPUnit_Framework_TestCase
  • 8. Exercise 1 -Simple test case Test first! namespace PhpNw12TestsWorkshop class TutorialTest method testGreetingsReturnWelcomeMessage() $ phpunit tests/Workshop/TutorialTest.php
  • 9. Exercise 1 -Simple test case Code later! namespace PhpNw12Workshop class Tutorial method greetings() - empty for now $ phpunit tests/Workshop/TutorialTest.php
  • 10. Exercise 1 -Simple test case Fix the code! method greetings() - implement the functionality $ phpunit tests/Workshop/TutorialTest.php
  • 11. Exercise 1 -Simple test case Another test @test annotation method GetAttendeesReturnsListOfNames() assertInternalType() $ phpunit tests/Workshop/TutorialTest.php
  • 12. Exercise 1 -Simple test case Implementation method getAttendees() - return empty array $ phpunit tests/Workshop/TutorialTest.php
  • 13. Exercise 2 - Fixing the failure Refactoring add $_attendees property method getAttendees() return $_attendees $ phpunit tests/Workshop/TutorialTest.php
  • 14. Exercise 2 - Fixing the failure Refactoring continues initialize $_attendees in the constructor $ phpunit tests/Workshop/TutorialTest.php
  • 15. Exercise 2 - Fixing the failure Fix broken code assign default value in the constructor $ phpunit tests/Workshop/TutorialTest.php
  • 16. Exercise 3PHPUnit CLI options --colors --testdox --debug --filter
  • 17. Exercise 4 - More assertions Testing booleans - part 1new testtestTutorialHasNoPlacesLeftWhenMoreThen3Attendees()assertFalse()new method arePlacesLeft()$ phpunit tests/Workshop/TutorialTest.php
  • 18. Exercise 4 - More assertions Testing booleans - part 2 new test testTutorialHasPlacesLeftWhenLessThen3Attendees() assertTrue() $ phpunit tests/Workshop/TutorialTest.php
  • 19. Exercise 4 - More assertions Testing numbersnew testtestTutorialIsNotFullWhenItNotExceedsMaximumCapacity()assertGreaterThan(), assertLessThan(), assertNotNull()$ phpunit tests/Workshop/TutorialTest.php
  • 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. 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. Exercise 4 - More assertions Testing arraysnew test testAttendeeGotAddedToTheList()assertContains()new testtestGetAtendeesReturnCorrectNumberOfAttendees()assertCount()$ phpunit tests/Workshop/TutorialTest.php
  • 23. Exercise 5 - phpunit.xml phpunit.xml.dist vs phpunit.xml
  • 24. Exercise 6 Asserting exceptionsnew testtestAddAttendeeThrowsExceptionWhenAddingNewPersonToFullTutorial()setExpectedException()new method addAttendee()$ phpunit
  • 25. Exercise 6 Asserting exceptionsRefactor to add functionality refactor arePlacesLeft() refactor method addAttendee() $ phpunit
  • 26. Exercise 6Asserting exceptions Using annotations @expectedException @expectedExceptionMessage @expectedExceptionCode $ phpunit
  • 27. Exercise 6Asserting exceptions Using try...catchcatching exceptions using conventionalmethodsfail()$ phpunit
  • 28. Exercise 7 Asserting errors Testing PHP errorsnew testtestInitiatingTutorialWithWrongParamThrowsError()PHP errors converted to PHPUnit_Framework_Error$ phpunit
  • 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. Exercise 8 Asserting outputnew test testDisplayShowsGreetingsMessage()new method displaySummary()expectOutputString()$ phpunit
  • 31. Exercise 9 Data providersnew providerprovideListOfAttendeesNotExceedingMaximumTutorialCapacity()modify testtestTutorialIsNotFullWhenItExceedsMaximumCapacity()@dataProvider$ phpunit
  • 32. Exercise 10 Code coverage--coverage-htmlforceCoversAnnotation@covers$ phpunit --coverage-html=/tmp/phpnw12-tutorial
  • 33. Q&Ahttps://joind.in/talk/view/7069