ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Unit test
Dai test manuali al continuous testing
ARGOMENTO
2
Agenda
1
Introduzione
1. Pattern test
2
TDD
1. Dall’approccio tradizionale al
testing
3
Unit e Integration Test
1. Mock, stubs, fake
2. Esempi prativi
4
Continuous testing
3
Presentazione
@ATosato86
andreatosato
andreatosato
ANDREA TOSATO
4
Svantaggi
• Richiede molto tempo di sviluppo (tempo = costo!)
• Richiede enorme pazienza al team di sviluppo
• Necessita di una rigorosa documentazione
• Non è possibile testare tutti gli input e gli scenari
come nel mondo reale
5
Costo degli errori
6
Costo dei test
7
Costo dei test
8
Costo dei test
Rampa creata in tempo e nel budget!
9
Ciclo di vita del codice
• Unit test è un segmento di
codice utilizzato per testare un
pezzo di codice applicativo
• Verifica se il codice:
o Rispetta i requisiti e il design
o Si comporta come previsto
o Aiuta a identificare gli
algoritmi e lo logiche errate
10
I componenti fondamenti di un test
• Il test deve essere leggibile
• Il test deve essere veritiero
• Il test deve essere mantenibile
• Il test deve essere ISOLATO
• Il test deve essere veloce
• Il test si deve documentare da se (nomenclatura)
• Il test non deve contenere logica
11
Manual vs Automated
• Test automatizzati
 Decrementano
drammaticamente il numero di
difetti del codice
 Migliorano il disegno app.
 Sono una buona
documentazione
 Riducono il costo ai
cambiamenti
 Consento refactoring
• Test manuali
Richiede una persona
designata ai test ad ogni
release
Meno efficiente
Non strutturato
Non ripetibile
Non comprensibile
rispetto al codice
12
Tipi di test
End to end (black-box)
Difficili da scrivere e da eseguire.
Riscontrano malfunzionamenti grafici
Functional test
Semplici da scrivere
Trovano bug di media importanza
Unit test
Semplici da scrivere
Trovano bug di logica
14
Tipi di test
TDD
ARGOMENTO
16
Approcci
Approccio Tradizionale
Test Drive Development
17
TDD
• Stile di sviluppo/ progettazione
• Non è una tecnica di testing.
• La programmazione e lo unit test non sono attività
separate.
Si scrive prima un test che fallisce, e poi si scrive il relativo codice.
Differisce dagli approcci tradizionali, in cui prima si scrive il codice e poi (forse) lo si testa.
18
Vantaggi del TDD
• Permette di scrivere software migliore e più rapidamente
(costruendo software in piccoli incrementi- 1 test alla volta)
• Evita il procedere per tentativi
• Produce codice pulito e che funziona (in modo opposto allo
sviluppo architecture driven, in cui si fanno prima tutte le
decisioni).
• Consente agli sviluppatori di produrre un insieme di test di
regressione automatizzabili, man mano che sviluppano.
• Riduce il tempo per la correzione dei difetti
19
Costo dei test
La fatica ripaga
20
Acceptance test
Si trasformano i requisiti applicativi in test eseguibili.
21
Red/Green/Refactor pattern
Red Scrivi un Test che fallisce.
Green Scrivi il codice per soddisfare il test.
Refactor Migliora il codice senza cambiarne la
funzionalità.
Ripeti.
22
AAA pattern
• Arrange
Creazione dell’ambiente applicativo che
si vuole testare.
• Act
Esecuzione del codice che si vuole
testare
• Assert
Verifica e validazione dei risultati
ottenuti
[TestMethod]
public void NoNewMessages()
{
//Arrange
Mailbox mailbox = new Mailbox();
//Act
var result = mailbox.GetCount(0);
//Assert
Assert.AreEqual("No new messages.", result);
}
23
Refactoring
Eliminare codice duplicato
Migliorarne l’espressività
Riduzione accoppiamento e migliora il codice
Dopo il Refactoring, i test vanno rieseguiti.
Unit test
ARGOMENTO
25
Unit test
Non è Unit Test se:
• comunica con il database
• comunica in rete o servizi terze parti
• non può essere lanciato in parallelo ad altri test
• utilizza il filesystem
26
Unit test
Unit Test se:
• non comunica con il database
• non comunica in rete o servizi terze parti
• può essere lanciato in parallelo ad altri test
• non utilizza il filesystem
• dovrebbe durare massimo 1 secondo
27
Unit test
Inoltre…
• Scritto ed eseguito dai sviluppatori
• Obiettivo: separare ciascuna parte e testare singolarmente
• Eseguito prima delle integrazioni
• Considera di utilizzare dei White-Box
• IntelliTest https://www.microsoft.com/en-us/research/project/pex-and-moles-isolation-and-white-box-unit-testing-for-net/
• Utilizza le asserzioni per verificare le esecuzioni del codice
28
Unit test
Non solo Unit Test!
Click to edit Master title style
Demo
Unit - Test
Click to edit Master title style
Demo
Code Coverage
Integration test
ARGOMENTO
32
Integration test
33
Unit test
In casa Microsoft - Martin Woodward
Mock - Stubs
ARGOMENTO
Un aiuto agli integration test
35
Nomenclatura
Stubs (variante test doubles)
Fornisce un comportamento fissato (metodi che tornano
sempre lo stesso valore).
Viene usato per controllare l'"input indiretto" del codice sotto
test.
36
Nomenclatura
Mock (variante spy)
Memorizza esplicitamente le aspettative sul comportamento del
codice sotto test, e verificare lo stato.
Comportamento programmabile, che simula quello
dell'oggetto reale
37
Mock vs Stub
In genere lo stub è molto più semplice di un Mock-Object.
Gli stub forniscono risposte preconfezionate, e nulla che sia al di
fuori di ciò che è previsto per il test.
I mock hanno implementazioni più sofisticate che consentono di
verificare il comportamento dell’unità testata (e non solo lo stato)
verificando ad esempio le collaborazioni avute con altri oggetti
ed il relativo ordine di esecuzione. Possono verificare se l’oggetto
che li usa lo fa correttamente. Testano unità senza legarsi ad
oggetti esterni.
38
La classe Client (da testare) usa i metodi di Helper.
Vogliamo controllare direttamente ciò che Helper restituisce a Client
Click to edit Master title style
Demo
Moq
40
Framework & tools
Functional o
Application Test
ARGOMENTO
Click to edit Master title style
Demo
43
Continuous testing
È una pratica di sviluppo software in cui i membri del team di
sviluppo integrano il proprio lavoro frequentemente, almeno una
volta al giorno. Ogni integrazione è verificata da strumenti
automatici che eseguono il build e riesegueno tutti i test sulla
nuova configurazione, per trovare errori di integrazione
rapidamente.
Click to edit Master title style
Demo
VSTS – continuous testing
45
Costo dei test
La fatica ripaga!
46
Altri temi sul testing
• UI testing
• Selenium
• Test client-side
• Jasmine
• Protractor (end-to-end)
• SQL testing https://www.red-gate.com/blog/database-development/dont-unit-test-sql-server-code/amp
47
Grazie
Domande?
cloudgenverona @cloudgen_verona CloudGen Verona

