SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Introduction to Hamcrest and LambdaJ
Cristian Coțoi
1. BDD (Agile)

2. Ce este hamcrest
3. Ce este lambdaj
4. Beneficii
Ce este BDD…

Stories & Scenarios

As a [X]
I want [Y]
So that [Z]
Acceptance Testing
Driven Development



Atenția  business
Scrise de întreaga
echipă
 … pentru toată echipa
 Testele pică până este
implementat story-ul








Test Driven
Development

Atenția  partea
tehnică
 Scrise de developeri
 …pentru developeri
 Testele pică până este
implementată
funcționalitatea




Ambele sunt scrise înaintea codului aplicației
Arată foarte similar
... Dar nu se pot substiti una pe cealaltă
Story

• Title: Customer pays bills
through internet banking
• As a customer,
• I want to pay bills through
internet banking,
• So that I don’t have to make a
trip to each supplier.

Scenario 1

• Scenario 1: There is enough
credit
• Given the account is in credit
• And the user is properly
authenticated
• When the customer attempts
to make a payment to a supplier
• And he inputs the correct bill
details
• Then ensure the transaction is
recorded
• And ensure the amount of
money is debited
HAMCREST







http://hamcrest.org
“Hamcrest it is not a
testing library: it just
happens that matchers are
very useful for testing.”
Permite scrierea codului
într-un limbaj aproape
natural
Anagramă de la
“matchers”

LAMBDAJ




http://code.google.com/p/l
ambdaj/
Accesează colecții fără
bucle (for, while etc.)
Operații:
filtrare, conversie, sortare, i
nvocare de metode etc.




JUnit
Clarity
 assertEquals(a, “3”);
 assertThat(a, is(“3”));
Hamcrest
Speed
 Not necessarily applicable for isNull() or isNotNull()
 assertThat(list, hasItems(“banana”, “minion”, “unicorn”));





Core
 is
Logical
 allOf - like Java &&
 anyOf - like Java ||
 not
Object
 equalTo - test object equality using Object.equals
 notNullValue, nullValue - test for null
Collections
 hasEntry, hasKey, hasValue - test a map contains an entry, key or value
 hasItem, hasItems - test a collection contains elements
 Number
 closeTo - test floating point values are close to a given value
 greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo - test
ordering
 Text
 equalToIgnoringCase
 containsString, endsWith, startsWith - test string matching

Imports


hasItem, hasItems

Varianta
clasică


hasItem, hasItems
Folosind
hamcrest


hasItems (când verificarea „pică”)


hasEntry, hasValue, hasKey


Numbers


Tiny automation code


Întrebări?
Imports



Este conceput pentru manipularea colecțiilor
Poate folosi Hamcrest pentru matching
Lizibilitate
 Colecțiile sunt foarte comune
 Iterațiile nu sunt extraordinar de simple
 ...și sunt greu de citit
 Operații lambdaj: conversie, filtrare, sortare, extragere etc.



Comparații matematice (cu hamcrest)

• Sume


Invocarea unei metode

Varianta
clasică
Folosind
LambdaJ


Select


Select


Cel mai tânăr Papoi




Colecții Null sau goale în forEach
Clase finale
Very large collections


Întrebări?








Integrare cu metodologiile Agile (BDD)
Claritate
Viteză
Curbă de învățare rapidă
Aplicații numeroase



Cristian Coțoi – SDET
cotoic@amazon.com




Hamcrest
 http://code.google.com/p/hamcrest/wiki/Tutorial
 http://edgibbs.com/junit-4-with-hamcrest/
 http://rafaelnaufal.com/blog/2010/03/15/using-hamcrest-and-junit/ writing your own matchers
LambdaJ
 http://code.google.com/p/lambdaj/
 http://code.google.com/p/lambdaj/wiki/LambdajFeatures

Weitere ähnliche Inhalte

Andere mochten auch

Software testing expert evangelization
Software testing expert evangelizationSoftware testing expert evangelization
Software testing expert evangelizationTestCampRO
 
Distributed load testing (Local vs Cloud)
Distributed load testing (Local vs Cloud)Distributed load testing (Local vs Cloud)
Distributed load testing (Local vs Cloud)TestCampRO
 
