About ApprovalTests for Better Unit Testing


Published on

Deck for talk at SoCalCodeCamp on ApprovalTests for Unit Testing

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • www.ApprovalTests.com-->http://approvaltests.sourceforge.net/
  • Idea here is common misconceptions, i.e. ‘why I do not do TDD’Why TDD == spec, feedback, granularity, regressionIke to tell story of ‘why’ TDD change lock.
  • About Unit Testing Legacy Code - http://c2.com/cgi/wiki?UnitTestingLegacyCodePhoto credit - http://www.typemock.com/blog/2011/06/21/who-wants-a-free-typemock-t-shirt-2/
  • Image credit - http://www.elinext.com/test-driven-development
  • “Makes the invisible in your code more visible” – “the best ToString() you’ve ever had!”
  • Simple C# demo, show Console (breakpoint), Assert, Approve, Reporter with Add, then with Explain, then into Reporters (DiffHTMLDiff)Here are the steps from www.ApprovalTests.comStep 1: Create your scenario Step 2: Write that scenario in EnglishStep 3: Translate English to Code (TDD – name objects / methods FIRST)Step 4: Create Code so it works (Implement named objects / methods)Step 5: Run your test for feedback while you codeStep 6: Approve result so it continues to workStep 7: Change the requirement Step 8: See the new solution Step 9: Re-approve so it continues to work
  • Photo credit - http://www.cakewrecks.com/Make the point that you can use ApprovalTests at all levels of testing granularity – both TDD (small) and BDD (big) AND in between – Approvals == better Asserts
  • There is a header and lambda function for arrays that allows for thetesting circle.var word = "Llewellyn";Approvals.Approve(word, word.ToCharArray(), (c) => c+ " => " + (int)c);would produce:LlewellynL => 76l => 108e => 101w => 119e => 101l => 108l => 108y => 121n => 110
  • Re-iterate the testing cycle steps for TDD for new code, maybe mention BDD hereShow that the first illustration is incomplete – doesn’t include the ‘think it out / visualize it’ step
  • http://blog.approvaltests.com/2011/12/using-reporters-in-approval-tests.htmlReporters can be put at multiple levels, closest wins (assembly, class, method)Can use mult-reporter(s), separate with a commaPoint out that the reporters vary for the different (language) implementations, i.e. C#, Java, etc.. Point to the documentation!DiffReporter is a cascading reporter (supports multiple)Reporter are VIEWERS for results on test FAILURE onlyIncluded reporters which are not listed are also WinDiff, BeyondCompare and CodeCompare (http://www.devart.com/codecompare/)Types – text, image, HTML, more?List of free differencing tools - http://www.thefreecountry.com/programming/filecomparison.shtml
  • Demo the VS2010 plug in, right-click ‘Approve’ – only works when test has not passed already.Contents provided are serialized to the file, depending on the Writer (which is encapsulated and associated to the method, examples:Approve(“”)*.txtApproveHtml(“”)*.htmlApprove(WinForm)*.pngApprove(IExecutableQuery)*.txt (SQL statement source generally)The act of approving allows you to take the contents of the received file and write them Namer – creates file name based on method nameWriter (depending on type, i.e. string) – inserts contents of object (i.e. string) into the new fileApprover – checks to see whether *.received file equals *.approvedReporter – called only on failure and then report (show) both files (*.approved & *.received)Received file is the contents that you gave the Writer written to the that file, the type is determined by the Writer, i.e. tiff, png
  • http://www.youtube.com/watch?v=52YouQkd-f8Can approve ‘parts’ of a form (controls), or reports (RDLC), different types of files, such as .tiff, etc…*to test part of a Form, must make that part (control) publicC# demo using WinDiff (or TortoiseImageDiff) – new code TDD, ReSharper or VS refactoring tools?Do a version of this demo - http://www.youtube.com/watch?v=k5Y_jo4Nn54Also for ArraysThere is a header and lambda function for arrays that allows for thetesting circle.var word = "Llewellyn";Approvals.Approve(word, word.ToCharArray(), (c) => c+ " => " + (int)c);would produce:LlewellynL => 76l => 108e => 101w => 119e => 101l => 108l => 108y => 121n => 110
  • http://blog.approvaltests.com/2012/03/testing-difficult-code.html
  • Picture credit - http://www.idownloadblog.com/2010/12/03/cydia-gets-an-update-and-becomes-much-faster/
  • Testing non-deterministic results – blog – testing the weatherOn test “pass”, there is NOT call to the data source (web service, database, etc…)ILoader implements Load(), uses IExQuerycombines, creates abstract base LoaderForSystem, example:SQL  conn string, T-SQLEF  conn string, LINQWCF  server, URL (query)
  • Photo credit - http://www.autorobinc.com/system-integration.html
  • Image credits - http://dilbert.com/strips/comic/2006-12-08/Biz opportunity (i.e. how much) Legacy code is out thereIntro the idea of using approvals for legacy code in addition to TDD (i.e. test-first on new code)Image credit - http://www.agilitrix.com/wp-content/uploads/2010/03/Legacy-Systems-ThoughtWorks-QTB.jpg
  • http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052
  • Explain the syntax of ApprovalTests.Combinations.CombinationApprovals.VerifyAllCombinations (arg,arg,arg) in C# (9 overloads)Can pass  Enumerable, Array, RangeContrast to Debug.Assert (individual results) – ApprovalTests.Combinations.CombinationApprovals.VerifyAllCombinations  much richer output, here you are testing EVERY element in combination
  • Intro the idea of using approvals for legacy code in addition to TDD (i.e. test-first on new code)Right click on testconfig file>Data And Diagnostics
  • Intro the idea of using approvals for legacy code in addition to TDD (i.e. test-first on new code)Code coverage in VS2010 Ultimate or can use DotCover (around $ 100), or othersCode Coverage is defined as running your tests and seeing how much of your code is ‘covered’
  • Be sure to read the FAQ - http://approvaltests.sourceforge.net/?q=node/3
  • Future – Approval Katas or Koans“Try our bowling with approvals. - Java / C# with MS Test / C# with Nunit”
  • www.ApprovalTests.com-->http://approvaltests.sourceforge.net/
  • About ApprovalTests for Better Unit Testing

    1. 1. An open source library for better Unit Testing Woody Zuill & Lynn Langit Agile 2012 – August 2012
    2. 2. Building your Testing ToolkitTest Runner Nunit, MSTest, JUnit, XUnitCode Asserts, otherLibraries Approval TestsManual testsAutomated tests & tools Differencing Tools
    3. 3. Approval Tests and Test Types
    4. 4. Using Unit Tests• New Code - application code (components)• Testing APIs – functionality, edge cases• Legacy Code – locking tests• Learning new APIs – scope, functionality
    5. 5. Unit Testing Output?• Granularity• Feedback• Specification• Regression
    6. 6. Why use ApprovalTests?• Simple = easy to test = use Asserts – Ex. Verify a Scalar Value• Complex = hard to test = use ApprovalTests – Ex. Verify Array Values – Ex. Verify a GUI – Ex. Verify a Database query
    7. 7. Getting started DEMO
    8. 8. How Test-Driven TDD BDD • Assert • Think Bigger • Do • Less Code • Verify • ScenariosApprovalTests fit inwith both types of testing
    9. 9. Testing ArraysDEMO
    10. 10. Testing Circle
    11. 11. About Reporters• Reporters are DiffTools• ApprovalTests includes support for many Reporters• It’s simple to add hooks to your favorite diff tool
    12. 12. How to Approve Contents provided are serialized to the file as binary (for comparison)• Rename *.received to *.approved• Right click ‘Approve’ (VS plug-in)• Right click ‘Use whole file’ (DiffReporter)• Copy ‘Move’ statement from Test Results to command window
    13. 13. • HTML• Browser/text diff• ASP.NET• ASP.NET verifyApproving GUIsDEMO
    14. 14. Code must be Testable• Easy to write tests for it• Runnable tests must exist – Write one test that passes – Then write another test on another method – Return to the first test when needed
    15. 15. Peel and Slice Videos• 3 videos in .NET (Mocks) series ‘peel, slice and peel and slice’• Getting ready to use (data) Loaders
    16. 16. Testing Data Loading• Mocks (fakes) data population• Tests the USE of data• Does not require a live connection to the database• Thin layer (ILoader) which decouples data load• Unit Tests test without dependencies
    17. 17. Data Usage PatternsCommon Preferred Load Manipulate Save
    18. 18. Testing calls to a DatabaseDEMO
    19. 19. Integration Testing• Allows testing of functionality across multiple objects (behavior or scenarios)• Example testing ASP.NET MVC – video here
    20. 20. Legacy CodeHow much of your code base is dead code?
    21. 21. Locking Tests (Characterization Tests)• Test to enable refactoring• Ensures system still works the same• Most often used with Legacy Code• Quickly results in high test coverage
    22. 22. Legacy Code – Chart SmartDEMO
    23. 23. Visual Studio Code Coverage Config
    24. 24. Code CoverageHow muchof your code baseis covered?
    25. 25. Who wrote ApprovalTests?Llewellyn DanFalco Gilkerson
    26. 26. For More Information• www.ApprovalTests.com• Pick your language & download• Pick your diff tool• Reference, Approve & Enjoy• Learn more – videos (.NET)• Podcast - HerdingCode
    27. 27. @WoodyZuill @LynnLangit Q&A APPROVAL TESTS