You are on page 1of 72

Unit Testing Test Driven Development no TDD uses

Unit testing
CDL - Cursul 5

Mihai Maruseac
mihai.maruseac@rosedu.org
20 mai 2010
ROSEdu

1 / 25
Unit Testing Test Driven Development no TDD uses

Before we begin

> echo ”Test” | grep T


Test
(linux.c, 36): Cannot wait for child: No child processes

> echo ”Test” | grep T


Test
> exit

2 / 25
Unit Testing Test Driven Development no TDD uses

Studiu de caz

1. Pornim cu un nucleu mic...

3 / 25
Unit Testing Test Driven Development no TDD uses

Studiu de caz

2. Adăugăm la el element cu element...

4 / 25
Unit Testing Test Driven Development no TDD uses

Studiu de caz

3. S, i ajungem aici.

5 / 25
Unit Testing Test Driven Development no TDD uses

Solut, ii

Ne structurăm mai bine codul de la ı̂nceput.

6 / 25
Unit Testing Test Driven Development no TDD uses

Solut, ii

Ne structurăm mai bine codul de la ı̂nceput.


Putem ajunge la buguri ce vor fi vizibile abia la integrarea a două
port, iuni de cod.

6 / 25
Unit Testing Test Driven Development no TDD uses

Solut, ii

Ne structurăm mai bine codul de la ı̂nceput.


Putem ajunge la buguri ce vor fi vizibile abia la integrarea a două
port, iuni de cod.
Sau, putem testa fiecare unitate de cod (funct, ie, clasă etc.)

6 / 25
Unit Testing Test Driven Development no TDD uses

Ce presupune unit testing?

garant, ie = code that just works

7 / 25
Unit Testing Test Driven Development no TDD uses

Ce presupune unit testing?

garant, ie = code that just works


unitate = cea mai mică bucată independentă din cod

7 / 25
Unit Testing Test Driven Development no TDD uses

Ce presupune unit testing?

garant, ie = code that just works


unitate = cea mai mică bucată independentă din cod
scop = izolare bucăt, i cod, demonstrare corectitudine

7 / 25
Unit Testing Test Driven Development no TDD uses

Unit testing s, i scrierea codului

ÎNAINTE : detaliere aspecte proiect ı̂ntr-o manieră folositoare

8 / 25
Unit Testing Test Driven Development no TDD uses

Unit testing s, i scrierea codului

ÎNAINTE : detaliere aspecte proiect ı̂ntr-o manieră folositoare


SCRIERE : se evită scrierea de prea mult cod

8 / 25
Unit Testing Test Driven Development no TDD uses

Unit testing s, i scrierea codului

ÎNAINTE : detaliere aspecte proiect ı̂ntr-o manieră folositoare


SCRIERE : se evită scrierea de prea mult cod
MODIFICARE : garantează că nu se strică ce mergea deja

8 / 25
Unit Testing Test Driven Development no TDD uses

Cum testăm?

manual: assert sau if

9 / 25
Unit Testing Test Driven Development no TDD uses

Cum testăm?

manual: assert sau if


automat: folosim un framework existent

9 / 25
Unit Testing Test Driven Development no TDD uses

Exercit, iu 1

Task 1
Scriet, i o funct, ie mystrrev ce va realiza inversarea unui s, ir de caractere
dat argument. Funct, ia va returna un pointer către s, irul rezultat.

10 / 25
Unit Testing Test Driven Development no TDD uses

Exercit, iu 1

Task 1
Scriet, i o funct, ie mystrrev ce va realiza inversarea unui s, ir de caractere
dat argument. Funct, ia va returna un pointer către s, irul rezultat.

Task 2
Scriet, i o funct, ie check mystrrev ce va testa funct, ia anterioară:
mystrrev(mystrrev(sir)) == sir

10 / 25
Unit Testing Test Driven Development no TDD uses

Exercit, iu 1

Task 1
Scriet, i o funct, ie mystrrev ce va realiza inversarea unui s, ir de caractere
dat argument. Funct, ia va returna un pointer către s, irul rezultat.

Task 2
Scriet, i o funct, ie check mystrrev ce va testa funct, ia anterioară:
mystrrev(mystrrev(sir)) == sir

At, i tratat s, i cazul argumentelor nule?

10 / 25
Unit Testing Test Driven Development no TDD uses

Exercit, iu 1

