27. TDD Mantra Primo step Think Think : ragionare per piccoli passi Focalizzarsi sul comportamento che il codice deve avere.
28. TDD Mantra Think “ Vogliamo sviluppare una libreria aritmetica che agisca solo su numeri non negativi” Esempio aritLib.py
29.
30. TDD Mantra Secondo step Think Red Bar : scrittura dei test. Focalizzarsi sul comportamento della classe e la sua interfaccia pubblica. Red bar
31. TDD Mantra Secondo step Think import aritLib import unittest class AritLibTest ( unittest.TestCase ): knownValues = ((0, 0, 0), (1, 1, 2), (2, 3, 5), (-1, -1, -1), (-10, 10, -1),(10, -5, -1), ) def testAdd ( self ): for x, y, sum in self .knownValues: result = aritLib.add(x, y) self .assertEquals(sum, result) Red bar
32. TDD Mantra Secondo step Think ERROR : testAdd (__main__.AritLibTest) ---------------------------------------------------------------------- Traceback (most recent call last): File “AritLibTest.py”, line 11, in testAdd result = aritLib.add(x,y) AttributeError : 'module' object has no attribute 'add' ---------------------------------------------------------------------- Ran 1 test in 0.000s FAILED (errors=1) Red bar class AritLibTest ( unittest.TestCase ): knownValues = ((0, 0, 0), (1, 1, 2), (2, 3, 5), (-1, -1, -1), (-10, 10, -1),(10, -5, -1), ) def testAdd ( self ): for x, y, sum in self .knownValues: result = aritLib.add(x, y) self .assertEquals(sum, result) IL TEST FALLISCE PERCHE’ LA FUNZIONE ANCORA NON ESISTE!
33. TDD Mantra Terzo step Think Red bar Green Bar Test fallito Green Bar : scrittura del codice. Scrivere solo il codice necessario per superare i test scritti in precedenza.
34. TDD Mantra Terzo step Think Red bar Green Bar Test fallito aritLib.py def add (x, y): if x < 0: return -1 if y < 0: return -1 return x+y
35. TDD Mantra Terzo step Think Red bar Green Bar Test fallito aritLib.py def add (x, y): if x < 0: return -1 if y < 0: return -1 return x+y ---------------------------------------------------------------------- Ran 1 test in 0.000 s ---------------------------------------------------------------------- OK Vengono lanciati i test scritti in precedenza senza modificarli.
36. TDD Mantra Quarto step Think Red bar Green Bar Test fallito Refactoring Refactoring : semplificare ed aggiustare il desing della funzionalità sviluppata. In questa fase non bisogna assolutamente modificare la semantica della funzionalità implementata!
37. TDD Mantra Quarto step Think Red bar Green Bar Test fallito Prima def add (x, y): if x < 0: return -1 if y < 0: return -1 return x+y Refactoring Dopo def add (x, y): if x < 0 or y < 0: return -1 return x+y
38. TDD Mantra Quarto step Think Red bar Green Bar Test fallito Refactoring Dopo def add (x, y): if x < 0 or y < 0: return -1 return x+y ---------------------------------------------------------------------- Ran 1 test in 0.000 s ---------------------------------------------------------------------- OK Vengono lanciati i test scritti in precedenza senza modificarli.