Your SlideShare is downloading. ×
About ApprovalTests for Better Unit Testing
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

About ApprovalTests for Better Unit Testing


Published on

Deck for talk at SoCalCodeCamp on ApprovalTests for Unit Testing

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide
  • 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 - credit -
  • Image credit -
  • “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 - 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
  • 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 ( – text, image, HTML, more?List of free differencing tools -
  • 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
  • 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 - 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
  • Picture credit -
  • 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 -
  • Image credits - 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 -
  • 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 -
  • Future – Approval Katas or Koans“Try our bowling with approvals. - Java / C# with MS Test / C# with Nunit”
  • Transcript

    • 1. An open source library for better Unit Testing Woody Zuill & Lynn Langit Agile 2012 – August 2012
    • 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. Approval Tests and Test Types
    • 4. Using Unit Tests• New Code - application code (components)• Testing APIs – functionality, edge cases• Legacy Code – locking tests• Learning new APIs – scope, functionality
    • 5. Unit Testing Output?• Granularity• Feedback• Specification• Regression
    • 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. Getting started DEMO
    • 8. How Test-Driven TDD BDD • Assert • Think Bigger • Do • Less Code • Verify • ScenariosApprovalTests fit inwith both types of testing
    • 9. Testing ArraysDEMO
    • 10. Testing Circle
    • 11. About Reporters• Reporters are DiffTools• ApprovalTests includes support for many Reporters• It’s simple to add hooks to your favorite diff tool
    • 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. • HTML• Browser/text diff• ASP.NET• ASP.NET verifyApproving GUIsDEMO
    • 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. Peel and Slice Videos• 3 videos in .NET (Mocks) series ‘peel, slice and peel and slice’• Getting ready to use (data) Loaders
    • 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. Data Usage PatternsCommon Preferred Load Manipulate Save
    • 18. Testing calls to a DatabaseDEMO
    • 19. Integration Testing• Allows testing of functionality across multiple objects (behavior or scenarios)• Example testing ASP.NET MVC – video here
    • 20. Legacy CodeHow much of your code base is dead code?
    • 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. Legacy Code – Chart SmartDEMO
    • 23. Visual Studio Code Coverage Config
    • 24. Code CoverageHow muchof your code baseis covered?
    • 25. Who wrote ApprovalTests?Llewellyn DanFalco Gilkerson
    • 26. For More Information•• Pick your language & download• Pick your diff tool• Reference, Approve & Enjoy• Learn more – videos (.NET)• Podcast - HerdingCode
    • 27. @WoodyZuill @LynnLangit Q&A APPROVAL TESTS