Task 1
Scriet, i o funct, ie mystrrev ce va realiza inversarea unui s, ir de caractere
dat argument. Funct, ia va returna un pointer către s, irul rezultat.

Task 2
Scriet, i o funct, ie check mystrrev ce va testa funct, ia anterioară:
mystrrev(mystrrev(sir)) == sir

At, i tratat s, i cazul argumentelor nule?


At, i verificat memoria?

10 / 25
Unit Testing Test Driven Development no TDD uses

Testing vs Coding

Pe ce punem mai mult accent? Scris teste sau scris cod?

11 / 25
Unit Testing Test Driven Development no TDD uses

Testing vs Coding

Pe ce punem mai mult accent? Scris teste sau scris cod?


...

11 / 25
Unit Testing Test Driven Development no TDD uses

Testing vs Coding

Pe ce punem mai mult accent? Scris teste sau scris cod?


...
...

11 / 25
Unit Testing Test Driven Development no TDD uses

Testing vs Coding

Pe ce punem mai mult accent? Scris teste sau scris cod?


...
...
Depinde

11 / 25
Unit Testing Test Driven Development no TDD uses

Test Driven Development (TDD)

scriem testele ı̂ntâi

12 / 25
Unit Testing Test Driven Development no TDD uses

Test Driven Development (TDD)

scriem testele ı̂ntâi


de fiecare dată când modificăm codul verificăm să nu crească
numărul testelor picate (s, i care sunt ele)

12 / 25
Unit Testing Test Driven Development no TDD uses

Test Driven Development (TDD)

scriem testele ı̂ntâi


de fiecare dată când modificăm codul verificăm să nu crească
numărul testelor picate (s, i care sunt ele)
pică un test trecut anterior = am modificat ceva ce nu trebuia
modificat (don’t commit)

12 / 25
Unit Testing Test Driven Development no TDD uses

Test Driven Development (TDD)

scriem testele ı̂ntâi


de fiecare dată când modificăm codul verificăm să nu crească
numărul testelor picate (s, i care sunt ele)
pică un test trecut anterior = am modificat ceva ce nu trebuia
modificat (don’t commit)
toate testele trecute = proiect terminat

12 / 25
Unit Testing Test Driven Development no TDD uses

Test Driven Development (TDD)

scriem testele ı̂ntâi


de fiecare dată când modificăm codul verificăm să nu crească
numărul testelor picate (s, i care sunt ele)
pică un test trecut anterior = am modificat ceva ce nu trebuia
modificat (don’t commit)
toate testele trecute = proiect terminat
apare un bug nou → se scriu noi teste s, i repetăm secvent, a

12 / 25
Unit Testing Test Driven Development no TDD uses

1 pic = 1000 words

13 / 25
Unit Testing Test Driven Development no TDD uses

Exercit, iu 2

Descriere
Vom implementa funct, ia mystrcmp ce va compara dacă două s, iruri de
caractere sunt egale. Vom ı̂ncepe prin a defini testul s, i apoi vom scrie
codul.

Task 1
Scriet, i o funct, ie de test ce va verifica că mystrcmp returnează ce
trebuie. Nu implementat, i mystrcmp, scriet, i doar un stub.

14 / 25
Unit Testing Test Driven Development no TDD uses

Exercit, iu 2

Descriere
Vom implementa funct, ia mystrcmp ce va compara dacă două s, iruri de
caractere sunt egale. Vom ı̂ncepe prin a defini testul s, i apoi vom scrie
codul.

Task 1
Scriet, i o funct, ie de test ce va verifica că mystrcmp returnează ce
trebuie. Nu implementat, i mystrcmp, scriet, i doar un stub.

Task 2
Implementat, i mystrcmp, pe etape. Rulat, i funct, ia de test de fiecare dată
când compilat, i.

14 / 25
Unit Testing Test Driven Development no TDD uses

Timeline

15 / 25
Unit Testing Test Driven Development no TDD uses

6 motive pentru TDD

1 Focus

16 / 25
Unit Testing Test Driven Development no TDD uses

6 motive pentru TDD

1 Focus
2 S, tii când ai terminat

16 / 25
Unit Testing Test Driven Development no TDD uses

6 motive pentru TDD

1 Focus
2 S, tii când ai terminat
3 Dacă un unit merge o dată va merge mereu

16 / 25
Unit Testing Test Driven Development no TDD uses

