php unit (eng)

16
PHPUnit What is Unit Testing? Benefits What is PHPUnit? Installation The Bank Account Example Categories of (Unit) Tests / Software Testing Pyramid Links

Upload: anatoliy-okhotnikov

Post on 01-Sep-2014

1.078 views

Category:

Technology


0 download

DESCRIPTION

● What is Unit Testing? ● Benefits ● What is PHPUnit? ● Installation ● The Bank Account Example ● Categories of (Unit) Tests / Software Testing Pyramid ● Links

TRANSCRIPT

Page 1: Php unit (eng)

PHPUnit

● What is Unit Testing?● Benefits● What is PHPUnit?● Installation● The Bank Account Example● Categories of (Unit) Tests / Software Testing

Pyramid● Links

Page 2: Php unit (eng)

What is Unit Testing?

● In computer programming, unit testing is a method by which individual units of source code are tested to determine if they are fit for use. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual function or procedure. Unit tests are created by programmers or occasionally by white box testers.

● Ideally, each test case is independent from the others: substitutes like method stubs, mock objects, fakes and test harnesses can be used to assist testing a module in isolation. Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended. Its implementation can vary from being very manual (pencil and paper) to being formalized as part of build automation

Page 3: Php unit (eng)

Benefits

The goal of unit testing is to isolate each part of the program and show that the individual parts are correct. A unit test provides a strict, written contract that the piece of code must satisfy. As a result, it affords several benefits. Unit tests find problems early in the development cycle.

● Facilitates change (refactor, regression, etc.)● Simplifies integration (parts, sum & integration)● Documentation (live, understand API)● Design (TDD, no formal design, design element)

Page 4: Php unit (eng)

What is PHPUnit?

● PHPUnit is the de-facto standard for unit testing in PHP projects. It provides both a framework that makes the writing of tests easy as well as the functionality to easily run the tests and analyse their results.

● Part of xUnit family, created by Sebastian Bergmann, integrated & supported by Zend Studio/Framework.

● Simple installation with PEAR

Page 5: Php unit (eng)

Installation

● PHPUnit should be installed using the PEAR Installer. This installer is the backbone of PEAR, which provides a distribution system for PHP packages, and is shipped with every release of PHP since version 4.3.0.

● The PEAR channel (pear.phpunit.de) that is used to distribute PHPUnit needs to be registered with the local PEAR environment. Furthermore, components that PHPUnit depends upon are hosted on additional PEAR channels.

● This has to be done only once. Now the PEAR Installer can be used to install packages from the PHPUnit channel:

pear channel-discover pear.phpunit.depear channel-discover components.ez.nopear channel-discover pear.symfony-project.com

● After the installation you can find the PHPUnit source files inside your local PEAR directory; the path is usually /usr/lib/php/PHPUnit.

pear install phpunit/PHPUnit

Page 6: Php unit (eng)

The Bank Account Example<?phpclass BankAccount{

protected $balance = 0;

public function getBalance() {

return $this->balance;}

protected function setBalance($balance) {

if ($balance>=0) {$this->balance = $balance;

} else {throw new BankAccountException;

}}

public function depositMoney($balance){

$this->setBalance($this->getBalance() + $balance);return $this->getBalance();

}

public function withdrawMoney($balance){

$this->setBalance($this->getBalance() - $balance);return $this->getBalance();

}}

Page 7: Php unit (eng)

Bank Account Test

<?phprequire_once 'BankAccount.php';

class BankAccountTest extends PHPUnit_Framework_TestCase{

public function testBalanceIsInitiallyZero(){

$ba = new BankAccount;$this->assertEquals(0, $ba->getBalance());

}}

Page 8: Php unit (eng)

Running the Test(s)

Test can server as an executable specification.

Page 9: Php unit (eng)

The setUp() template method

<?phprequire_once 'BankAccount.php';

class BankAccountTest extends PHPUnit_Framework_TestCase{

protected $ba;

public function setUp(){

$this->ba = new BankAccount}

public function testBalanceIsInitiallyZero(){

$this->assertEquals(0, $this->ba->getBalance());}

}

Page 10: Php unit (eng)

More Tests...

<?phprequire_once 'BankAccount.php';

class BankAccountTest extends PHPUnit_Framework_TestCase{

// ...public function testBalanceCannotBecomeNegative(){

try { $this->ba->withdrawMoney(1); } catch (BankAccountException $e) { $this->assertEquals(0, $this->ba->getBalance()); return; } $this->fail();

}}

Page 11: Php unit (eng)

Even More Tests...

<?phprequire_once 'BankAccount.php';

class BankAccountTest extends PHPUnit_Framework_TestCase{

// ...public function testBalanceCannotBecomeNegative2(){

try { $this->ba->withdrawMoney(-1); } catch (BankAccountException $e) { $this->assertEquals(0, $this->ba->getBalance()); return; } $this->fail();

}}

Page 12: Php unit (eng)

More and More Tests...

<?phprequire_once 'BankAccount.php';

class BankAccountTest extends PHPUnit_Framework_TestCase{

// ...public function testDepositingAndWithdrawingMoneyWorks(){

$this->assertEquals(0, $this->ba->getBalance()); $this->ba->depositMoney(1); $this->assertEquals(1, $this->ba->getBalance()); $this->ba->withdrawMoney(1); $this->assertEquals(0, $this->ba->getBalance());

}}

Page 13: Php unit (eng)

Live Documentation

Page 14: Php unit (eng)

Categories of (Unit) Tests

● Small: Unit Tests● Check conditional logic in the code● A debugger should not be required in case of failure

● Medium: Functional Tests● Check whether the interfaces between classes

abide by their contracts

● Large: End-to-End Tests● Check for “wiring bugs”

Page 15: Php unit (eng)

Software Testing Pyramid

Page 16: Php unit (eng)

Links

● http://www.phpunit.de/manual/3.6/en/index.html● https://github.com/sebastianbergmann/phpunit/● http://en.wikipedia.org/wiki/Unit_testing● http://www.slideshare.net/sebastian_bergmann/

getting-started-with-phpunit● http://www.slideshare.net/DragonBe/introductio

n-to-unit-testing-with-phpunit-presentation-705447