PHPUnit - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

PHPUnit

Description:

Les tests sont ex cut s des centaines de fois chaque jour pour valider que tout ... Appelle des m thodes assertXXX. S bastien Hordeaux - Forum PHP 10/11 ... – PowerPoint PPT presentation

Number of Views:512
Avg rating:3.0/5.0
Slides: 30
Provided by: sbastien2
Category:
Tags: appelle | phpunit

less

Transcript and Presenter's Notes

Title: PHPUnit


1
PHPUnit
  • Faire du développement agile un atout compétitif
  • Sébastien Hordeaux - WaterProof
  • shordeaux_at_waterproof.fr

2
Tests?
  • Régression / Unitaires
  • Recette / Acceptation
  • Charge / Performances
  • Ergonomie
  • Sécurité (Chorizo!)

3
PHPUnit
  • Réutilisable
  • Séparation Test/Production
  • Automatisation de lexécution
  • Analyse des résultats
  • Tests facile à écrire

4
Comment ca marche?
  • Cinétique
  • Ecriture dun test
  • Exécution des tests
  • Modification dans lapplication
  • Exécution des tests
  • Les tests sont exécutés des centaines de fois
    chaque jour pour valider que tout fonctionne bien
    à tout moment.

5
Installation
  • pear channel-discover pear.phpunit.de
  • pear install phpunit/PHPUnit-beta
  • pear install channel//pear.phpunit.de/PHPUnit-3.0
    .0RC1

6
Structure dun test unitaire
  • ltClassgtTest
  • extends PHPUnit_Framework_TestCase
  • Méthodes testltMethodNamegt
  • Appelle des méthodes assertXXX

7
TestCase
require_once 'PHPUnit/Framework.php' class
ArrayTest extends PHPUnit_Framework_TestCase
public function testNewArrayIsEmpty() //
Create the Array fixture. fixture
array() // Assert that the size of the
Array fixture is 0. this-gtassertEquals(0,
sizeof(fixture))
8
Exécution de tests unitaires
  • Ligne de commande
  • Interface web
  • Environnement de développement
  • PHP Eclipse
  • PHPEdit 2.14 (Janvier 2007)

9
Gestion du contexte (1/3)
  • Méthodes spécifiques
  • setUp
  • tearDown
  • Eviter les duplications de code

10
Gestion du contexte (2/3)
require_once 'PHPUnit/Framework.php' class
ArrayTest extends PHPUnit_Framework_TestCase
public function testNewArrayIsEmpty() //
Create the Array fixture. fixture
array() // Assert that the size of the
Array fixture is 0. this-gtassertEquals(0,
sizeof(fixture)) public function
testArrayContainsAnElement() // Create
the Array fixture. fixture array()
// Add an element to the Array fixture.
fixture 'Element' // Assert
that the size of the Array fixture is 1.
this-gtassertEquals(1, sizeof(fixture))
11
Gestion du contexte (3/3)
require_once 'PHPUnit/Framework.php' class
ArrayTest extends PHPUnit_Framework_TestCase
protected fixture protected function
setUp() // Create the Array
fixture. this-gtfixture array()
public function testNewArrayIsEmpty()
// Assert that the size of the Array
fixture is 0. this-gtassertEquals(0,
sizeof(this-gtfixture)) public
function testArrayContainsAnElement()
// Add an element to the Array fixture.
this-gtfixture 'Element' // Assert
that the size of the Array fixture is 1.
this-gtassertEquals(1, sizeof(this-gtfixture))

12
Mock Objects (1/3)
  • Tester que le comportement attendu va bien se
    produire