Atlandes engineeering1
Atlandes engineeering1Atlandes engineeering1
Atlandes engineeering1Sekhar Varier
 
Prepare your case: key steps to a successful presentation
Prepare your case: key steps to a successful presentationPrepare your case: key steps to a successful presentation
Prepare your case: key steps to a successful presentationTestCampRO
 
How to build a proper software staging environment for testing
How to build a proper software staging environment for testing How to build a proper software staging environment for testing
How to build a proper software staging environment for testing TestCampRO
 
Testing world seen through the thick lens of a business analyst
Testing world seen through the thick lens of a business analyst Testing world seen through the thick lens of a business analyst
Testing world seen through the thick lens of a business analyst TestCampRO
 
Performance testing for web applications – techniques, metrics and profiling
Performance testing for web applications – techniques, metrics and profilingPerformance testing for web applications – techniques, metrics and profiling
Performance testing for web applications – techniques, metrics and profilingTestCampRO
 
Test Automation and Continuous Integration
Test Automation and Continuous Integration Test Automation and Continuous Integration
Test Automation and Continuous Integration TestCampRO
 
First steps in becoming a better tester
First steps in becoming a better testerFirst steps in becoming a better tester
First steps in becoming a better testerTestCampRO
 
Gabay sa-guro baitang7-ikatlong-markahan_08032012
Gabay sa-guro baitang7-ikatlong-markahan_08032012Gabay sa-guro baitang7-ikatlong-markahan_08032012
Gabay sa-guro baitang7-ikatlong-markahan_08032012Rosannie Doria
 

Andere mochten auch (11)

Software testing expert evangelization
Software testing expert evangelizationSoftware testing expert evangelization
Software testing expert evangelization
 
Distributed load testing (Local vs Cloud)
Distributed load testing (Local vs Cloud)Distributed load testing (Local vs Cloud)
Distributed load testing (Local vs Cloud)
 
Tim
TimTim
Tim
 
Atlandes engineeering1
Atlandes engineeering1Atlandes engineeering1
Atlandes engineeering1
 
Prepare your case: key steps to a successful presentation
Prepare your case: key steps to a successful presentationPrepare your case: key steps to a successful presentation
Prepare your case: key steps to a successful presentation
 
How to build a proper software staging environment for testing
How to build a proper software staging environment for testing How to build a proper software staging environment for testing
How to build a proper software staging environment for testing
 
Testing world seen through the thick lens of a business analyst
Testing world seen through the thick lens of a business analyst Testing world seen through the thick lens of a business analyst
Testing world seen through the thick lens of a business analyst
 
Performance testing for web applications – techniques, metrics and profiling
Performance testing for web applications – techniques, metrics and profilingPerformance testing for web applications – techniques, metrics and profiling
Performance testing for web applications – techniques, metrics and profiling
 
Test Automation and Continuous Integration
Test Automation and Continuous Integration Test Automation and Continuous Integration
Test Automation and Continuous Integration
 
First steps in becoming a better tester
First steps in becoming a better testerFirst steps in becoming a better tester
First steps in becoming a better tester
 
Gabay sa-guro baitang7-ikatlong-markahan_08032012
Gabay sa-guro baitang7-ikatlong-markahan_08032012Gabay sa-guro baitang7-ikatlong-markahan_08032012
Gabay sa-guro baitang7-ikatlong-markahan_08032012
 

Writing quick and beautiful automation code

