SlideShare a Scribd company logo
1 of 44
Download to read offline
Introduzione al Testing
in Javascript
by Piero Bozzolo (@petecocoon)
@petecocoon
Software Craftsman
etalia.net
Di cosa parleremo?
Perché scrivere test
Test unitari
Assert / Should / Expect
Javascript: l’altro ieri
Javascript: ieri
Javascript: oggi
Backend più leggeri (API Java / Python / Ruby + FE in JS)
Lascio la logica di business nel backend e il front si occupa
solo del front
Backend in js
Flash is dead
Perché?
Come si collauda un sito in JS?
CTRL + R
Come si collauda un sito in JS?
CTRL + R (199x-20xx)
Riposa in Pace sarai sempre nei nostri cuori
Perché scrivere test?
Meno bachi nelle
nuove features
Perché scrivere test?
Meno bachi nelle
vecchie features
Perché scrivere test?
Refactor
modifica una riga di codice e so subito se
tutto continua a funzionare
Perché scrivere test?
i test sono
Documentazione
Perché scrivere test?
migliorano
il Design
Perché scrivere test?
migliorano
il Design
è difficile testare codice organizzato o
scritto male
Test unitari
Per unità si intende normalmente il minimo componente
di un programma dotato di funzionamento autonomo
Classi / Metodi (OOP)
Funzioni (Procedurale)
Cosa testare?
Risultato di una funzione
Proprietà e metodi di un oggetto
Test funzionali
Controllano che una determinata feature
funziona correttamente.
Alto livello
Esempi
Test unitario:
[2, 3, 6, 10].indexOf(3) è uguale a 1?
Test funzionale:
Metto nome utente e password nella form, con
un nome non valido, e controllo che un errore
appaia sotto la form.
Test unitari
Come?
Assert
Demo
chai.js* assert style
*http://chaijs.com/api/assert
chai.js* assert style
*http://chaijs.com/api/assert
Chai.js supporta altri due stili
Should
chai.js should style
Expect
chai.js expect style
Installazione ed uso
$ npm install mocha
$ npm install chai
$ npm install sinon
sia per frontend che per backend (node.js)
Installazione ed uso
Ruby On Rails: Konacha
gem 'konacha'
gem 'konacha-chai-matchers'
ambiente compatibile con l’assets pipeline
mocha
“Mocha allows you to use any assertion library
you want, if it throws an error, it will work!”
http://visionmedia.github.io/mocha/
Sinon.js
Spy
Una spy è una funzione che registra argomenti, valori di ritorno, eccezioni di un’altra funzione. Può essere anonima oppure decorare una
funzione esistente.
Stub
Uno stub è una funzione spia con un comportamento preprogrammato
chai
chai + mocha
mocha + chai output
mocha + chai output
Svantaggi
- Curva di apprendimento in salita
- Legacy code difficile da testare
- Costo (sopratutto iniziale)
Argomenti interessanti non trattati:
Continuous integration
Test Driven Development
Alternative (Jasmine / QUnit / Karma)
@petecocoon
piero.bozzolo@gmail.com
http://visionmedia.github.io/mocha/
http://chaijs.com/
http://sinonjs.org/

More Related Content

Similar to Testing in javascript

Il buon programmatore - consigli pratici per una vita felice
Il buon programmatore - consigli pratici per una vita feliceIl buon programmatore - consigli pratici per una vita felice
Il buon programmatore - consigli pratici per una vita feliceAndrea Dottor
 
Scala: come recuperare la programmazione funzionale e perché
Scala: come recuperare la programmazione funzionale e perchéScala: come recuperare la programmazione funzionale e perché
Scala: come recuperare la programmazione funzionale e perchéEdmondo Porcu
 
TDD in WordPress
TDD in WordPressTDD in WordPress
TDD in WordPresslucatume
 
Software Testing & Test Driven Development
Software Testing & Test Driven DevelopmentSoftware Testing & Test Driven Development
Software Testing & Test Driven DevelopmentSergio Santoro
 
Tech Webinar: Test e2e per AngularJS e non solo
Tech Webinar: Test e2e per AngularJS e non soloTech Webinar: Test e2e per AngularJS e non solo
Tech Webinar: Test e2e per AngularJS e non soloCodemotion
 
Mocking Objects Practices
Mocking Objects PracticesMocking Objects Practices
Mocking Objects PracticesGrUSP
 
Introduzione a Ruby On Rails
Introduzione a Ruby On RailsIntroduzione a Ruby On Rails
Introduzione a Ruby On RailsLuca Mearelli
 
Node js dev day napoli 2016
Node js dev day napoli 2016Node js dev day napoli 2016
Node js dev day napoli 2016Michele Nasti
 