More Related Content

What's hot (18)

TTT - Test, Tools and Tips - jug roma
TTT - Test, Tools and Tips - jug romaTTT - Test, Tools and Tips - jug roma
TTT - Test, Tools and Tips - jug roma
diego mauricio lagos morales
Ìý
Test automatizzati & serenity bdd
Test automatizzati & serenity bddTest automatizzati & serenity bdd
Test automatizzati & serenity bdd
diego mauricio lagos morales
Ìý
Introduzione al Test Driven Development
Introduzione al Test Driven DevelopmentIntroduzione al Test Driven Development
Introduzione al Test Driven Development
Ennio Masi
Ìý
Unit test
Unit testUnit test
Unit test
Roberto Cappelletti
Ìý
Software Testing & Test Driven Development
Software Testing & Test Driven DevelopmentSoftware Testing & Test Driven Development
Software Testing & Test Driven Development
Sergio Santoro
Ìý
Unit testing 101
Unit testing 101Unit testing 101
Unit testing 101
Daniel Londero
Ìý
Webinar: “Testing automatico: la scelta vincente per ottenere una riduzione d...
Webinar: “Testing automatico: la scelta vincente per ottenere una riduzione d...Webinar: “Testing automatico: la scelta vincente per ottenere una riduzione d...
Webinar: “Testing automatico: la scelta vincente per ottenere una riduzione d...
Emerasoft, solutions to collaborate
Ìý
Test Driven Development for iOS
Test Driven Development for iOSTest Driven Development for iOS
Test Driven Development for iOS
Alessandro Ceseno
Ìý
Linux Day 20091024 Test Driven Development
Linux Day 20091024 Test Driven DevelopmentLinux Day 20091024 Test Driven Development
Linux Day 20091024 Test Driven Development
Roberto Albertini
Ìý
PowerMock TDD User Group Milano
PowerMock TDD User Group MilanoPowerMock TDD User Group Milano
PowerMock TDD User Group Milano
Massimo Groppelli
Ìý
Detailed Model Capture
Detailed Model CaptureDetailed Model Capture
Detailed Model Capture
fcospito
Ìý
Dependency injection: the good parts
Dependency injection:  the good partsDependency injection:  the good parts
Dependency injection: the good parts
Massimo Groppelli
Ìý
Java Unit Testing - JUnit (1)
Java Unit Testing - JUnit (1)Java Unit Testing - JUnit (1)
Java Unit Testing - JUnit (1)
fgianneschi
Ìý
Unit Testing Mockito
Unit Testing MockitoUnit Testing Mockito
Unit Testing Mockito
Luca S Lopomo
Ìý
Le pratiche ingegneristiche di eXtreme Programming
Le pratiche ingegneristiche di eXtreme ProgrammingLe pratiche ingegneristiche di eXtreme Programming
Le pratiche ingegneristiche di eXtreme Programming
Andrea Francia
Ìý
Java Unit Testing - JUnit (2)
Java Unit Testing - JUnit (2)Java Unit Testing - JUnit (2)
Java Unit Testing - JUnit (2)
fgianneschi
Ìý
Introduzione al Test Driven Development
Introduzione al Test Driven DevelopmentIntroduzione al Test Driven Development
Introduzione al Test Driven Development
Ennio Masi
Ìý
Software Testing & Test Driven Development
Software Testing & Test Driven DevelopmentSoftware Testing & Test Driven Development
Software Testing & Test Driven Development
Sergio Santoro
Ìý
Unit testing 101
Unit testing 101Unit testing 101
Unit testing 101
Daniel Londero
Ìý
Webinar: “Testing automatico: la scelta vincente per ottenere una riduzione d...
Webinar: “Testing automatico: la scelta vincente per ottenere una riduzione d...Webinar: “Testing automatico: la scelta vincente per ottenere una riduzione d...
Webinar: “Testing automatico: la scelta vincente per ottenere una riduzione d...
Emerasoft, solutions to collaborate
Ìý
Test Driven Development for iOS
Test Driven Development for iOSTest Driven Development for iOS
Test Driven Development for iOS
Alessandro Ceseno
Ìý
Linux Day 20091024 Test Driven Development
Linux Day 20091024 Test Driven DevelopmentLinux Day 20091024 Test Driven Development
Linux Day 20091024 Test Driven Development
Roberto Albertini
Ìý
PowerMock TDD User Group Milano
PowerMock TDD User Group MilanoPowerMock TDD User Group Milano
PowerMock TDD User Group Milano
Massimo Groppelli
Ìý
Detailed Model Capture
Detailed Model CaptureDetailed Model Capture
Detailed Model Capture
fcospito
Ìý
Dependency injection: the good parts
Dependency injection:  the good partsDependency injection:  the good parts
Dependency injection: the good parts
Massimo Groppelli
Ìý
Java Unit Testing - JUnit (1)
Java Unit Testing - JUnit (1)Java Unit Testing - JUnit (1)
Java Unit Testing - JUnit (1)
fgianneschi
Ìý
Unit Testing Mockito
Unit Testing MockitoUnit Testing Mockito
Unit Testing Mockito
Luca S Lopomo
Ìý
Le pratiche ingegneristiche di eXtreme Programming
Le pratiche ingegneristiche di eXtreme ProgrammingLe pratiche ingegneristiche di eXtreme Programming
Le pratiche ingegneristiche di eXtreme Programming
Andrea Francia
Ìý
Java Unit Testing - JUnit (2)
Java Unit Testing - JUnit (2)Java Unit Testing - JUnit (2)
Java Unit Testing - JUnit (2)
fgianneschi
Ìý

