Software Testing & Test Driven Development

35
Software Testing e Test Driven Development Cassino, 29 ottobre 2015

Transcript of Software Testing & Test Driven Development

Page 1: Software Testing & Test Driven Development

Software Testinge

Test Driven Development

Cassino, 29 ottobre 2015

Page 2: Software Testing & Test Driven Development

Mi presento

Sergio Santoro

Web developer

PHP e Python

In Facile.it da un anno

@taueres

FOSS enthusiast

Contributor di MediaWiki e Pywikibot

Page 3: Software Testing & Test Driven Development

Problema

Monitorare l'evoluzione del software ed evitare carenze di affidabilità e di attinenza ai requisiti.

Soluzioni:– Ispezioni software (QA)

– Software Testing

Page 4: Software Testing & Test Driven Development

Software Testing

Cos'è?

Attività d'indagine per rilevare e prevenire carenze di affidabilità e correttezza in un sistema software.

Cosa NON è?Attività che ci consente di dimostrare l'assenza di errori in un sistema software

NO C

OM

PLETEZZA

NO C

OM

PLETEZZA

Page 5: Software Testing & Test Driven Development

Software Failures

Si ha una failure quando il software espone un comportamento erroneo.

Il comportamento effettivo è diverso da quello atteso.

Esempio: 5 + 2 = 6

Le failures sono spesso causate da bug d'implementazione.

Problemi hardware?PEBKAC?

Page 6: Software Testing & Test Driven Development

Obiettivi del software testing

● Rilevare il maggior numero di failures possibili.

● …

Il software testing non si occupa di:

● Individuare e sistemare i bug

● Migliorare il desing

● Formalizzare il problema

Solo?

Page 7: Software Testing & Test Driven Development

Testing, perché?

Il testing è un'attività costosa e non dà certezze.

Perché attuarla?– Meno failures

– Bolder developers

– Refactor più semplice

– Documentazione aggiuntiva per il codice

– Miglior design

– Ispezioni software aggiuntive

– Non riguarda solo i software developers

Page 8: Software Testing & Test Driven Development

Modalità d'esecuzione dei test

● Software Testing manuale

Eseguire il programma manualmente e verificarne il corretto funzionamento.

● Software Testing automatico

Esercitare il software mediante l'uso di altri programmi. Valutare automaticamente la sua correttezza.

Page 9: Software Testing & Test Driven Development

Software Testing manuale

Esempi: acceptance test, beta test.

Vantaggi:– Test molto efficaci con l'utente finale;

– Riproducono le effettive condizioni d'esercizio.

Svantaggi:– Attività molto lenta e costosa;

– Errori lato utente;

– Solo per software già funzionante e completo.

Page 10: Software Testing & Test Driven Development

Software Testing automatico

Vantaggi:– Test più veloci;

– Test eseguibili continuamente;

– Test più formali;

– Applicabile in qualsiasi stadio di sviluppo;

– Code coverage.

Svantaggi:– Ulteriore codice da produrre e manutenere;

– Simulazione ambiente di produzione;

– Solo per gli addetti ai lavori.

Page 11: Software Testing & Test Driven Development

Tipologie Testing automatico

Testing funzionale (End-to-end)

Esercitare il sistema nella sua interezza.

Testing d'integrazione

Testare l'interazione tra componenti

Testing d'unità

Esercitare una singola componente

Page 12: Software Testing & Test Driven Development

Esempio

Page 13: Software Testing & Test Driven Development

Testing d'unità

Esercitare una singola componente in un ambiente “ideale” per verificare il suo corretto funzionamento.

Vantaggi:– Bug ben localizzati.

– I risultati non sono influenzati da altre componenti.

– Test molto veloci e semplici.

Page 14: Software Testing & Test Driven Development

Strumenti per il Testing d'unità

Testing-frameworks xUnit.

Ogni linguaggio ha la sua implementazione:

– Java, JUnit

– Python, PyUnit

– PHP, PHPUnit

Page 15: Software Testing & Test Driven Development

ComponenteComponenteda testareda testare

Dip2 Dip3

Dip1 UserStub1

Stub2 Stub3

TestDriver

Page 16: Software Testing & Test Driven Development

Test driver

Esercitare la componente under-test e verificare la correttezza dell'output.

Responsabilità:– Definire le condizioni d'ingresso

– Definire l'oracolo

– Esercitare la componente

– Confrontare i risultati con l'oracolo

Page 17: Software Testing & Test Driven Development

Test driver

Si realizza mediante la scrittura di uno o più test cases

