Introduzione ai test automatici con PHPunit

Preview:

Citation preview

1,2,Test

Test Manuali

TEST AUTOMATICI

Sebastian Bergman

• PHPUNIT.Phar

• PEAR

• Composer

Installazione

http://phpunit.de/manual/3.7/en/installation.html

http://getcomposer.org/

...{ "require-dev": { ... "phpunit/phpunit": "3.7.*" }, ...

}...

./composer.json

Configurazione

./phpunit.xml

http://phpunit.de/manual/3.7/en/appendixes.configuration.html

<?xml version="1.0" encoding="UTF-8"?><phpunit colors="true"> <testsuites> <testsuite name="Application Test Suite"> <directory>./Acme/Tests</directory> </testsuite> </testsuites></phpunit>

./phpunit.xml

http://phpunit.de/manual/3.7/en/appendixes.configuration.html

<?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>

./phpunit.xml

http://phpunit.de/manual/3.7/en/appendixes.configuration.html

<?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>

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

<?phpclass TrueTest extends PHPUnit_Framework_TestCase{    public function testTrue()    {        $this->assertTrue(true);    }}?>

AssertEquals

<?phpclass EqualsTest extends PHPUnit_Framework_TestCase{    public function testEquals()    {        $this->assertEquals('uguale', 'uguale');    }}?>

AssertEquals

<?phpclass EqualsTest extends PHPUnit_Framework_TestCase{    public function testObjectsAreEqual()    {        $this->assertEquals(new stdClass,

new stdClass);    }}?>

AssertCount

<?phpclass CountTest extends PHPUnit_Framework_TestCase{    public function testCount()    {        $this->assertCount(3,array(1,2,3));    }}?>

AssertContains

<?phpclass ContainsTest extends PHPUnit_Framework_TestCase{    public function testContainsOK()    {        $this->assertContains('cerca', 

'chi cerca trova');    }}?>

AssertContains

<?phpclass ContainsTest extends PHPUnit_Framework_TestCase{    public function testArrayContains()    {        $this->assertContains(‘ragno’,

array(‘ragno’,‘nel’,‘buco’));    }}?>

ORGANIZZAZIONE

http://phpunit.de/manual/3.7/en/organizing-tests.html

Tipologie

UNIT

Unit Test<?phpclass MyClass{    public function hello()    {        return ‘Ciao!’;    }}class MyClassTest extends PHPUnit_Framework_TestCase{    public function testHello()    {

$class = new MyClass();        $this->assertEquals(‘Ciao!’, $class->hello());    }}

Integration

Integration Test<?phpnamespace Acme\StoreBundle\Tests\Entity;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

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<?phprequire_once 'PHPUnit/Extensions/SeleniumTestCase.php'; class WebTest extends PHPUnit_Extensions_SeleniumTestCase{    protected function setUp()    {        $this->setBrowser('*firefox');        $this->setBrowserUrl('http://php.net/');    }     public function testTitle()    {        $this->open('http://php.net/');        $this->assertTitle('PHP: Hypertext Preprocessor');    }}

Functional Test<?php

namespace Acme\DemoBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

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

OUTPUT legenda

http://phpunit.de/manual/3.7/en/textui.html

.Eseguito con successoFIl test è fallitoEErrore nell’esecuzioneSSaltato (Skipped)INon completo

comandi utili

http://phpunit.de/manual/current/en/textui.html

--verboseMostra più informazioni, come il nome dei test saltati o incompleti

--debugMostra più informazioni, come il nome del test in esecuzione

--stop-on-errorBlocca il processo al primo errore

--stop-on-failureBlocca il processo al primo fallimento di un test

--filter [nome]Processa solo i test che contengono nel nome il termine passato

Test-Driven Development

VANTAGGI

• Affidabile

• StabilE

• LeggibilE

• Cooperativo

• Sviluppo rapido

?

{ ... "require-dev": {

... "whatthejeff/nyancat-phpunit-resultprinter": "~1.1" }

...}

composer.json

phpunit.xml<phpunit ...

printerFile="vendor/whatthejeff/nyancat-phpunit-resultprinter/src/NyanCat/PHPUnit/ResultPrinter.php"printerClass="NyanCat\PHPunit\ResultPrinter">...

https://github.com/whatthejeff/nyancat-phpunit-resultprinter

Recommended