Similar to Unit Testing (20)

PASS Virtual Chapter - Unit Testing su SQL Server
PASS Virtual Chapter - Unit Testing su SQL ServerPASS Virtual Chapter - Unit Testing su SQL Server
PASS Virtual Chapter - Unit Testing su SQL Server
Alessandro Alpi
Ìý
Fe05 test drivenjavascriptdevelopment
Fe05   test drivenjavascriptdevelopmentFe05   test drivenjavascriptdevelopment
Fe05 test drivenjavascriptdevelopment
DotNetCampus
Ìý
Agileday2013 pratiche agili applicate all'infrastruttura
Agileday2013 pratiche agili applicate all'infrastrutturaAgileday2013 pratiche agili applicate all'infrastruttura
Agileday2013 pratiche agili applicate all'infrastruttura
XPeppers
Ìý
Delphi & Dintorni Webinar - Diventa un mago del Testing
Delphi & Dintorni Webinar - Diventa un mago del TestingDelphi & Dintorni Webinar - Diventa un mago del Testing
Delphi & Dintorni Webinar - Diventa un mago del Testing
Marco Breveglieri
Ìý
L'Occhio di Ra sul Testing
L'Occhio di Ra sul TestingL'Occhio di Ra sul Testing
L'Occhio di Ra sul Testing
Felice Pescatore
Ìý
DotNetCampus - Continuous Integration con Sql Server
DotNetCampus - Continuous Integration con Sql ServerDotNetCampus - Continuous Integration con Sql Server
DotNetCampus - Continuous Integration con Sql Server
Alessandro Alpi
Ìý
CONTINUOUS INTEGRATION CON SQL SERVER
CONTINUOUS INTEGRATION CON SQL SERVERCONTINUOUS INTEGRATION CON SQL SERVER
CONTINUOUS INTEGRATION CON SQL SERVER
DotNetCampus
Ìý
Presentazione della Tesi di Laurea Specialistica : STRUMENTI PER LA GENERAZIO...
Presentazione della Tesi di Laurea Specialistica : STRUMENTI PER LA GENERAZIO...Presentazione della Tesi di Laurea Specialistica : STRUMENTI PER LA GENERAZIO...
Presentazione della Tesi di Laurea Specialistica : STRUMENTI PER LA GENERAZIO...
Boymix81
Ìý
Detailed Model Capture
Detailed Model CaptureDetailed Model Capture
Detailed Model Capture
fcospito
Ìý
A brief intro to TDD for a JUG-TAA event
A brief intro to TDD for a JUG-TAA eventA brief intro to TDD for a JUG-TAA event
A brief intro to TDD for a JUG-TAA event
Pietro Di Bello
Ìý
Una fugace occhiata al Test Driven Development (2006)
Una fugace occhiata al Test Driven Development  (2006)Una fugace occhiata al Test Driven Development  (2006)
Una fugace occhiata al Test Driven Development (2006)
Roberto Bettazzoni
Ìý
Java Unit Testing - Introduction
Java Unit Testing - IntroductionJava Unit Testing - Introduction
Java Unit Testing - Introduction
fgianneschi
Ìý
PASS Virtual Chapter - SQL Server Continuous Integration
PASS Virtual Chapter - SQL Server Continuous IntegrationPASS Virtual Chapter - SQL Server Continuous Integration
PASS Virtual Chapter - SQL Server Continuous Integration
Alessandro Alpi
Ìý
Unit testing in Visual Studio 2013
Unit testing in Visual Studio 2013Unit testing in Visual Studio 2013
Unit testing in Visual Studio 2013
DomusDotNet
Ìý
DevOpsHeroes 2016 - Realizzare Continouous Integration con SQL Server e Visua...
DevOpsHeroes 2016 - Realizzare Continouous Integration con SQL Server e Visua...DevOpsHeroes 2016 - Realizzare Continouous Integration con SQL Server e Visua...
DevOpsHeroes 2016 - Realizzare Continouous Integration con SQL Server e Visua...
Alessandro Alpi
Ìý
Clean programming 2020-01-25 @ Modena Tech Summit
Clean programming 2020-01-25 @ Modena Tech SummitClean programming 2020-01-25 @ Modena Tech Summit
Clean programming 2020-01-25 @ Modena Tech Summit
Davide Muzzarelli
Ìý
Android Test Driven Development
Android Test Driven DevelopmentAndroid Test Driven Development
Android Test Driven Development
sazilla
Ìý
Test Driven Development @ Xe.Net
Test Driven Development @ Xe.NetTest Driven Development @ Xe.Net
Test Driven Development @ Xe.Net
Mauro Servienti
Ìý
PhpUnit
PhpUnitPhpUnit
PhpUnit
Pietro Alberto Rossi
Ìý
Introduzione ai Coded UI Tests
Introduzione ai Coded UI TestsIntroduzione ai Coded UI Tests
Introduzione ai Coded UI Tests
Pietro Libro
Ìý
PASS Virtual Chapter - Unit Testing su SQL Server
PASS Virtual Chapter - Unit Testing su SQL ServerPASS Virtual Chapter - Unit Testing su SQL Server
PASS Virtual Chapter - Unit Testing su SQL Server
Alessandro Alpi
Ìý
Fe05 test drivenjavascriptdevelopment
Fe05   test drivenjavascriptdevelopmentFe05   test drivenjavascriptdevelopment
Fe05 test drivenjavascriptdevelopment
DotNetCampus
Ìý
Agileday2013 pratiche agili applicate all'infrastruttura
Agileday2013 pratiche agili applicate all'infrastrutturaAgileday2013 pratiche agili applicate all'infrastruttura
Agileday2013 pratiche agili applicate all'infrastruttura
XPeppers
Ìý
Delphi & Dintorni Webinar - Diventa un mago del Testing
Delphi & Dintorni Webinar - Diventa un mago del TestingDelphi & Dintorni Webinar - Diventa un mago del Testing
Delphi & Dintorni Webinar - Diventa un mago del Testing
Marco Breveglieri
Ìý
L'Occhio di Ra sul Testing
L'Occhio di Ra sul TestingL'Occhio di Ra sul Testing
L'Occhio di Ra sul Testing
Felice Pescatore
Ìý
DotNetCampus - Continuous Integration con Sql Server
DotNetCampus - Continuous Integration con Sql ServerDotNetCampus - Continuous Integration con Sql Server
DotNetCampus - Continuous Integration con Sql Server
Alessandro Alpi
Ìý
CONTINUOUS INTEGRATION CON SQL SERVER
CONTINUOUS INTEGRATION CON SQL SERVERCONTINUOUS INTEGRATION CON SQL SERVER
CONTINUOUS INTEGRATION CON SQL SERVER
DotNetCampus
Ìý
Presentazione della Tesi di Laurea Specialistica : STRUMENTI PER LA GENERAZIO...
Presentazione della Tesi di Laurea Specialistica : STRUMENTI PER LA GENERAZIO...Presentazione della Tesi di Laurea Specialistica : STRUMENTI PER LA GENERAZIO...
Presentazione della Tesi di Laurea Specialistica : STRUMENTI PER LA GENERAZIO...
Boymix81
Ìý
Detailed Model Capture
Detailed Model CaptureDetailed Model Capture
Detailed Model Capture
fcospito
Ìý
A brief intro to TDD for a JUG-TAA event
A brief intro to TDD for a JUG-TAA eventA brief intro to TDD for a JUG-TAA event
A brief intro to TDD for a JUG-TAA event
Pietro Di Bello
Ìý
Una fugace occhiata al Test Driven Development (2006)
Una fugace occhiata al Test Driven Development  (2006)Una fugace occhiata al Test Driven Development  (2006)
Una fugace occhiata al Test Driven Development (2006)
Roberto Bettazzoni
Ìý
Java Unit Testing - Introduction
Java Unit Testing - IntroductionJava Unit Testing - Introduction
Java Unit Testing - Introduction
fgianneschi
Ìý
PASS Virtual Chapter - SQL Server Continuous Integration
PASS Virtual Chapter - SQL Server Continuous IntegrationPASS Virtual Chapter - SQL Server Continuous Integration
PASS Virtual Chapter - SQL Server Continuous Integration
Alessandro Alpi
Ìý
Unit testing in Visual Studio 2013
Unit testing in Visual Studio 2013Unit testing in Visual Studio 2013
Unit testing in Visual Studio 2013
DomusDotNet
Ìý
DevOpsHeroes 2016 - Realizzare Continouous Integration con SQL Server e Visua...
DevOpsHeroes 2016 - Realizzare Continouous Integration con SQL Server e Visua...DevOpsHeroes 2016 - Realizzare Continouous Integration con SQL Server e Visua...
DevOpsHeroes 2016 - Realizzare Continouous Integration con SQL Server e Visua...
Alessandro Alpi
Ìý
Clean programming 2020-01-25 @ Modena Tech Summit
Clean programming 2020-01-25 @ Modena Tech SummitClean programming 2020-01-25 @ Modena Tech Summit
Clean programming 2020-01-25 @ Modena Tech Summit
Davide Muzzarelli
Ìý
Android Test Driven Development
Android Test Driven DevelopmentAndroid Test Driven Development
Android Test Driven Development
sazilla
Ìý
Test Driven Development @ Xe.Net
Test Driven Development @ Xe.NetTest Driven Development @ Xe.Net
Test Driven Development @ Xe.Net
Mauro Servienti
Ìý
Introduzione ai Coded UI Tests
Introduzione ai Coded UI TestsIntroduzione ai Coded UI Tests
Introduzione ai Coded UI Tests
Pietro Libro
Ìý

