Download - Unit testing 2014
![Page 1: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/1.jpg)
Unit Testing
30/04/2014 - Giacomo Petronio
![Page 2: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/2.jpg)
Cronoprogramma
● Software testing e software checking● Livelli di test● Unit test / JUnit● Codice testabile● Dependency Inversion● Friendly Classes● Test-Driven Development
![Page 3: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/3.jpg)
Software Testing
Venire a conoscenza di eventuali rischi
Verifica dei requisiti espliciti ed impliciti
Valutazione della Qualità● Correttezza
● Affidabilità
● Robustezza
● Usabilità
● Efficienza
● Manutenibilità
● Portabilità
![Page 4: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/4.jpg)
Software Checking
Attività che mira a valutare un attributo o un comportamento di un sistema, ed a verificare che questo soddisfi i requisiti.
Checking is the process of making evaluations by applying algorithmic decision rules to specific observations of a product.
![Page 5: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/5.jpg)
Test Correttezza
Black-boxBasati sulle specificheDiversi livelli
![Page 6: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/6.jpg)
Test Correttezza
Livello Pro ControSystem tests
alta confidenza se tutto OK lenti
poco indicativi in caso di fail
difficilmente automatizzabili
Unit tests veloci
molto indicativi in caso di fail
automatizzabili
confidenza relativa
![Page 7: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/7.jpg)
# tests
tempoesecuzione
Scenario
Integration Tests
Unit Tests
test intero sistemapunto di vista dell'utente
test di collezioni di classisottosistemi isolati
test di classi singole
Livelli di test
![Page 8: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/8.jpg)
Struttura Unit Test
Test DriverUnit Under Test
Stimolo
Assert
Contesto
![Page 9: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/9.jpg)
JUnit
![Page 10: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/10.jpg)
JUnit Asserts
assertEquals(expected, actual)
assertTrue(value)assertFalse(value)
assertNull(value)assertNotNull(value)
assertThat(value, Matcher)● assertThat(value, is("A"));● assertThat(value, is(true));● assertThat(value, is(notNull()));
![Page 11: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/11.jpg)
Running JUnit Tests
![Page 12: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/12.jpg)
JUnit Best Practices
● N model classes → N test classes
● 1 test case per funzionalità
Stack Test Cases● test Empty Stack● test Correct Order● test Null Input● test Pop When Empty
![Page 13: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/13.jpg)
JUnit Best Practices
Un assert per test caseStack stack = new Stack();stack.push("A");
assertFalse(stack.isEmpty());assertEquals("A", stack.peek());assertFalse(stack.isEmpty());assertEquals("A", stack.pop());assertTrue(stack.isEmpty());
![Page 14: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/14.jpg)
JUnit Best Practices
White box / Black box
![Page 15: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/15.jpg)
JUnit Best Practices
Come testo un metodo privato?
● non testare
● reflection
● cambiare visibilità
● classe emergente
![Page 16: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/16.jpg)
JUnit Best Practices
Nome Classe di Test
Stack StackTest
Stesso package ma diversa cartella ● src/it/units/inginf
o Stack.java● test/it/units/inginf
o StackTest.java
![Page 17: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/17.jpg)
stackShouldBeEmpty(){ … }popShouldReturnNullWhenEmpty(){ … }
peekShouldNotRemoveItem(){ … }
JUnit Best Practices
Nomi test-case verbosi
test01(){ … }test02(){ … }
![Page 18: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/18.jpg)
JUnit Best Practices
Testare le eccezioni
@Test(expected=NullPointerException.class)public void shouldThrowExceptionOnNullInput() {
Stack stack = new Stack();stack.push(null);
}
![Page 19: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/19.jpg)
JUnit Best Practices
Classi di equivalenza
Testare tutti gli input Risorse LimitateVS
![Page 20: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/20.jpg)
IMPORTANTE!!Caratteristiche di ogni Unit Test
Velocità Ripetibilità Indipendenza
DBMSNetworkingFile-System
![Page 21: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/21.jpg)
Benefici Unit Testing
12 3
Documentazione
Regression testing
Refactoring
![Page 22: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/22.jpg)
Facile…
Test DriverUnit Under Test
Stimolo
Assert
Contesto
… o no?
![Page 23: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/23.jpg)
Come scrivere codice non testabile
● Mescolare i new con la logica● Codice nel costruttore● Global state (es. singletons)● Metodi statici● Troppi condizionali● Troppe responsabilità
![Page 24: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/24.jpg)
Test DriverUnit Under Test
Stimolo
Assert
Contesto
Other Class
Other Class
Other Class
DB
File System
Internet
Facile…?
![Page 25: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/25.jpg)
Test DriverUnit Under Test
Stimolo
Assert
Contesto
Other Class
Other Class
Other Class
DB
File System
Internet
Codice Testabile
SEAM
![Page 26: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/26.jpg)
Test DriverUnit Under Test
Stimolo
Assert
Contesto
Friendly
Friendly
Friendly
Codice Testabile
SEAM
![Page 27: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/27.jpg)
Codice Testabile
Crezione Business LogicGrafo degli Oggetti
![Page 28: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/28.jpg)
Test DriverUnit Under Test
Friendly
Friendly
Friendly
Codice Testabile
SEAMOggetto istanziato
![Page 29: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/29.jpg)
Dependency Inversion SOLIDConcrete, Abstract
Concrete Abstract
class Geek { playWith(PlayStation ps){
ps.play(); }}
class Geek { playWith(IConsolle c){
c.play(); }}
![Page 30: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/30.jpg)
Esempio
MovieFilter
+moviesDirectedBy(director)
<<Interface>>MovieFinder
+ getAllMovies()
MovieFinderImpl<<crea>>
esempio…
![Page 31: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/31.jpg)
Inversion of Control (IoC)
MovieFilter
+moviesDirectedBy(director)
<<Interface>>MovieFinder
+ getAllMovies()
MovieFinderImpl<<crea>>
Assemblatore <<crea>>
<<inject>>
![Page 32: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/32.jpg)
IoC: Dependency Injection
● Constructor Injectiono Dipendenze esplicite
● Setter Injectiono Dipendenze meno espliciteo Comodo in classi esistenti
● Frameworko Guiceo Springo CDI (J2EE)
![Page 33: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/33.jpg)
IoC: Service Locator
MovieFilter
+moviesDirectedBy(director)
<<Interface>>MovieFinder
+ getAllMovies()
MovieFinderImpl
Service Locator
<<chiede>>
<<crea>>
![Page 34: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/34.jpg)
Friendly Classes
Stub● Non hanno logica interna● Comportamento predefinito● Non si effettuano verifiche sugli stub● Es. finta risorsa web
![Page 35: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/35.jpg)
Friendly Classes
MockProgrammabile:
● “Quando ricevi X…”o restituisci Yo lancia eccezioneo …
Verifiche sui mock:● Numero di chiamate● Controllo parametro passato
![Page 36: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/36.jpg)
Mockito
Mock Lifecycle:● Crea mock a partire da interfaccia● Programma comportamento voluto● Utilizzo indiretto● Verifica dell’utilizzo
![Page 37: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/37.jpg)
Mockito
Esempio
MovieFinder finder = mock(MovieFinder.class)
when(finder.findAll()).thenReturn(allMovies);
MovieFilter filter = new MovieFilter(finder);
Lister.moviesDirectedBy(“Martin Scorsese”);
verify(finder).findAll(); // verifica la chiamata
esempio…
![Page 38: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/38.jpg)
Mockito
MovieFinder finder = mock(MovieFinder.class)when(finder.findAll()).___________ .thenReturn(allMovies); .thenReturn(none, all); .thenThrows(new RuntimeException()); .then( callback );
MovieLister lister = mock(MovieLister.class)when(lister.moviesDirectedBy(_____)).then(…); “Martin Scorsese” anyString() any()
![Page 39: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/39.jpg)
Legacy Code
“Codice ereditato da altri?”“Codice rimasto da una versione precedente?”
Codice senza test
![Page 40: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/40.jpg)
Test Driven Development
Si sviluppa in 3 fasi:RED scrivere un singolo test che fallisceGREEN scrivere quanto basta per far passare i testBLUE refactoring
Metodologia di sviluppo
![Page 41: Unit testing 2014](https://reader035.vdocuments.pub/reader035/viewer/2022081519/55840718d8b42a126e8b4979/html5/thumbnails/41.jpg)
Test Driven Development
Test First
Effetti diretti sul codice:● Disaccoppiato● Coeso● Testabile● API Driven