class ArrayUtilsTest extends \PHPUnit_Framework_TestCase{ public function testSumArray() { $inputArray = [1, 2, 3]; $expected = 6; $arrayUtils = new ArrayUtils();

$actual = $arrayUtils->sumArray($inputArray);

$this->assertEquals($expected, $actual); }}

Page 18: Software Testing & Test Driven Development

Stub e Mock

Rimpiazzare le componenti reali con implementazioni fittizie.

Stub: Simula il comportamento della componente reale.

Mock: Stub + expectations.

Page 19: Software Testing & Test Driven Development

Esempio

Page 20: Software Testing & Test Driven Development

Esempio Stub

class SubscriptionBuilderStub extends SubscriptionBuilder{ public function __construct() {}

// Overrides public function getSubscriptionFrom(Category $c, User $user) { return new Subscription(); }}

Page 21: Software Testing & Test Driven Development

Esempio Mockclass SubscriptionBuilderMock extends SubscriptionBuilder{ public $userArg;

public function __construct() {}

// Overrides public function getSubscriptionFrom(Category $c, User $user) { $this->userArg = $user; return new Subscription(); }

public function checkExpectations($expectedUser) { return $this->userArg === $expectedUser; }}

Page 22: Software Testing & Test Driven Development

Prophecy

Libreria inclusa in PHPUnit che semplifica l'implementazione di Stub e Mock

Prophet Object Prophecy Method Prophecy

Stub / MockCheck

Predictions

1 * 1 *

1

1

Page 23: Software Testing & Test Driven Development

Prophecy

function testWithStub(){ $p = new Prophecy\Prophet();

$builderStub = $p->prophesize('SubscriptionBuilder'); $builderStub->getSubscriptionFrom(Argument::cetera()) ->willReturn(new Subscription());

$stub = $builderStub->reveal();

// ...}

Page 24: Software Testing & Test Driven Development

function testWithMock(){ $p = new Prophecy\Prophet();

$user = new User();

$builderMock = $p->prophesize('SubscriptionBuilder'); $builderMock->getSubscriptionFrom(Argument::any(), $user) ->shouldBeCalledTimes(1) ->willReturn(new Subscription());

$mock = $builderMock->reveal();

// ...

$p->checkPredictions();}

Prophecy

Page 25: Software Testing & Test Driven Development

Test Driven Development

Cos'è?

È una metodologia per lo sviluppo software con approccio Test-first.

I test occupano una posizione predominante.

“Come usare una componente?”

Solo testing d'unità.

Page 26: Software Testing & Test Driven Development

Normal flow

Design

Implement

Test

Page 27: Software Testing & Test Driven Development

TDD flow

Design

Test

Implement

Page 28: Software Testing & Test Driven Development

Principi del TDD

● Si può aggiungere una nuova funzionalità solo quando c'è un test corrispondente che fallisce.

● Si può riparare un bug solo quando esso viene esercitato da un test che sta fallendo.

● Il Refactoring è l'unica operazione concessa con test verdi.

Green

- New features - Bug fixing

- Refactoring

Red

Page 29: Software Testing & Test Driven Development

Test Driven Development

L'attività di sviluppo viene suddivisa in molte piccole iterazioni.

Ogni iterazione è indipendente dalle altre.

Problem Solution

Partialsolution

Partialsolution

Page 30: Software Testing & Test Driven Development

Iterazione TDD

Comprendere, formalizzare il problema.Definire l'API.

Rolvere il problemasenza preoccuparsi del design.Migliorare la

qualità del codice senza aggiungere nuove funzionalità.

Page 31: Software Testing & Test Driven Development

Iterazione TDD

Quanto devono essere ampie le iterazioni?

Molto soggettivo:– Comprensione del problema

– Comprensione del sistema

– Boldness

Generalmente durano una manciata di minuti.

Page 32: Software Testing & Test Driven Development

Iterazione TDD

Quante iterazioni?

Almeno una iterazione per ogni caso d'uso della componente.

Esempio:

+ * + = +

- * + = -

- * - = +

3 casi d'uso -> 3 iterazioni

Page 33: Software Testing & Test Driven Development

Esempio iterazioni TDD

Problema: somma tra diverse valute.

1 € + 1 $ = ?

It. 1: somma con un solo addendo

It. 2: somma di addendi con la stessa valuta

It. 3: somma di addendi con valuta differente

Page 34: Software Testing & Test Driven Development

Vantaggi TDD

● Forte comprensione del problema● Continuo feedback, più boldness● Risolvere il problema ≠ Design● Be the user of your code!● Divide et impera

Page 35: Software Testing & Test Driven Development

TDD nel ciclo di sviluppo

Analisi delproblema

Analisi delsistema

DesignAlto livello

TDDFunctional

TestAcceptance

Test

Problem System Implementation