Hinweis der Redaktion

  1. Cristian CoțoiÎn următoarele 40 de minute, vă voi vorbi despre cum să scriem teste automate rapid și care sunt și ușor de citit și înțeles atât pentru testeri cât și pentru ceilalți colegi de echipă.Pentru început, ca o persoană curioasă, aș vrea să știu câți dintre voi:- Deja scriu teste automate- Nu scriu, dar își doresc să învețePe lângă aspectele tehnice ale acestei prezentări, vă voi explica și câteva principii Agile și motivul pentru care sunt strâns conectate cu cele 2 librării.Metodologia Agile a căpătat amploare și treptat modul de dezvoltare software s-a adaptat. Una dintre principiile Agile este că fiecare are capacitatea de a se implica în aproape orice task al echipei, atunci când este necesar.Acest lucru nu este tocmai aplicabil dacă ne gândim la project manageri care să scrie cod, dar același lucru nu este neaparat valabil dacă vorbim despre project manageri care să scrie teste.
  2. Voi explica puțin ce înseamnă BDD și care este legătura lui cu metodologia Agile.......Și nu în ultimul rând, de ce am folosi aceste librării.
  3. BDD a evoluat din practicile agile și a fost conceput pentru a le face mai accesibile și eficiente pentru echipele de dezvoltare. Iar prin echipe de dezvoltare mă refer la întreaga echipă, nu doar programatorii.Principalul beneficiu al BDD este capacitatea de a scrie testele într-un limbaj accesibil tuturor membrilor echipei.Iar aceasta se face prin intermediul poveștilor și scenariilor.Și ca să înțelegeți la ce mă refer un story are următoarea structură:As a...X reprezintă o persoană, Y reprezintă o funcționalitate iar Z reprezintă un beneficiu obținut prin implementarea funcționalității. Această abordare are capacitatea de a defini valoarea unei funcționalități încă înainte de a fi implementată. Dacă nu este astfel definită atunci există riscul de a implementa o funcționalitate de tipul „pentru-că-așa-vrea-clientul”. Aici este momentul în care unele cerințe „mistice” sunt filtrate corespunzător.Exemplu...
  4. Există 2 implementări ale BDD în momentul de față: ATDD și TDD
  5. Ca un studiu de cazam avea un story pentru achitarea facturilor prin internet banking.Iar un scenariu `happy flow` ar fi…
  6. Deci la ce am putea folosi hamcrest și lambdaj?În câteva cuvinte....Hamcrest nu este o librărie de testare. Pur și simplu este foarte utilă pentru testare.Pe celaltă parte, lambdaj permite accesarea colecțiilor fără bucle
  7. De ce aș vrea să scriu verificările cu hamcrest?În primul rând pentru claritate. De exemplu, prima variantă reprezintă expresia clasică de verificare a unei variabile – metodă JUnit.Cea de-a doua este scrisă folosind metodele hamcrest.Diferența aceasta nu este foarte mare atunci când analizăm doar o linie, dar face o diferență foarte mare în momentul în care avem câteva zeci de verificări și elementele devin din ce în ce mai complexe.În al 2-lea rând pentru viteză:E destul de simplu: “assert that list has items ‘banana’, ‘minion’, ‘unicorn’”
  8. Ce ne oferă hamcrest ca
  9. Avem matchers pentru colecții, numere, textPentru numere poate părea o complicare inutilă să scriu toată denumierea, dar o să vă arăt peste câteva slide-uri un exemplu care să vă explice de ce este mai bine.
  10. Dacă vom dori să folosim hamcrest, trebuie să declarăm acest import static
  11. Și un exemplu de cod dintr-un test, unde se verifică dacă într-un blog, în lista de articole afișate există și ultimul articol creat de noi (mai exact de codul de testare).
  12. Dacă vom dori să folosim și hamcrest împreună cu lambdaj, trebuie să declarăm acest import static.
  13. LambdaJ, foarte pe scurt:E folosit pentru manipularea colecțiilorPoate folosi hamcrest pentru matching
  14. Colecțiile sunt foarte des folosite în cod, dar lucrul cu ele nu este extraordinar ușor, iar citirea codului nici atât.De câte ori nu vi s-a întâmplat să reveniți asupra unui cod scris de voi cu câteva luni în urmă și să nu reușiți să înțelegeți cum funcționează?Dar pe codul unui coleg? Și nu mă refer aici la câteva laboratoare de facultate cu 10 linii de cod, ci la proiecte cu câteva mii de linii cod.
  15. You can read this like:select from family items having…last nameequal to `Papoi`
  16. You can read this like:select from family items having…last nameequal to `Papoi`
  17. The current implementation of forEach cannot handle null or empty collections – this will just throw an exception.The same is true for a final class that is used with on()On average it is about 2-3 times slower, but depending on the collection it sometimes can be 6 times slower.