Test your code like a pro - PHPUnit in practice

33
Test your code like a pro PHPUnit in practice Sebastian Marek, Software Architect

description

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.

Transcript of Test your code like a pro - PHPUnit in practice

Page 1: Test your code like a pro - PHPUnit in practice

Test your code like a proPHPUnit in practice

Sebastian Marek, Software Architect

Page 2: Test your code like a pro - PHPUnit in practice

Mea Pole living in Sheffield

over 12 years in development

big fan of process automation

TDD and CI

occasionally contributes to open source software

Page 3: Test your code like a pro - PHPUnit in practice

Who are you?

What’s you typical environment you work in?

What’s your experience in unit testing?

What are your expectations?

You?

Page 4: Test your code like a pro - PHPUnit in practice

Agenda

Preparations / SetupSimple test caseFixing first failurePHPUnit CLI optionsAssertionsphpunit.xml file

Asserting exceptionsAsserting PHP errorsAsserting outputUsing data providersGenerating code coverage report

Page 5: Test your code like a pro - PHPUnit in practice

Resources

WIFI: phpnw12-phpunit

hosts file: 192.168.254.55 phpnw12-tutorial

http://phpnw12-tutorial

Page 6: Test your code like a pro - PHPUnit in practice

Rescue plan

$ git clone http://phpnw12-tutorial/phpnw12-tutorial.git

$ cd phpnw12-tutorial

$ git reset --hard

$ git pull origin exercise-<number>

Page 7: Test your code like a pro - PHPUnit in practice

Exercise 1Simple test case

repository structure

src & tests

naming conventions

Tutorial.php and TutorialTest.php

extends \PHPUnit_Framework_TestCase

Page 8: Test your code like a pro - PHPUnit in practice

Exercise 1 -Simple test case

namespace PhpNw12\Tests\Workshop

class TutorialTest

method testGreetingsReturnWelcomeMessage()

$ phpunit tests/Workshop/TutorialTest.php

Test first!

Page 9: Test your code like a pro - PHPUnit in practice

Exercise 1 -Simple test case

namespace PhpNw12\Workshop

class Tutorial

method greetings() - empty for now

$ phpunit tests/Workshop/TutorialTest.php

Code later!

Page 10: Test your code like a pro - PHPUnit in practice

Exercise 1 -Simple test case

method greetings() - implement the functionality

$ phpunit tests/Workshop/TutorialTest.php

Fix the code!

Page 11: Test your code like a pro - PHPUnit in practice

Exercise 1 -Simple test case

@test annotation

method GetAttendeesReturnsListOfNames()

assertInternalType()

$ phpunit tests/Workshop/TutorialTest.php

Another test

Page 12: Test your code like a pro - PHPUnit in practice

Exercise 1 -Simple test case

method getAttendees() - return empty array

$ phpunit tests/Workshop/TutorialTest.php

Implementation

Page 13: Test your code like a pro - PHPUnit in practice

Exercise 2 - Fixing the failure

add $_attendees property

method getAttendees() return $_attendees

$ phpunit tests/Workshop/TutorialTest.php

Refactoring

Page 14: Test your code like a pro - PHPUnit in practice

Exercise 2 - Fixing the failure

initialize $_attendees in the constructor

$ phpunit tests/Workshop/TutorialTest.php

Refactoring continues

Page 15: Test your code like a pro - PHPUnit in practice

Exercise 2 - Fixing the failure

assign default value in the constructor

$ phpunit tests/Workshop/TutorialTest.php

Fix broken code

Page 16: Test your code like a pro - PHPUnit in practice

Exercise 3 PHPUnit CLI options

--colors

--testdox

--debug

--filter

Page 17: Test your code like a pro - PHPUnit in practice

Exercise 4 - More assertions

new test testTutorialHasNoPlacesLeftWhenMoreThen3Attendees()

assertFalse()

new method arePlacesLeft()

$ phpunit tests/Workshop/TutorialTest.php

Testing booleans - part 1

