Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

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

  • Identifiez-vous pour voir les commentaires

  • Soyez le premier à aimer ceci

Introduzione ai test automatici con PHPunit

  1. 1. 1,2,Test
  2. 2. Test Manuali
  3. 3. TEST AUTOMATICI
  4. 4. Sebastian Bergman
  5. 5. http://phpunit.de/manual/current/en/index.html
  6. 6. Installazione • PHPUNIT.Phar • PEAR • Composer http://phpunit.de/manual/3.7/en/installation.html
  7. 7. http://getcomposer.org/
  8. 8. ./composer.json ... { "require-dev": { ... "phpunit/phpunit": "3.7.*" }, ... } ...
  9. 9. Configurazione
  10. 10. ./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.configuration.html
  11. 11. ./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.configuration.html
  12. 12. ./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.configuration.html
  13. 13. 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()
  14. 14. 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()
  15. 15. AssertTrue <?php class TrueTest extends PHPUnit_Framework_TestCase {     public function testTrue()     {         $this->assertTrue(true);     } } ?>
  16. 16. AssertEquals <?php class EqualsTest extends PHPUnit_Framework_TestCase {     public function testEquals()     {         $this->assertEquals('uguale', 'uguale');     } } ?>
  17. 17. AssertEquals <?php class EqualsTest extends PHPUnit_Framework_TestCase {     public function testObjectsAreEqual()     {         $this->assertEquals(new stdClass, new stdClass);     } } ?>
  18. 18. AssertCount <?php class CountTest extends PHPUnit_Framework_TestCase {     public function testCount()     {         $this->assertCount(3,array(1,2,3));     } } ?>
  19. 19. AssertContains <?php class ContainsTest extends PHPUnit_Framework_TestCase {     public function testContainsOK()     {         $this->assertContains('cerca',  'chi cerca trova');     } } ?>
  20. 20. AssertContains <?php class ContainsTest extends PHPUnit_Framework_TestCase {     public function testArrayContains()     {         $this->assertContains(‘ragno’, array(‘ragno’,‘nel’,‘buco’));     } } ?>
  21. 21. ORGANIZZAZIONE http://phpunit.de/manual/3.7/en/organizing-tests.html
  22. 22. Tipologie
  23. 23. UNIT
  24. 24. 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());     } }
  25. 25. Integration
  26. 26. 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()         ;     } ...
  27. 27. 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();     } }
  28. 28. Functional
  29. 29. 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');     } }
  30. 30. 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() );     } }
  31. 31. in azione
  32. 32. OUTPUT legenda . Eseguito con successo F Il test è fallito E Errore nell’esecuzione S Saltato (Skipped) I Non completo http://phpunit.de/manual/3.7/en/textui.html
  33. 33. 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 --filter [nome] Processa solo i test che contengono nel nome il termine passato http://phpunit.de/manual/current/en/textui.html
  34. 34. Test-Driven Development
  35. 35. VANTAGGI • Affidabile • StabilE • LeggibilE • Cooperativo • Sviluppo rapido
  36. 36. ?
  37. 37. 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

×