13
Mock Objects (2/3)
require_once 'PHPUnit/Framework.php' class
ObserverTest extends PHPUnit_Framework_TestCase
public function testUpdateIsCalledOnce()
// Create a Mock Object for the Observer
class // mocking only the update()
method. observer this-gtgetMock('Observ
er', array('update')) // Set up the
expectation for the update() method // to
be called only once and with the string
'something' // as its parameter.
observer-gtexpects(this-gtonce())
-gtmethod('update')
-gtwith(this-gtequalTo('something') )
// Create a Subject object and attach the
mocked // Observer object to it.
subject new Subject
subject-gtattach(observer) // Call
the doSomething() method on the subject object
// which we expect to call the mocked
Observer object's // update() method with
the string 'something'.
subject-gtdoSomething()
14
Mock Objects (3/3)
  • never()
  • atLeastOnce()
  • once()
  • exactly(int count)
  • at(int index)
  • anything()
  • arrayContains(mixed value)
  • arrayHasKey(mixed key)
  • equalTo(mixed value)
  • fileExists()
  • greaterThan(mixed value)
  • hasProperty(string property)
  • identicalTo(mixed value)
  • isInstanceOf(string className)
  • isType(string type)
  • lessThan(mixed value)
  • matchesRegularExpression(string pattern)
  • stringContains(string string, bool case)

15
Suites
  • Package 1
  • Class 1.1 ? Class11Test
  • Class 1.2 ? Class12Test
  • Package 2
  • Class 2.1 ? Class21Test
  • Class 2.2 ? Class22Test

16
Suites
require_once('PHPUnit/Framework/TestSuite.php') r
equire_once('Application/Tests/Package1/AllTests.p
hp') class AllTests public static function
suite() suite new
PHPUnit_Framework_TestSuite('Project')
suite-gtaddTest(Package1_AllTestssuite())
return suite
require_once('PHPUnit/Framework/TestSuite.php') r
equire_once('Application/Tests/Package1/Class11Tes
t.php') class Package_AllTests public
static function suite() suite
new PHPUnit_Framework_TestSuite('Package1')
suite-gtaddTestSuite('Package1_Class11Test')
return suite
17
Tests incomplets
  • ! correct
  • ! incorrect
  • cas particulier, traité comme tel

require_once 'PHPUnit/Framework.php' class
SampleTest extends PHPUnit_Framework_TestCase
public function testSomething()
// Optional Test anything here, if you want.
this-gtassertTrue(true, 'This should already
work.') // Stop here and mark this test
as incomplete. this-gtmarkTestIncomplete(N
ot yet implementedet.')
18
Tests à ne pas effectuer (Skip)
  • Quand les conditions nécessaires au test ne sont
    pas réunies, permet dêtre traité comme un cas
    spécifique
  • Plateforme spécifique (Win/Linux/Mac)
  • Extensions PHP manquante

require_once 'PHPUnit/Framework.php' class
DatabaseTest extends PHPUnit_Framework_TestCase
protected function setUp() if
(!extension_loaded('mysqli'))
this-gtmarkTestSkipped('MySQLi ext. is not
available.') public
function testConnection()
19
Génération de code
  • Génération de TestCase à partir de classe PHP
    existante
  • phpunit --skeleton Foo

20
Génération de documentation
  • Génération de documentation à partir des noms des
    méthodes de test (Wiki style)
  • phpunit testdox-html Foo.html Foo
  • phpunit testdox-txt Foo.text Foo

21
Emails (Fakemail)
  • Permet de tester le contenu des emails
  • Agit comme un serveur SMTP
  • Sauve les emails dans des fichiers textes au lieu
    de les envoyer
  • http//www.lastcraft.com/fakemail.php

22
Code coverage (XDebug)
  • Quelle proportion du code est testée?
  • Donne des outils visuels pour savoir où les
    lacunes sont

23
Tests Web (Selenium RC)
  • Selenium Remote Control permet de créer des tests
    automatiques dapplications web via un browser
    avec javascript (IE/FF)
  • Plus dinformations sur Selenium RC
    http//www.openqa.org/selenium-rc/

24
Quand tester?
  • Pendant le développement
  • Valider que tout fonctionne bien avant et après
    vos modifications (Refactoring)
  • Pendant le debuggage (Non régression)
  • Trouver le bug
  • Isoler son plus petit moyen de reproduction
  • Créer un test permettant de détecter le bug
  • Correction du bug

25
Cas concret eZ systems
  • Nouveaux composants/fonctionalités
  • Ecriture des tests
  • Ecriture du code
  • Commit
  • Bugs
  • Obligation davoir un rapport dans wIT
  • Ecriture du test
  • Correction du bug
  • Commit

26
Qui lutilise?
  • eZ systems
  • 2.500 tests dans les eZ components (87.84)
  • Zend
  • 400 tests dans le Zend Framework
  • Beaucoup dautres http//www.phpunit.de/wiki/WhoU
    sesPHPUnit
  • Nous
  • Vous ?

27
Pour en savoir plus
  • Le site de référence http//www.phpunit.de/
  • Documentation http//www.phpunit.de/pocket_guide/

28
Remerciements
  • Sebastian Bergman
  • Auteur de la librairie
  • Auteur du Pocket book sous licence libre

29
Merci de votre attentionQuestions ?
  • Retrouvez nos solutions sur http//www.waterproof.
    fr
  • PHPEdit wIT
Write a Comment
User Comments (0)
About PowerShow.com