More from Andrea Tosato (20)

Codemotion Azure Container Apps
Codemotion Azure Container AppsCodemotion Azure Container Apps
Codemotion Azure Container Apps
Andrea Tosato
Ìý
Lite db for dummies
Lite db for dummiesLite db for dummies
Lite db for dummies
Andrea Tosato
Ìý
Azure Static Web Apps & Blazor
Azure Static Web Apps & BlazorAzure Static Web Apps & Blazor
Azure Static Web Apps & Blazor
Andrea Tosato
Ìý
Dapr logicapps
Dapr logicappsDapr logicapps
Dapr logicapps
Andrea Tosato
Ìý
How to develop modern web application, with no money and nod javascript
How to develop modern web application, with no money and nod javascriptHow to develop modern web application, with no money and nod javascript
How to develop modern web application, with no money and nod javascript
Andrea Tosato
Ìý
Entity framework core v3 from sql to no sql
Entity framework core v3 from sql to no sqlEntity framework core v3 from sql to no sql
Entity framework core v3 from sql to no sql
Andrea Tosato
Ìý
How to develop modern web application - With no money and no Javascript
How to develop modern web application - With no money and no JavascriptHow to develop modern web application - With no money and no Javascript
How to develop modern web application - With no money and no Javascript
Andrea Tosato
Ìý
Mixing Identity server, AAD, ASP .NET Identity
Mixing Identity server, AAD, ASP .NET IdentityMixing Identity server, AAD, ASP .NET Identity
Mixing Identity server, AAD, ASP .NET Identity
Andrea Tosato
Ìý
An introduction to GraphQL in .NET Core
An introduction to GraphQL in .NET CoreAn introduction to GraphQL in .NET Core
An introduction to GraphQL in .NET Core
Andrea Tosato
Ìý
DevOps Heroes 2019
DevOps Heroes 2019DevOps Heroes 2019
DevOps Heroes 2019
Andrea Tosato
Ìý
dotNetConf2019
dotNetConf2019dotNetConf2019
dotNetConf2019
Andrea Tosato
Ìý
Cost Optimization - Global Azure Bootcamp 2019
Cost Optimization - Global Azure Bootcamp 2019Cost Optimization - Global Azure Bootcamp 2019
Cost Optimization - Global Azure Bootcamp 2019
Andrea Tosato
Ìý
Deep Dive Azure Functions - Global Azure Bootcamp 2019
Deep Dive Azure Functions - Global Azure Bootcamp 2019Deep Dive Azure Functions - Global Azure Bootcamp 2019
Deep Dive Azure Functions - Global Azure Bootcamp 2019
Andrea Tosato
Ìý
Azure Function Workflow
Azure Function WorkflowAzure Function Workflow
Azure Function Workflow
Andrea Tosato
Ìý
Azure Cognitive Service on Container
Azure Cognitive Service on ContainerAzure Cognitive Service on Container
Azure Cognitive Service on Container
Andrea Tosato
Ìý
Deploy multi-environment application with Azure DevOps
Deploy multi-environment application with Azure DevOpsDeploy multi-environment application with Azure DevOps
Deploy multi-environment application with Azure DevOps
Andrea Tosato
Ìý
Azure Cognitive Service in Container
Azure Cognitive Service in ContainerAzure Cognitive Service in Container
Azure Cognitive Service in Container
Andrea Tosato
Ìý
Azure Signalr Service
Azure Signalr ServiceAzure Signalr Service
Azure Signalr Service
Andrea Tosato
Ìý
Xamarin - Microcharts
Xamarin - MicrochartsXamarin - Microcharts
Xamarin - Microcharts
Andrea Tosato
Ìý
Introduzione Xamarin
Introduzione XamarinIntroduzione Xamarin
Introduzione Xamarin
Andrea Tosato
Ìý
Codemotion Azure Container Apps
Codemotion Azure Container AppsCodemotion Azure Container Apps
Codemotion Azure Container Apps
Andrea Tosato
Ìý
Lite db for dummies
Lite db for dummiesLite db for dummies
Lite db for dummies
Andrea Tosato
Ìý
Azure Static Web Apps & Blazor
Azure Static Web Apps & BlazorAzure Static Web Apps & Blazor
Azure Static Web Apps & Blazor
Andrea Tosato
Ìý
Dapr logicapps
Dapr logicappsDapr logicapps
Dapr logicapps
Andrea Tosato
Ìý
How to develop modern web application, with no money and nod javascript
How to develop modern web application, with no money and nod javascriptHow to develop modern web application, with no money and nod javascript
How to develop modern web application, with no money and nod javascript
Andrea Tosato
Ìý
Entity framework core v3 from sql to no sql
Entity framework core v3 from sql to no sqlEntity framework core v3 from sql to no sql
Entity framework core v3 from sql to no sql
Andrea Tosato
Ìý
How to develop modern web application - With no money and no Javascript
How to develop modern web application - With no money and no JavascriptHow to develop modern web application - With no money and no Javascript
How to develop modern web application - With no money and no Javascript
Andrea Tosato
Ìý
Mixing Identity server, AAD, ASP .NET Identity
Mixing Identity server, AAD, ASP .NET IdentityMixing Identity server, AAD, ASP .NET Identity
Mixing Identity server, AAD, ASP .NET Identity
Andrea Tosato
Ìý
An introduction to GraphQL in .NET Core
An introduction to GraphQL in .NET CoreAn introduction to GraphQL in .NET Core
An introduction to GraphQL in .NET Core
Andrea Tosato
Ìý
DevOps Heroes 2019
DevOps Heroes 2019DevOps Heroes 2019
DevOps Heroes 2019
Andrea Tosato
Ìý
dotNetConf2019
dotNetConf2019dotNetConf2019
dotNetConf2019
Andrea Tosato
Ìý
Cost Optimization - Global Azure Bootcamp 2019
Cost Optimization - Global Azure Bootcamp 2019Cost Optimization - Global Azure Bootcamp 2019
Cost Optimization - Global Azure Bootcamp 2019
Andrea Tosato
Ìý
Deep Dive Azure Functions - Global Azure Bootcamp 2019
Deep Dive Azure Functions - Global Azure Bootcamp 2019Deep Dive Azure Functions - Global Azure Bootcamp 2019
Deep Dive Azure Functions - Global Azure Bootcamp 2019
Andrea Tosato
Ìý
Azure Function Workflow
Azure Function WorkflowAzure Function Workflow
Azure Function Workflow
Andrea Tosato
Ìý
Azure Cognitive Service on Container
Azure Cognitive Service on ContainerAzure Cognitive Service on Container
Azure Cognitive Service on Container
Andrea Tosato
Ìý
Deploy multi-environment application with Azure DevOps
Deploy multi-environment application with Azure DevOpsDeploy multi-environment application with Azure DevOps
Deploy multi-environment application with Azure DevOps
Andrea Tosato
Ìý
Azure Cognitive Service in Container
Azure Cognitive Service in ContainerAzure Cognitive Service in Container
Azure Cognitive Service in Container
Andrea Tosato
Ìý
Azure Signalr Service
Azure Signalr ServiceAzure Signalr Service
Azure Signalr Service
Andrea Tosato
Ìý
Xamarin - Microcharts
Xamarin - MicrochartsXamarin - Microcharts
Xamarin - Microcharts
Andrea Tosato
Ìý
Introduzione Xamarin
Introduzione XamarinIntroduzione Xamarin
Introduzione Xamarin
Andrea Tosato
Ìý

