Testování v PHP

Post on 17-May-2015

943 views 3 download

description

Úvod do testování aplikací v PHP.

transcript

Testování v PHPOndřej Mirtesondrej@mirtes.cz

Proč psát testy?

• Nedílná součást vývoje• Stálost• Rychle odhalí chyby• Refaktoring

„Testy někdy dopíšu.“

100% code coverage?

Dobrý návrh aplikace

• Opravdové objekty (OOP)• Minimum statických metod a proměnných• Dependency Injection

Dependency Injection

public function __construct(){ $this->db = DbConnection::getInstance();}

Dependency Injection

public function __construct(IDbConnection $db){ $this->db = $db;}

PHPUnit

Instalace

$ sudo pear channel-discover pear.phpunit.de$ sudo pear install phpunit/PHPUnit$ phpunit –-versionPHPUnit 3.5.2 by Sebastian Bergmann.

Bootstrap

/** načtení oblíbeného frameworku */require_once(__DIR__ . '/../libs/Nette/loader.php');

/** abstraktní třída jednotlivých testů */require_once(__DIR__ . '/TestCase.php');

/** další nastavení... */

/myapp/tests/bootstrap.php

phpunit.xml

<phpunitbootstrap="bootstrap.php"colors="true"backupGlobals="false"

/>

/myapp/tests/phpunit.xml

TestCase

abstract class TestCase extends PHPUnit_Framework_TestCase{

}

/myapp/tests/TestCase.php

První test

class StackTest extends TestCase{

public function testInitialStackIsEmpty() { $stack = new Stack; $this->assertEquals(0, count($stack->items)); }

}

/myapp/tests/Stack/StackTest.php

Implementace

class Stack{

private $items = array();

public function getItems() { return $this->items; }

}

/myapp/Stack/Stack.php

Spuštění testů

$ phpunit .PHPUnit 3.5.2 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 4.00Mb

OK (1 test, 1 assertion)

/myapp/tests/

Spuštění testů

$ phpunit –-testdox .

StackTest [x] Initial stack is empty

/myapp/tests/

Vložení prvku

public function testPushedItemIsInTheStack(){ $stack = new Stack; $stack->push(5); $this->assertContains(5, $stack->getItems());}

/myapp/tests/Stack/StackTest.php

Vložení prvku

$ phpunit –-testdox .

StackTest [x] Initial stack is empty [ ] Pushed item is in the stack

/myapp/tests/

Vložení prvku

public function push($item){ $this->items[] = $item;}

/myapp/Stack/Stack.php

Vložení prvku

$ phpunit –-testdox .

StackTest [x] Initial stack is empty [x] Pushed item is in the stack

/myapp/tests/

Výběr prvku

public function testPoppedItemIsReturned(){ $stack = new Stack; $stack->push(5); $this->assertEquals(5, $stack->pop()); $this->assertNotContains(5, $stack->getItems());}

/myapp/tests/Stack/StackTest.php

Podtečení zásobníku

/** * @expectedException StackUnderflowException */public function testStackCannotBeUnderflowed(){ $stack = new Stack; $stack->pop();}

/myapp/tests/Stack/StackTest.php

Další asserty

• assertNull• assertSame• assertGreaterThan• assertType• assertInstanceOf• assertRegexp• assertThat

setUp() & tearDown()class StackTest extends TestCase{

private $stack;

protected function setUp() { $this->stack = new Stack; }

public function testInitialStackIsEmpty() { $this->assertEquals(0, count($this->stack->getItems())); }

public function testPushedItemIsInTheStack() { $this->stack->push(5); $this->assertContains(5, $this->stack->getItems()); }

}

Další vlastnosti

• markTestSkipped()• getMock()• --coverage-html• Propojení s Continuous Integration

Otázky?

Díky za pozornost!