6 motive pentru TDD

1 Focus
2 S, tii când ai terminat
3 Dacă un unit merge o dată va merge mereu
4 Cod mult mai modular

16 / 25
Unit Testing Test Driven Development no TDD uses

6 motive pentru TDD

1 Focus
2 S, tii când ai terminat
3 Dacă un unit merge o dată va merge mereu
4 Cod mult mai modular
5 Cod mai curat s, i mai us, or de ı̂nt, eles

16 / 25
Unit Testing Test Driven Development no TDD uses

6 motive pentru TDD

1 Focus
2 S, tii când ai terminat
3 Dacă un unit merge o dată va merge mereu
4 Cod mult mai modular
5 Cod mai curat s, i mai us, or de ı̂nt, eles
6 Satisfact, ia de a vedea că lucrurile merg

16 / 25
Unit Testing Test Driven Development no TDD uses

6 motive pentru TDD

1 Focus
2 S, tii când ai terminat
3 Dacă un unit merge o dată va merge mereu
4 Cod mult mai modular
5 Cod mai curat s, i mai us, or de ı̂nt, eles
6 Satisfact, ia de a vedea că lucrurile merg
7 Progres vizibil

16 / 25
Unit Testing Test Driven Development no TDD uses

6 motive pentru TDD

1 Focus
2 S, tii când ai terminat
3 Dacă un unit merge o dată va merge mereu
4 Cod mult mai modular
5 Cod mai curat s, i mai us, or de ı̂nt, eles
6 Satisfact, ia de a vedea că lucrurile merg
7 Progres vizibil
8 “Documentare” cod

16 / 25
Unit Testing Test Driven Development no TDD uses

No silver bullet

Nu merge mereu

17 / 25
Unit Testing Test Driven Development no TDD uses

No silver bullet

Nu merge mereu
Prea multe teste

17 / 25
Unit Testing Test Driven Development no TDD uses

No silver bullet

Nu merge mereu
Prea multe teste
Linii de test de 100 de ori mai multe ca linii de cod utile

17 / 25
Unit Testing Test Driven Development no TDD uses

No silver bullet

Nu merge mereu
Prea multe teste
Linii de test de 100 de ori mai multe ca linii de cod utile
Teste nesemnificative

17 / 25
Unit Testing Test Driven Development no TDD uses

No silver bullet

Nu merge mereu
Prea multe teste
Linii de test de 100 de ori mai multe ca linii de cod utile
Teste nesemnificative
Teste pe cazuri nespecificate

17 / 25
Unit Testing Test Driven Development no TDD uses

No silver bullet

Nu merge mereu
Prea multe teste
Linii de test de 100 de ori mai multe ca linii de cod utile
Teste nesemnificative
Teste pe cazuri nespecificate
Teste incorect specificate

17 / 25
Unit Testing Test Driven Development no TDD uses

No silver bullet

Nu merge mereu
Prea multe teste
Linii de test de 100 de ori mai multe ca linii de cod utile
Teste nesemnificative
Teste pe cazuri nespecificate
Teste incorect specificate
Teste depinzând de implementare

17 / 25
Unit Testing Test Driven Development no TDD uses

No silver bullet

Nu merge mereu
Prea multe teste
Linii de test de 100 de ori mai multe ca linii de cod utile
Teste nesemnificative
Teste pe cazuri nespecificate
Teste incorect specificate
Teste depinzând de implementare
Uneori testarea durează prea mult

17 / 25
Unit Testing Test Driven Development no TDD uses

No silver bullet

Nu merge mereu
Prea multe teste
Linii de test de 100 de ori mai multe ca linii de cod utile
Teste nesemnificative
Teste pe cazuri nespecificate
Teste incorect specificate
Teste depinzând de implementare
Uneori testarea durează prea mult
Testarea manuală poate fi sărită

17 / 25
Unit Testing Test Driven Development no TDD uses

No silver bullet

Nu merge mereu
Prea multe teste
Linii de test de 100 de ori mai multe ca linii de cod utile
Teste nesemnificative
Teste pe cazuri nespecificate
Teste incorect specificate
Teste depinzând de implementare
Uneori testarea durează prea mult
Testarea manuală poate fi sărită
not thinking ahead

17 / 25
Unit Testing Test Driven Development no TDD uses

Not thinking ahead

18 / 25
Unit Testing Test Driven Development no TDD uses

