Test Driven Development in action Corrado Aaron Visaggio visaggio@unisannio.it, Research Centre on Software Technology - RCOST University of Sannio Benevento, Italy Corrado Aaron Visaggio 1 Agile Manifesto L Agile Manifesto (agilemanifesto.org). descrive i principi guida che orientano lo sviluppo degli Agile Methods: Individui ed iterazioni piuttosto che processi e strumenti; Software funzionante piuttosto che documentazione onnicomprensiva. Collaborazione del committente piuttosto che negoziazione del contratto. Rispondere al cambiamento piuttosto che seguire un piano. Corrado Aaron Visaggio 2
Agile Manifesto L Agile Manifesto (agilemanifesto.org). descrive i principi guida che orientano lo sviluppo degli Agile Methods: Individui ed iterazioni piuttosto che processi e strumenti; Software funzionante piuttosto che documentazione onnicomprensiva. Collaborazione del committente Scrum piuttosto che negoziazione del contratto. http://jeffsutherland.org/scrum/ Rispondere al cambiamento piuttosto che seguire un piano. Crystal http://alistair.cockburn.us/crystal/crystal.html Corrado Aaron Visaggio 3 extreme Progamming: the r-evolution XP nasce da un idea di Kent Beck [Beck, 2000]. La sua prima applicazione è stata in Daimler Chrysler, nel progetto C3, un progetto di gestione stipendi. I valori di XP: Comunicazione. Semplicità. Feedback. Coraggio. http:// www.extremeprogramming.org Corrado Aaron Visaggio 4
XP: the 12 practices Planning Game. Pianificazione delle iterazioni successive, tramite gli stand up meeting. Scrittura delle story card. Small releases. Devono essere rilasciate piccole versioni funzionanti del software. Metaphor. Il team di progetto deve avere una visione chiara e precisa degli elementi architetturali e delle relazioni che vi intercorrono. Simple design. Il progetto del sistema deve essere semplice. Corrado Aaron Visaggio 5...XP: the 12 practices... Testing. La codifica deve seguire la scrittura degli unit test. Refactoring [Fowler, 2000]. Pair programming. Collective Ownership. Chiunque può modificare qualunque porzione di codice. Continuous Integration. Integrazione dei prodotti delle singole coppie nella Code Base. 40-Hour week. Corrado Aaron Visaggio 6
...XP: the 12 practices On site customer. Il committente partecipa allo sviluppo del sistema. Coding Standards. La codifica deve seguire standard molto precisi. Corrado Aaron Visaggio 7 XP: il processo Corrado Aaron Visaggio 8
Definition (1/2) The test aspect TDD means that the tests for programming units [in Object Oriented Programming unit means a class method] drive the code writing. The programmer writes the test prior to the code to be tested. The programmer changes the code according to the results of the tests: the code rises from the tests ( results) In usual unit test the programmer writes the code as first and then (writes and) executes the tests. Corrado Aaron Visaggio 9 The driven aspect Definition (2/2) The goal of TDD is write clean code that works: TDD should avoid duplication in the code code is changed only when the test fails. TDD should drives the decisions to make about the design, including interface and code of each method. TDD is not a technique for testing, but for developing. The development aspect TDD is a practice of code development, it is not a process: it can be used in larger processes together with other practices. Tests produced during tdd is relevant part of the product s set and shouldn t be thrown away. Corrado Aaron Visaggio 10
The process of TDD start Write class interfaces Write the tests Execute the tests Write/change the code Go to the next class No Did the tests fail? Yes Corrado Aaron Visaggio 11 The Process of Testing After Coding (TAC) start Write class interfaces Write/change the code Write the tests Execute the tests Drivers+stub Go to the next class Did Yes the tests fail? No Corrado Aaron Visaggio 12
The Example The Use Case Story: to write a program that should calculate the sum and the difference of two integers and should return the results. Corrado Aaron Visaggio 13 TDD: first step Step 1. Let s write the interfaces of the class write a rough version of the methods body, too. public class MathsOperation { int result; public integer add (integer int1; integer int2) {result= int1+int2;} public integer difference (integer int1; integer int2) {result=int1-int2;} public integer getresult() {} } Corrado Aaron Visaggio 14
TDD: second & third step Step 2. Let s write the test code MathsOperation calc= new clalc(); assertequals(4; calc.add(0,4)); assertequals(4; calc.add(4,0)); assertequals(4; calc.add(2,2)); assertequals(4; calc.add(2,2)); assertequals(2*integer.max_value; calc.add(integer.max_value, Integer.MAX_VALUE)); assertequals (4; calc.add(2,2)); assertequals (0; calc.difference(2,2)); assertequals (1; calc.difference(2,3)); //now I d like to have the result of the last sum assertequals(4; calc.getresult()); Step 3. Let s execute the test code look the bar is RED: something went wrong, go to the Corrado Aaron Visaggio 15 code and change it! Corrado Aaron Visaggio 16
TDD: fourth step (1/2) Step 4. Let s improve the code on the basis of the test feedback: assertequals(2*integer.max_value; calc.add(integer.max_value, Integer.MAX_VALUE)); The mistakes is due to the absence of the controls on the maximum value; the solution is to add a control in the method like that: if ((int1== Integer.MAX_VALUE) (int2== Integer.MAX_VALUE)) then System.out.println( Please, enter number smaller than 2 31-1. ); Now, let s go on with tests; the bar is red again... Corrado Aaron Visaggio 17 TDD: fourth step (2/3) assertequals (1; calc.difference(2,3)); The mistakes is due to the absence of a controls on the inputs: the result must be an integer, so the order is important; the solution is to add a control in the method like that: if ((int1 < int2) then result= int2-int1; else int1-int2. The bar is yet RED... Corrado Aaron Visaggio 18
TDD: fourth step (3/3) assertequals(4; calc.getresult()); The mistakes suggests me that I need two variables for results: public class MathsOperation { int sumresult; int differenceresult; public integer add (integer int1; integer int2) {sumresult= int1+int2;} public integer difference (integer int1; integer int2) {differenceresult=int1-int2} public integer getsum() {return sumresult;} public integer getdifference() {return differenceresult;} } Finally the bar is GREEN everything is ok. Corrado Aaron Visaggio 19 TAC: first step First step: in TAC the phase of analysis is more accurate, the code should be written in a more precise way. public class MathsOperation { int sumresult; int differenceresult; public integer add (integer int1; integer int2) {sumresult= int1+int2;} public integer difference (integer int1; integer int2) {differenceresult=int1-int2} public integer getsumresult() {return sumresult;} public integer getdifferenceresult() {return differenceresult;} } Corrado Aaron Visaggio 20
TAC: the process After the code, the test bed should be written. The test bed will be a java class, with a main method and assertequals method. After having executed the tests, the developer should analyse all the test s results and make all the corrections together on the code. On the contrary, with tdd, the corrections were made incrementally. Corrado Aaron Visaggio 21 TAC: the test bed public class TestBed { public static void main(string[] args) { MathsOperation calc= new MathsOperation (); assertequals(4, calc.add(0,4)); assertequals(4, calc.add(4,0)); assertequals(4, calc.add(2,2)); assertequals(4, calc.add(2,2)); assertequals(2*integer.max_value, calc.add(integer.max_value, Integer.MAX_VALUE)); assertequals (4, calc.add(2,2)); assertequals (0, calc.difference(2,2)); assertequals (1, calc.difference(2,3)); // now I d like to have the result of the last sum assertequals(4, calc.getsumresult()); } public static boolean assertequals (int value1, int value2) { System.out.print("expected :" + value1 + " ; obtained: "+ value2); if (value1!= value2) { System.out.println(": Test Failed!"); return false;} else { System.out.println(": Test Succeded!"); return false; Corrado Aaron Visaggio 22 }}}
TAC: the test results expected :4 ; obtained: 4: Test Succeded! expected :4 ; obtained: 4: Test Succeded! expected :4 ; obtained: 4: Test Succeded! expected :4 ; obtained: 4: Test Succeded! expected :-2 ; obtained: -2: Test Succeded! [this is not correct, because the computer records -2 instead of 2*( 2 31-1)] expected :4 ; obtained: 4: Test Succeded! expected :0 ; obtained: 0: Test Succeded! expected :1 ; obtained: -1: Test Failed! expected :4 ; obtained: 4: Test Succeded! Corrado Aaron Visaggio 23 TAC: change the code The code is changed accordingly to the test s result. The code obtained will be the same obtained with the tdd. Corrado Aaron Visaggio 24
Test Driven Development links The home: http://www.testdriven.com/modules/news/ JUnit org: http://www.junit.org/index.htm JUnit tour: http://junit.sourceforge.net/doc/cookstour/cookstour.htm Corrado Aaron Visaggio 25 Corrado Aaron Visaggio 26