Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Unit Testing with PHPUnit

Introduction to Unit Testing with PHPUnit

Presentation from Ottawa PHP Meetup on April 1, 2015.

Fraser Reed

April 01, 2015
Tweet

Other Decks in Technology

Transcript

  1. TOPICS What is unit testing? Why should I test my

    code? Background on PHPUnit Installation The basics Simple example Working with resources Questions?
  2. WHAT IS UNIT TESTING? Testing individual units of code to

    determine if they are fit to use. The smallest testable part of an application: functions, classes and methods Prove that code performs within a set of guidelines Unit testing is a fundamental building block of writing professional, maintainable code
  3. WHY SHOULD I TEST MY CODE? Confidence in your code

    Prevent breaking changes Speeds up future development Helps write clean code
  4. BACKGROUND ON PHPUNIT Written by Sebastian Bergmann https://phpunit.de/ https://github.com/sebastianbergmann/phpunit Current

    stable version: 4.5.1 Many optional packages: code coverage, lines of code, dbunit, selenium, etc.
  5. INSTALLATION VIA PHAR w g e t h t t

    p s : / / p h a r . p h p u n i t . d e / p h p u n i t . p h a r p h p p h p u n i t . p h a r
  6. INSTALLATION VIA COMPOSER { " r e q u i

    r e - d e v " : { " p h p u n i t / p h p u n i t " : " 4 . 5 . * " } }
  7. ClassTest inherits (most of the time) from PHPUnit_Framework_TestCase. Alternatively, you

    can extend PHPUnit_Framework_TestCase to override or customize basic functionality.
  8. Inside the test methods, assertion methods are used to assert

    that an actual value matches an expected value.