Unit Testing

  • 1. Unit test Dai test manuali al continuous testing ARGOMENTO
  • 2. 2 Agenda 1 Introduzione 1. Pattern test 2 TDD 1. Dall’approccio tradizionale al testing 3 Unit e Integration Test 1. Mock, stubs, fake 2. Esempi prativi 4 Continuous testing
  • 4. 4 Svantaggi • Richiede molto tempo di sviluppo (tempo = costo!) • Richiede enorme pazienza al team di sviluppo • Necessita di una rigorosa documentazione • Non è possibile testare tutti gli input e gli scenari come nel mondo reale
  • 8. 8 Costo dei test Rampa creata in tempo e nel budget!
  • 9. 9 Ciclo di vita del codice • Unit test è un segmento di codice utilizzato per testare un pezzo di codice applicativo • Verifica se il codice: o Rispetta i requisiti e il design o Si comporta come previsto o Aiuta a identificare gli algoritmi e lo logiche errate
  • 10. 10 I componenti fondamenti di un test • Il test deve essere leggibile • Il test deve essere veritiero • Il test deve essere mantenibile • Il test deve essere ISOLATO • Il test deve essere veloce • Il test si deve documentare da se (nomenclatura) • Il test non deve contenere logica
  • 11. 11 Manual vs Automated • Test automatizzati  Decrementano drammaticamente il numero di difetti del codice  Migliorano il disegno app.  Sono una buona documentazione  Riducono il costo ai cambiamenti  Consento refactoring • Test manuali Richiede una persona designata ai test ad ogni release Meno efficiente Non strutturato Non ripetibile Non comprensibile rispetto al codice
  • 12. 12 Tipi di test End to end (black-box) Difficili da scrivere e da eseguire. Riscontrano malfunzionamenti grafici Functional test Semplici da scrivere Trovano bug di media importanza Unit test Semplici da scrivere Trovano bug di logica
  • 16. 17 TDD • Stile di sviluppo/ progettazione • Non è una tecnica di testing. • La programmazione e lo unit test non sono attività separate. Si scrive prima un test che fallisce, e poi si scrive il relativo codice. Differisce dagli approcci tradizionali, in cui prima si scrive il codice e poi (forse) lo si testa.
  • 17. 18 Vantaggi del TDD • Permette di scrivere software migliore e più rapidamente (costruendo software in piccoli incrementi- 1 test alla volta) • Evita il procedere per tentativi • Produce codice pulito e che funziona (in modo opposto allo sviluppo architecture driven, in cui si fanno prima tutte le decisioni). • Consente agli sviluppatori di produrre un insieme di test di regressione automatizzabili, man mano che sviluppano. • Riduce il tempo per la correzione dei difetti
  • 18. 19 Costo dei test La fatica ripaga
  • 19. 20 Acceptance test Si trasformano i requisiti applicativi in test eseguibili.
  • 20. 21 Red/Green/Refactor pattern Red Scrivi un Test che fallisce. Green Scrivi il codice per soddisfare il test. Refactor Migliora il codice senza cambiarne la funzionalità. Ripeti.
  • 21. 22 AAA pattern • Arrange Creazione dell’ambiente applicativo che si vuole testare. • Act Esecuzione del codice che si vuole testare • Assert Verifica e validazione dei risultati ottenuti [TestMethod] public void NoNewMessages() { //Arrange Mailbox mailbox = new Mailbox(); //Act var result = mailbox.GetCount(0); //Assert Assert.AreEqual("No new messages.", result); }
  • 22. 23 Refactoring Eliminare codice duplicato Migliorarne l’espressività Riduzione accoppiamento e migliora il codice Dopo il Refactoring, i test vanno rieseguiti.
  • 24. 25 Unit test Non è Unit Test se: • comunica con il database • comunica in rete o servizi terze parti • non può essere lanciato in parallelo ad altri test • utilizza il filesystem
  • 25. 26 Unit test Unit Test se: • non comunica con il database • non comunica in rete o servizi terze parti • può essere lanciato in parallelo ad altri test • non utilizza il filesystem • dovrebbe durare massimo 1 secondo
  • 26. 27 Unit test Inoltre… • Scritto ed eseguito dai sviluppatori • Obiettivo: separare ciascuna parte e testare singolarmente • Eseguito prima delle integrazioni • Considera di utilizzare dei White-Box • IntelliTest https://www.microsoft.com/en-us/research/project/pex-and-moles-isolation-and-white-box-unit-testing-for-net/ • Utilizza le asserzioni per verificare le esecuzioni del codice
  • 27. 28 Unit test Non solo Unit Test!
  • 28. Click to edit Master title style Demo Unit - Test
  • 29. Click to edit Master title style Demo Code Coverage
  • 32. 33 Unit test In casa Microsoft - Martin Woodward
  • 33. Mock - Stubs ARGOMENTO Un aiuto agli integration test
  • 34. 35 Nomenclatura Stubs (variante test doubles) Fornisce un comportamento fissato (metodi che tornano sempre lo stesso valore). Viene usato per controllare l'"input indiretto" del codice sotto test.
  • 35. 36 Nomenclatura Mock (variante spy) Memorizza esplicitamente le aspettative sul comportamento del codice sotto test, e verificare lo stato. Comportamento programmabile, che simula quello dell'oggetto reale
  • 36. 37 Mock vs Stub In genere lo stub è molto più semplice di un Mock-Object. Gli stub forniscono risposte preconfezionate, e nulla che sia al di fuori di ciò che è previsto per il test. I mock hanno implementazioni più sofisticate che consentono di verificare il comportamento dell’unità testata (e non solo lo stato) verificando ad esempio le collaborazioni avute con altri oggetti ed il relativo ordine di esecuzione. Possono verificare se l’oggetto che li usa lo fa correttamente. Testano unità senza legarsi ad oggetti esterni.
  • 37. 38 La classe Client (da testare) usa i metodi di Helper. Vogliamo controllare direttamente ciò che Helper restituisce a Client
  • 38. Click to edit Master title style Demo Moq
  • 41. Click to edit Master title style Demo
  • 42. 43 Continuous testing È una pratica di sviluppo software in cui i membri del team di sviluppo integrano il proprio lavoro frequentemente, almeno una volta al giorno. Ogni integrazione è verificata da strumenti automatici che eseguono il build e riesegueno tutti i test sulla nuova configurazione, per trovare errori di integrazione rapidamente.
  • 43. Click to edit Master title style Demo VSTS – continuous testing
  • 44. 45 Costo dei test La fatica ripaga!
  • 45. 46 Altri temi sul testing • UI testing • Selenium • Test client-side • Jasmine • Protractor (end-to-end) • SQL testing https://www.red-gate.com/blog/database-development/dont-unit-test-sql-server-code/amp