Italian community of Lazarus and Free Pascal TDD o Test-Driven Development
Il test-driven development (abbreviato in TDD e tradotto con sviluppo guidato dai test) è un modello di sviluppo del software che prevede che la stesura dei test automatici avvenga prima di quella del software che deve essere sottoposto a test, e che lo sviluppo del software applicativo sia orientato esclusivamente all'obiettivo di passare i test automatici precedentemente predisposti.
Più in dettaglio, il TDD prevede la ripetizione di un breve ciclo di sviluppo in tre fasi, detto "ciclo TDD". Nella prima fase (detta "fase rossa"), il programmatore scrive un test automatico per la nuova funzione da sviluppare, che deve fallire in quanto la funzione non è stata ancora realizzata. Nella seconda fase (detta "fase verde"), il programmatore sviluppa la quantità minima di codice necessaria per passare il test. Nella terza fase (detta "fase grigia" o di refactoring), il programmatore esegue il refactoring del codice per adeguarlo a determinati standard di qualità. Il TDD è una delle 12 regole base dell'XP, ma viene anche usato indipendentemente da questa metodologia; la sua applicazione è anche parte fondamentale dello sviluppo agile del software. Lazarus ci mette a disposizione dei semplici strumenti per poter adottare il TDD. Per prima cosa dobbiamo installare il package che trovate nella cartella d'installazione di lazarus nella sottodirectory components/fpunit/ide/ di nome fpcunitide.lpk Premessa: questa metodologia è utile per sviluppare le logiche dei software e non l'interfaccia grafica. Ora procediamo con un progetto d'esempio per capire come funziona, ipotizziamo di voler reinventare la ruota e di scrivere una libreria matematica che deve fornirmi le funzionalità di somma, differenza, moltiplicazione e divisione di due numeri. Partiamo con la creazione di un nuovo progetto File -> nuovo ... -> Progetto -> Applicazione di test FPCUnit Inserisci nome del test di default: TTestCase_Math Checchiamo su "Crea metodo di setup" e su "Crea metodo TearDown" Procediamo, si aprirà una finestra con del codice Codice: [Seleziona]
Ora procediamo con la fase rossa, aggiungendo del codice a questa unit trasformandola in Codice: [Seleziona]
Ora se compilate il software dovrebbe comparire una schermata con la lista dei test da eseguire (ovvero tutte le procedure presenti nel ramo published della classe) e due pulsanti in alto a sinistra con cui potete eseguire tutti i test o quello selezionato. Se premete sul pulsante che esegue tutti i test vedrete il risultato del vostro lavoro fino ad adesso. Bene, una volta definiti i test da superare passiamo alla unit (di nome uMath) che è il cuore del nostro ragionamento, che sarà scritta come segue Codice: [Seleziona]
Come si può dedurre questa unit è di per se molto semplice e non necessiterà della fase grigia, ma raramente ciò accade nella realtà. Procediamo quindi con la fase verde del nostro progetto, andando a modifica la unit TestCase1 come segue Codice: [Seleziona]
Se ora eseguite tutti i test noterete che tutte le procedure ad eccezione di TestHookUp superano il test. Per maggiori info fare riferimento al seguente link: https://wiki.freepascal.org/fpcunit
Navigazione [0] Lazarus 1.0 [#] Forum |