際際滷

際際滷Share a Scribd company logo
1,2,Test
Test Manuali
Introduzione ai test automatici con PHPunit
Introduzione ai test automatici con PHPunit
TEST AUTOMATICI
Introduzione ai test automatici con PHPunit
Sebastian Bergman
http://phpunit.de/manual/current/en/index.html
Installazione
 PHPUNIT.Phar
 PEAR
 Composer

http://phpunit.de/manual/3.7/en/installation.html
http://getcomposer.org/
./composer.json
...
{
"require-dev": {
...
"phpunit/phpunit": "3.7.*"
},
...
}
...
Introduzione ai test automatici con PHPunit
Introduzione ai test automatici con PHPunit
Configurazione
./phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./Acme/Tests</directory>
</testsuite>
</testsuites>
</phpunit>

http://phpunit.de/manual/3.7/en/appendixes.con鍖guration.html
./phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./Acme/Tests</directory>
<directory>./src/*/Tests</directory>
</testsuite>
</testsuites>
</phpunit>

http://phpunit.de/manual/3.7/en/appendixes.con鍖guration.html
./phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true"
bootstrap="./path/to/bootstrap.php">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./Acme/Tests</directory>
</testsuite>
</testsuites>
</phpunit>

http://phpunit.de/manual/3.7/en/appendixes.con鍖guration.html
Assert*
assertArrayHasKey()
assertClassHasAttribute()
assertClassHasStaticAttribute()
assertContains()
assertContainsOnly()
assertContainsOnlyInstancesOf()
assertCount()
assertEmpty()
assertEqualXMLStructure()
assertEquals()
assertFalse()
assertFileEquals()
assertFileExists()
assertGreaterThan()
assertGreaterThanOrEqual()
assertInstanceOf()
assertInternalType()
assertJsonFileEqualsJsonFile()
assertJsonStringEqualsJsonFile()
assertJsonStringEqualsJsonString()

assertLessThan()
assertLessThanOrEqual()
assertNull()
assertObjectHasAttribute()
assertRegExp()
assertStringMatchesFormat()
assertStringMatchesFormatFile()
assertSame()
assertSelectCount()
assertSelectEquals()
assertSelectRegExp()
assertStringEndsWith()
assertStringEqualsFile()
assertStringStartsWith()
assertTag()
assertThat()
assertTrue()
assertXmlFileEqualsXmlFile()
assertXmlStringEqualsXmlFile()
assertXmlStringEqualsXmlString()
Assert*
assertArrayHasKey()
assertClassHasAttribute()
assertClassHasStaticAttribute()
assertContains()
assertContainsOnly()
assertContainsOnlyInstancesOf()
assertCount()
assertEmpty()
assertEqualXMLStructure()
assertEquals()
assertFalse()
assertFileEquals()
assertFileExists()
assertGreaterThan()
assertGreaterThanOrEqual()
assertInstanceOf()
assertInternalType()
assertJsonFileEqualsJsonFile()
assertJsonStringEqualsJsonFile()
assertJsonStringEqualsJsonString()

assertLessThan()
assertLessThanOrEqual()
assertNull()
assertObjectHasAttribute()
assertRegExp()
assertStringMatchesFormat()
assertStringMatchesFormatFile()
assertSame()
assertSelectCount()
assertSelectEquals()
assertSelectRegExp()
assertStringEndsWith()
assertStringEqualsFile()
assertStringStartsWith()
assertTag()
assertThat()
assertTrue()
assertXmlFileEqualsXmlFile()
assertXmlStringEqualsXmlFile()
assertXmlStringEqualsXmlString()
AssertTrue

<?php
classTrueTestextendsPHPUnit_Framework_TestCase
{
publicfunctiontestTrue()
{
$this->assertTrue(true);
}
}
?>
AssertEquals

<?php
classEqualsTestextendsPHPUnit_Framework_TestCase
{
publicfunctiontestEquals()
{
$this->assertEquals('uguale', 'uguale');
}
}
?>
AssertEquals
<?php
classEqualsTestextendsPHPUnit_Framework_TestCase
{
publicfunctiontestObjectsAreEqual()
{
$this->assertEquals(newstdClass,
newstdClass);
}
}
?>
AssertCount

<?php
classCountTestextendsPHPUnit_Framework_TestCase
{
publicfunctiontestCount()
{
$this->assertCount(3,array(1,2,3));
}
}
?>
AssertContains
<?php
classContainsTestextendsPHPUnit_Framework_TestCase
{
publicfunctiontestContainsOK()
{
$this->assertContains('cerca',
'chi cerca trova');
}
}
?>
AssertContains

<?php
classContainsTestextendsPHPUnit_Framework_TestCase
{
publicfunctiontestArrayContains()
{
$this->assertContains(ragno,
array(ragno,nel,buco));
}
}
?>
ORGANIZZAZIONE

http://phpunit.de/manual/3.7/en/organizing-tests.html
Tipologie
UNIT
Unit Test
<?php
classMyClass
{
publicfunctionhello()
{
return Ciao!;
}
}
classMyClassTestextendsPHPUnit_Framework_TestCase
{
publicfunctiontestHello()
{
$class = newMyClass();
$this->assertEquals(Ciao!, $class->hello());
}
}
Integration
Integration Test
<?php
namespace AcmeStoreBundleTestsEntity;
use SymfonyBundleFrameworkBundleTestWebTestCase;
class ProductRepositoryFunctionalTest extends WebTestCase
{
private $em;
public function setUp()
{
static::$kernel = static::createKernel();
static::$kernel->boot();
$this->em = static::$kernel->getContainer()
->get('doctrine')
->getManager()
;
} ...
Integration Test
...
public function testSearchByCategoryName()
{
$products = $this->em
->getRepository('AcmeStoreBundle:Product')
->searchByCategoryName('foo')
;
$this->assertCount(1, $products);
}

protected function tearDown()
{
parent::tearDown();
$this->em->close();
}
}
Functional
Functional Test
<?php
require_once'PHPUnit/Extensions/SeleniumTestCase.php';

classWebTestextendsPHPUnit_Extensions_SeleniumTestCase
{
protectedfunctionsetUp()
{
$this->setBrowser('*firefox');
$this->setBrowserUrl('http://php.net/');
}

publicfunctiontestTitle()
{
$this->open('http://php.net/');
$this->assertTitle('PHP: Hypertext Preprocessor');
}
}
Functional Test
<?php
namespace AcmeDemoBundleTestsController;
use SymfonyBundleFrameworkBundleTestWebTestCase;
class DemoControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$crawler = $client->request('GET', '/demo/hello/Fabio');
$this->assertGreaterThan(
0,
$crawler->filter('html:contains("Hello Fabio")')
->count() );
}
}
in azione
Introduzione ai test automatici con PHPunit
OUTPUT legenda
.
Eseguito con successo
F
Il test 竪 fallito
E
Errore nellesecuzione
S
Saltato (Skipped)
I
Non completo

http://phpunit.de/manual/3.7/en/textui.html
Introduzione ai test automatici con PHPunit
Introduzione ai test automatici con PHPunit
comandi utili
--verbose
Mostra pi湛 informazioni, come il nome dei test saltati o incompleti
--debug
Mostra pi湛 informazioni, come il nome del test in esecuzione
--stop-on-error
Blocca il processo al primo errore
--stop-on-failure
Blocca il processo al primo fallimento di un test
--鍖lter [nome]
Processa solo i test che contengono nel nome il termine passato

http://phpunit.de/manual/current/en/textui.html
Test-Driven Development
VANTAGGI
 Affidabile
 StabilE
 LeggibilE
 Cooperativo
 Sviluppo rapido
?
Introduzione ai test automatici con PHPunit
composer.json
{
...
"require-dev": {
...
"whatthejeff/nyancat-phpunit-resultprinter": "~1.1"
}
...
}

phpunit.xml
<phpunit ...
printerFile="vendor/whatthejeff/nyancat-phpunitresultprinter/src/NyanCat/PHPUnit/ResultPrinter.php"
printerClass="NyanCatPHPunitResultPrinter">
...

https://github.com/whatthejeff/nyancat-phpunit-resultprinter
Introduzione ai test automatici con PHPunit

More Related Content

Introduzione ai test automatici con PHPunit