Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduzione ai test automatici con PHPunit

Introduzione ai test automatici con PHPunit

Come evitare i test manuali e iniziare a testare da subito in modo automatico il proprio codice PHP.

Slide sessione PUG Torino del 25 Settembre 2013

Fabio Giannese

September 25, 2013

Transcript

47 slides
  1. ./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>
  2. 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()
  3. 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()
  4. AssertContains <?php class ContainsTest extends PHPUnit_Framework_TestCase { public function testArrayContains()

    { $this->assertContains(‘ragno’, array(‘ragno’,‘nel’,‘buco’)); } } ?>
  5. Unit Test <?php class MyClass { public function hello() {

    return ‘Ciao!’; } } class MyClassTest extends PHPUnit_Framework_TestCase { public function testHello() { $class = new MyClass(); $this->assertEquals(‘Ciao!’, $class->hello()); } }
  6. Integration Test <?php namespace 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() ; } ...
  7. 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(); } }
  8. Functional Test <?php require_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'); } }
  9. 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() ); } }
  10. OUTPUT legenda http://phpunit.de/manual/3.7/en/textui.html . Eseguito con successo F Il test

    è fallito E Errore nell’esecuzione S Saltato (Skipped) I Non completo
  11. comandi utili http://phpunit.de/manual/current/en/textui.html --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 --filter [nome] Processa solo i test che contengono nel nome il termine passato
  12. ?

  13. { ... "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

More Decks by Fabio Giannese

Other Decks in Programming