PHPUnit Automated Unit Testing Framework

20
Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org PHPUnit Automated Unit Testing Framework June 5, 2008

description

An introduction to PHPUnit, written for the PHP Meetup group I run.

Transcript of PHPUnit Automated Unit Testing Framework

Page 1: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

PHPUnitAutomated Unit

Testing Framework

June 5, 2008

Page 2: PHPUnit Automated Unit Testing Framework

“I grew up around computers, fell in love with the Internet way back in 1994, and built a successful career around my interests in computers and business. My career focus has been on e-commerce, and in my personal time I study issues related to digital identity, trust, and reputation tracking. I’m also an small business owner, an avid Scrabble player, and a b-movie junkie.”

9 years professional development experience

PHP developer, certified Java developer

LinkedIn profile:http://www.linkedin.com/pub/2/832/9b0

Dave Ross

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

Page 3: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

The West Suburban Chicago PHP Meetup

Meets first Thursday of every month

Usually at the Panera in Wheaton

Official Meetup page:

http://php.meetup.com/381

Blog:

http://suburbanchicagophp.org

Page 4: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

PHPUnit is a member of the xUnit family of testing frameworks (SUnit, jUnit, pyUnit...)

Written for PHP 5.x, older versions supported PHP 4.x

Page 5: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

Unit testing:

Testing the smallest testable partsof an application

(Functions, Classes, Macros)

Page 6: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

The xUnit frameworks provide away to automate unit tests

If you automate your tests,the quality of your tests

is consistent

Page 7: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

Make it easy to test,and you'll do it more often.

If you use version control,test before every check-in

Page 8: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

You can wrap a set of testsinto a test suite

A suite tests larger units ofyour application, even the

application itself

Page 9: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

PHP itself is tested usingautomated tests

They use a framework called phpt

Page 10: PHPUnit Automated Unit Testing Framework

The PHPUnit Pocket Guide

Tells you everything youneed to know when

writing PHPUnit test cases

Buy the book, or read theHTML version at:

http://www.phpunit.de/pocket_guide/index.en.php

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

Page 11: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

Let's look at a test case!

Page 12: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

<?phprequire_once 'PHPUnit/Framework.php'; class ArrayTest extends PHPUnit_Framework_TestCase{ protected $fixture; protected function setUp() { // Create the Array fixture. $this->fixture = array(); } public function testNewArrayIsEmpty() { // Assert that the size of the Array fixture is 0. $this->assertEquals(0, sizeof($this->fixture)); } public function testArrayContainsAnElement() { // Add an element to the Array fixture. $this->fixture[] = 'Element'; // Assert that the size of the Array fixture is 1. $this->assertEquals(1, sizeof($this->fixture)); }}?>

Page 13: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

Tests should be run against aknown, fixed statecalled a fixture

setUp() creates this state

tearDown() destroys the stateand puts things back the way

they were

Page 14: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

Test cases are implementedas functions whose namesstart with the word “test”

This example has:testNewArrayIsEmpty()

testArrayContainsAnElement()

Page 15: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

Run a PHPUnit test casewith the phpunit command

phpunit UnitTest UnitTest.phptells PHPUnit to look for a

class UnitTestin the UnitTest.php file

(filename is optional)

Page 16: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

phpunit ArrayTestPHPUnit 3.3.0 by Sebastian Bergmann.

..

Time: 0 seconds

OK (2 tests)

Page 17: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

Can output test logs toa variety of formats:

GraphVizJSONTAPXML

Page 18: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

Test suite+

Exported results

allows forContinuous Integration

Page 19: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

Continuous integrationis a way to automatically

run your test suiteregularly

Once a day...

Once an hour...

Every time someone checks in code...

Page 20: PHPUnit Automated Unit Testing Framework

Dave Ross | West Suburban Chicago PHP Meetup | suburbanchicagophp.org

Official PHPUnit page:http://www.phpunit.de/

PHPUnit Pocket Guide:http://www.phpunit.de/pocket_guide

Effective Unit Testing:http://www.acm.org/ubiquity/views/t_burns_1.html

For More Information