Deep diving C# 4 (Raffaele Rialdi)
Deep diving C# 4 (Raffaele Rialdi)Deep diving C# 4 (Raffaele Rialdi)
Deep diving C# 4 (Raffaele Rialdi)DotNetMarche
 
Asp.net web form 4.5 - what's new!!
Asp.net web form 4.5 - what's new!!Asp.net web form 4.5 - what's new!!
Asp.net web form 4.5 - what's new!!Massimo Bonanni
 
Unit Tests VS End To End Tests
Unit Tests VS End To End TestsUnit Tests VS End To End Tests
Unit Tests VS End To End Testsmimmozzo_
 
Javaday 2006: Java 5
Javaday 2006: Java 5Javaday 2006: Java 5
Javaday 2006: Java 5Matteo Baccan
 
[Luca Cracco] - DevOps: strumenti di automazione per Drupal8
[Luca Cracco] - DevOps: strumenti di automazione per Drupal8[Luca Cracco] - DevOps: strumenti di automazione per Drupal8
[Luca Cracco] - DevOps: strumenti di automazione per Drupal8Wellnet srl
 
[drupalday2017] - DevOps: strumenti di automazione per Drupal8
[drupalday2017] - DevOps: strumenti di automazione per Drupal8[drupalday2017] - DevOps: strumenti di automazione per Drupal8
[drupalday2017] - DevOps: strumenti di automazione per Drupal8DrupalDay
 

Similar to Testing in javascript (20)

Il buon programmatore - consigli pratici per una vita felice
Il buon programmatore - consigli pratici per una vita feliceIl buon programmatore - consigli pratici per una vita felice
Il buon programmatore - consigli pratici per una vita felice
 
Scala: come recuperare la programmazione funzionale e perché
Scala: come recuperare la programmazione funzionale e perchéScala: come recuperare la programmazione funzionale e perché
Scala: come recuperare la programmazione funzionale e perché
 
TDD in WordPress
TDD in WordPressTDD in WordPress
TDD in WordPress
 
#dd12 grillo daniele_xpages_tips_tricks_rev2
#dd12 grillo daniele_xpages_tips_tricks_rev2#dd12 grillo daniele_xpages_tips_tricks_rev2
#dd12 grillo daniele_xpages_tips_tricks_rev2
 
Testing
TestingTesting
Testing
 
Software Testing & Test Driven Development
Software Testing & Test Driven DevelopmentSoftware Testing & Test Driven Development
Software Testing & Test Driven Development
 
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
 
Tech Webinar: Test e2e per AngularJS e non solo
Tech Webinar: Test e2e per AngularJS e non soloTech Webinar: Test e2e per AngularJS e non solo
Tech Webinar: Test e2e per AngularJS e non solo
 
Mocking Objects Practices
Mocking Objects PracticesMocking Objects Practices
Mocking Objects Practices
 
Introduzione a Ruby On Rails
Introduzione a Ruby On RailsIntroduzione a Ruby On Rails
Introduzione a Ruby On Rails
 
Node js dev day napoli 2016
Node js dev day napoli 2016Node js dev day napoli 2016
Node js dev day napoli 2016
 
Deep diving C# 4 (Raffaele Rialdi)
Deep diving C# 4 (Raffaele Rialdi)Deep diving C# 4 (Raffaele Rialdi)
Deep diving C# 4 (Raffaele Rialdi)
 
Asp.net web form 4.5 - what's new!!
Asp.net web form 4.5 - what's new!!Asp.net web form 4.5 - what's new!!
Asp.net web form 4.5 - what's new!!
 
Workshop: Introduzione ad TDD
Workshop: Introduzione ad TDDWorkshop: Introduzione ad TDD
Workshop: Introduzione ad TDD
 
Build Automation Tips
Build Automation TipsBuild Automation Tips
Build Automation Tips
 
Unit Tests VS End To End Tests
Unit Tests VS End To End TestsUnit Tests VS End To End Tests
Unit Tests VS End To End Tests
 
Javaday 2006: Java 5
Javaday 2006: Java 5Javaday 2006: Java 5
Javaday 2006: Java 5
 
Software Testing e TDD
Software Testing e TDDSoftware Testing e TDD
Software Testing e TDD
 
[Luca Cracco] - DevOps: strumenti di automazione per Drupal8
[Luca Cracco] - DevOps: strumenti di automazione per Drupal8[Luca Cracco] - DevOps: strumenti di automazione per Drupal8
[Luca Cracco] - DevOps: strumenti di automazione per Drupal8
 
[drupalday2017] - DevOps: strumenti di automazione per Drupal8
[drupalday2017] - DevOps: strumenti di automazione per Drupal8[drupalday2017] - DevOps: strumenti di automazione per Drupal8
[drupalday2017] - DevOps: strumenti di automazione per Drupal8
 

Testing in javascript