Writing quick andbeautifulautomation codeIntroduction to Hamcrest and LambdajCristian CoțoiMay 20 2013
•BDD (Agile) has new needs•What is hamcrest•What is lambdaj•ImprovementAgenda2
IN YOUR ZONEPurpose3•http://hamcrest.org•“Hamcrest it is not a testing library:it just happens that matchers arevery usefu...
IN YOUR ZONEWhy Hamcrest4•Clarity•assertThat(a, is(“3”));•assertEquals(a, “3”);•Speed•Not necessarily applicable for isNul...
IN YOUR ZONEHamcrest (some of the common matchers)5•Core• is•Logical• allOf - like Java &&• anyOf - like Java ||• not•Obje...
IN YOUR ZONEHamcrest (some of the common matchers)6•Collections• hasEntry, hasKey, hasValue - test a map contains an entry...
IN YOUR ZONEHamcrest7Imports
IN YOUR ZONEHamcrest Examples8•hasItem, hasItems
IN YOUR ZONEHamcrest examples9•hasItems (fail)
IN YOUR ZONEHamcrest examples10•hasEntry, hasValue, hasKey
IN YOUR ZONEHamcrest examples11•Numbers
IN YOUR ZONEHamcrest12•Actual automation code example
IN YOUR ZONELambdaj13•It’s designed to easily manipulate collections.•It uses Hamcrest matchers for filtering
IN YOUR ZONEWhy Lambdaj14•Readability•Collections are very common•Iterating over them is not dead-easy•Loops are hard to r...
IN YOUR ZONELambdaj examples15•Mathematical comparisons•Mathematical operations
IN YOUR ZONELambdaj examples16•Method invoke – add last name
IN YOUR ZONELambdaj examples17•Select
IN YOUR ZONELambdaj examples18•Youngest Papoi
IN YOUR ZONELambdaj automation example19
IN YOUR ZONELambdaj limitations20•Null or empty collection in forEach•Final class
IN YOUR ZONEBenefits21•Integration with Agile methodologies (BDD)•Clarity•Speed•Quick learning curve•Extensive application
IN YOUR ZONEThank you22Cristian Coțoi| Senior Tester
IN YOUR ZONEReferences23•Hamcrest• http://code.google.com/p/hamcrest/wiki/Tutorial• http://edgibbs.com/junit-4-with-hamcre...
Upcoming SlideShare
Loading in …5
×

Writing quick and beautiful automation code

720 views

Published on

This presentation is an intro to using hamcrest and lambdaj libraries. Nothing fancy, just some basics to get you started.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
720
On SlideShare
0
From Embeds
0
Number of Embeds
19
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • There is a new trend in BDD that empowers every member of the team to make changes in the code of the application & testing, starting with how the stories are connected with the code and should also be taken deeper.Hamcrest and lambdaj are 2 such libraries that make the code more human readable and easier to understand.
  • So why would I want to write assertions like this?ClarityassertThat() is a replacement for the traditionalassertEqual that is focused more on specifications than on BDDFirst one reads: “assert that a is 3”. The second reads “assert equals a (and) 3”SpeedThis is pretty str8 forward: “assert that list contains ‘banana’, ‘paple’, ‘papoi’”
  • Collections are hard to interrogate, especially when read from an outside source.It is easy to write a loop, but not so very easy to extract things from that loop or perform conversions or other operations on the items in the loop.
  • You can read this like:select from family items having…last nameequal to `Papoi`
  • 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.
  • Writing quick and beautiful automation code

    1. 1. Writing quick andbeautifulautomation codeIntroduction to Hamcrest and LambdajCristian CoțoiMay 20 2013
    2. 2. •BDD (Agile) has new needs•What is hamcrest•What is lambdaj•ImprovementAgenda2
    3. 3. IN YOUR ZONEPurpose3•http://hamcrest.org•“Hamcrest it is not a testing library:it just happens that matchers arevery useful for testing.”•Matching allows writing of lines thatare close-to natural language•Anagram of “matchers”•http://code.google.com/p/lambdaj/•Access collections without explicitloops•Operations include: itemsfiltering, converting, sorting, methodinvoking, concatenate etc.Hamcrest Lambdaj
    4. 4. IN YOUR ZONEWhy Hamcrest4•Clarity•assertThat(a, is(“3”));•assertEquals(a, “3”);•Speed•Not necessarily applicable for isNull() or isNotNull()•assertThat(list, hasItems(“banana”, “paple”, “papoi”));
    5. 5. IN YOUR ZONEHamcrest (some of the common matchers)5•Core• is•Logical• allOf - like Java &&• anyOf - like Java ||• not•Object• equalTo - test object equality using Object.equals• notNullValue, nullValue - test for null
    6. 6. IN YOUR ZONEHamcrest (some of the common matchers)6•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
    7. 7. IN YOUR ZONEHamcrest7Imports
    8. 8. IN YOUR ZONEHamcrest Examples8•hasItem, hasItems
    9. 9. IN YOUR ZONEHamcrest examples9•hasItems (fail)
    10. 10. IN YOUR ZONEHamcrest examples10•hasEntry, hasValue, hasKey
    11. 11. IN YOUR ZONEHamcrest examples11•Numbers
    12. 12. IN YOUR ZONEHamcrest12•Actual automation code example
    13. 13. IN YOUR ZONELambdaj13•It’s designed to easily manipulate collections.•It uses Hamcrest matchers for filtering
    14. 14. IN YOUR ZONEWhy Lambdaj14•Readability•Collections are very common•Iterating over them is not dead-easy•Loops are hard to read•Operations for collections: Convert, Index, Filter, Sort, Extractetc.
    15. 15. IN YOUR ZONELambdaj examples15•Mathematical comparisons•Mathematical operations
    16. 16. IN YOUR ZONELambdaj examples16•Method invoke – add last name
    17. 17. IN YOUR ZONELambdaj examples17•Select
    18. 18. IN YOUR ZONELambdaj examples18•Youngest Papoi
    19. 19. IN YOUR ZONELambdaj automation example19
    20. 20. IN YOUR ZONELambdaj limitations20•Null or empty collection in forEach•Final class
    21. 21. IN YOUR ZONEBenefits21•Integration with Agile methodologies (BDD)•Clarity•Speed•Quick learning curve•Extensive application
    22. 22. IN YOUR ZONEThank you22Cristian Coțoi| Senior Tester
    23. 23. IN YOUR ZONEReferences23•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

    ×