Testarea codului scris deja

goes without saying

19 / 25
Unit Testing Test Driven Development no TDD uses

Testarea codului scris deja

goes without saying


se poate face manual, prin script sau prin cod

19 / 25
Unit Testing Test Driven Development no TDD uses

Testarea codului scris deja

goes without saying


se poate face manual, prin script sau prin cod
ignorată

19 / 25
Unit Testing Test Driven Development no TDD uses

Integrarea cu SCM-uri

git bisect

20 / 25
Unit Testing Test Driven Development no TDD uses

Integrarea cu SCM-uri

git bisect
testăm manual?

20 / 25
Unit Testing Test Driven Development no TDD uses

Integrarea cu SCM-uri

git bisect
testăm manual?
putem face un script pentru testare

20 / 25
Unit Testing Test Driven Development no TDD uses

Integrarea cu SCM-uri - Demo


Descriere
Descărcat, i sursele unui proiect aflate pe git. Repository-ul are adresa:
git://gitorious.org/pali/pali.git
Avem de afis, at toate palindroamele dintr-o frază.

Problema
Compilat, i sursele s, i rulat, i programul. Vom ı̂ncerca să găsim commit-ul
ce a introdus eroarea folosind git bisect.

21 / 25
Unit Testing Test Driven Development no TDD uses

Integrarea cu SCM-uri - Demo


Descriere
Descărcat, i sursele unui proiect aflate pe git. Repository-ul are adresa:
git://gitorious.org/pali/pali.git
Avem de afis, at toate palindroamele dintr-o frază.

Problema
Compilat, i sursele s, i rulat, i programul. Vom ı̂ncerca să găsim commit-ul
ce a introdus eroarea folosind git bisect.

Solut, ie
Descărcat, i cele două fis, iere bash de la
http://swarm.cs.pub.ro/∼mihai/cdl/2010/.
Rulat, i ut.sh s, i vet, i obt, ine commit-ul ce a introdus gres, eala. Corectat, i-o
introducând un nou commit corect (nu facet, i push).

21 / 25
Unit Testing Test Driven Development no TDD uses

Stress testing

Robustet, ea programului

22 / 25
Unit Testing Test Driven Development no TDD uses

Stress testing

Robustet, ea programului
Teste peste limitele operării normale

22 / 25
Unit Testing Test Driven Development no TDD uses

Stress testing

Robustet, ea programului
Teste peste limitele operării normale
Paralelism, sisteme de operare, middleware, biblioteci, memory leaks

22 / 25
Unit Testing Test Driven Development no TDD uses

Stress testing

Robustet, ea programului
Teste peste limitele operării normale
Paralelism, sisteme de operare, middleware, biblioteci, memory leaks
Zburat, i cu un avion al cărui software nu a fost testat astfel?

22 / 25
Unit Testing Test Driven Development no TDD uses

Concluzii

Testing is good

23 / 25
Unit Testing Test Driven Development no TDD uses

Concluzii

Testing is good

23 / 25
Unit Testing Test Driven Development no TDD uses

Concluzii

Testing is good if done properly


“Testing can show the presence of bugs, but not the absence”

23 / 25
Unit Testing Test Driven Development no TDD uses

Concluzii

Testing is good if done properly


“Testing can show the presence of bugs, but not the absence”
suntem siguri că testăm tot?

23 / 25
Unit Testing Test Driven Development no TDD uses

Concluzii

Testing is good if done properly


“Testing can show the presence of bugs, but not the absence”
suntem siguri că testăm tot?
“Testing is the process of executing a program with the intent of
finding errors”

23 / 25
Unit Testing Test Driven Development no TDD uses

Concluzii

Testing is good if done properly


“Testing can show the presence of bugs, but not the absence”
suntem siguri că testăm tot?
“Testing is the process of executing a program with the intent of
finding errors”
Testarea codului duce s, i la cod lizibil, module separate etc.

23 / 25
Unit Testing Test Driven Development no TDD uses

Întrebări?

24 / 25
Unit Testing Test Driven Development no TDD uses

Bibliografie

Test Driven Development


Motive pentru TDD
Es, ecuri TDD
Unit testing ı̂n Python
Framework pentru unit testing ı̂n C
QuickCheck - Haskell
JUnit - Java
http://www.youtube.com/watch?v=XP4o0ArkP4s

25 / 25

You might also like