Page 18: Test your code like a pro - PHPUnit in practice

Exercise 4 - More assertions

new test testTutorialHasPlacesLeftWhenLessThen3Attendees()

assertTrue()

$ phpunit tests/Workshop/TutorialTest.php

Testing booleans - part 2

Page 19: Test your code like a pro - PHPUnit in practice

Exercise 4 - More assertions

new test testTutorialIsNotFullWhenItNotExceedsMaximumCapacity()

assertGreaterThan(), assertLessThan(), assertNotNull()

$ phpunit tests/Workshop/TutorialTest.php

Testing numbers

Page 20: Test your code like a pro - PHPUnit in practice

Exercise 4 - More assertions

new const MAX_CAPACITY

new method getNumberOfAttendees()

refactor arePlacesLeft() to use the above

$ phpunit tests/Workshop/TutorialTest.php

Refactor to add functionality

Page 21: Test your code like a pro - PHPUnit in practice

Exercise 4 - More assertions

new test testRoomIsAvailable()

assertInstanceOf()

new class Room

new property $_room initialized in the constructor

$ phpunit tests/Workshop/TutorialTest.php

Testing variable types

Page 22: Test your code like a pro - PHPUnit in practice

Exercise 4 - More assertions

new test testAttendeeGotAddedToTheList()

assertContains()

new test testGetAtendeesReturnCorrectNumberOfAttendees()

assertCount()

$ phpunit tests/Workshop/TutorialTest.php

Testing arrays

Page 23: Test your code like a pro - PHPUnit in practice

Exercise 5 - phpunit.xml

phpunit.xml.dist vs phpunit.xml

Page 24: Test your code like a pro - PHPUnit in practice

Exercise 6 Asserting exceptions

new test testAddAttendeeThrowsExceptionWhenAddingNewPersonToFullTutorial()

setExpectedException()

new method addAttendee()

$ phpunit

Page 25: Test your code like a pro - PHPUnit in practice

Exercise 6 Asserting exceptions

refactor arePlacesLeft()

refactor method addAttendee()

$ phpunit

Refactor to add functionality

Page 26: Test your code like a pro - PHPUnit in practice

Exercise 6 Asserting exceptions

@expectedException

@expectedExceptionMessage

@expectedExceptionCode

$ phpunit

Using annotations

Page 27: Test your code like a pro - PHPUnit in practice

Exercise 6 Asserting exceptions

catching exceptions using conventional methods

fail()

$ phpunit

Using try...catch

Page 28: Test your code like a pro - PHPUnit in practice

Exercise 7 Asserting errors

new test testInitiatingTutorialWithWrongParamThrowsError()

PHP errors converted to PHPUnit_Framework_Error

$ phpunit

Testing PHP errors

Page 29: Test your code like a pro - PHPUnit in practice

Exercise 7 Asserting errors

new method Room::includeDependencies()

new test testIncludeDependenciesThrowsWarningForMissingFiles()

PHP warnings converted to PHPUnit_Framework_Error_Warning

PHP notices converted to PHPUnit_Framework_Error_Notice

$ phpunit

Testing PHP warnings/notices

Page 30: Test your code like a pro - PHPUnit in practice

Exercise 8 Asserting output

new test testDisplayShowsGreetingsMessage()

new method displaySummary()

expectOutputString()

$ phpunit

Page 31: Test your code like a pro - PHPUnit in practice

Exercise 9 Data providers

new provider provideListOfAttendeesNotExceedingMaximumTutorialCapacity()

modify test testTutorialIsNotFullWhenItExceedsMaximumCapacity()

@dataProvider

$ phpunit

Page 32: Test your code like a pro - PHPUnit in practice

Exercise 10 Code coverage

--coverage-html

forceCoversAnnotation

@covers

$ phpunit --coverage-html=/tmp/phpnw12-tutorial

Page 33: Test your code like a pro - PHPUnit in practice

Q & Ahttps://joind.in/talk/view/7069