premier pas avec les tests unitaires

Post on 28-Jun-2015

417 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Cours d'introduction aux tests unitaires donné à Polytech'Tours.

TRANSCRIPT

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com1

Tests unitairesApproche du développement piloté par les tests

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com

Cedric Gatay

• EPU DI 2008

• Orange

• SRMvision

@Cedric_Gatay

cedric@gatay.fr

cedric.gatay.fr/+

2

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com

Agenda

• Pourquoi ?

• Quels outils ?

• Comment ?

• Quelques bonnes pratiques

3

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com4

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com

Pourquoi ?

• Le code parfait n’existe pas

• Il est difficile de répondre parfaitement à un besoin

• On cherche à réduire les coûts

Les tests unitaires font partie du quotidien des développeurs

5

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com6

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com

Quels effets ?

• Le code devient

• Explicité

• Organisé

• Assuré

7

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com8

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com

Des tests ? Mais lesquels ?

• Deux grands types de tests

• boîte noire

• boîte blanche

9

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com10

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com

Tests : Black box

• Tests d’acceptation

• vérification du point de vue utilisateur

• également appelés recette utilisateur

• automatisable par des tests d’IHM

• long et lourd à mettre en place

11

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com12

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com

Tests : Black box

• Tests fonctionnel

• vérification d’un pan fonctionnel

• respect du cahier des charges

• assuré par l’équipe qualité

• automatisable par des tests d’IHM

• long et lourd à mettre en place

13

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com14

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com

Tests : White box

• Tests d’intégration

• validation des algorithmes in situ

• utilisation d’éléments externes

• automatisable en écrivant du code

• simple à mettre en place

• potentiellement long à exécuter

15

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com16

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com

Tests : White box

• Tests unitaires

• validation des algorithmes

• limité au développement en cours

• automatisable en écrivant du code

• simple à mettre en place

• extrêmement rapide à exécuter

17

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com18

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com

Comment faire ?

• Test Black box

• réalisation manuelle

• outils de simulation dans l’IHM

• entrées et sorties vérifiées

19

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com

Comment faire ?

• Test White box

• réalisation automatique

• framework d’écritures de tests

• on produit du code pour tester le code

20

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com21

Test

Fail

Code

PassRefactor

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com

JUnit

• Test White box

• Créé par Kent Beck et Erich Gamma

• réalisation automatique

• vérification sur des postulats simples

• énormément utilisé dans l’industrie

22

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com23

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com

JUnit, exemple

Est ce que la moyenne de 5 et 10 retourne 7,5 ?

24

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com25

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com26

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com26

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com26

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com26

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com26

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com26

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com26

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com26

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com26

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com27

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com

JUnit

• La moyenne d'aucune note doit retourner -1

• Il doit être impossible d'ajouter une note négative!

• Il doit être impossible d'ajouter une note supérieure à 20!

• Il doit être possible d'ajouter 16,5

28

Pulling ITSM together Cedric Gatay - c.gatay@srmvision.com

JUnit

• Le calcul de moyenne sur aucune note doit lever une PasDeNoteException !

• L’ajout d’une note négative doit lever une NoteNegativeException !

29

top related