Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

How to use Approval Tests for C++ Effectively

79 views

Published on

Presented at CppEurope Online on 23 June 2020.

This talk, which was mostly live demos, covered aspects such as:

- Formatting options, to ensure failures are really easy to understand and act on
- An example of when a growing number of unit tests turns out easier to maintain with Approvals
- Different reports, and how to use them well
- Dealing with timestamps or dates (for example in logs)
- Dealing with external resources: I/O, networking, system API calls, etc.
- Dealing with multiple processes or threads
- Can I use approvals for the cases when RAM is low? Eg. an embedded controller that has 1MB of RAM

Links from the talk, including to the demo code, are available at:
https://github.com/claremacrae/talks/blob/main/How_to_use_Approval_Tests_for_C++_Effectively.md#top

Published in: Software
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

How to use Approval Tests for C++ Effectively

  1. 1. 1 How to use Approval Tests for C++ Effectively Clare Macrae (She/her) clare@claremacrae.co.uk 23 June 2020 CppEurope
  2. 2. 2 About Me • C++ and Qt developer since 1999 • My mission: Sustainable and efficient testing and refactoring of legacy code – Co-author of “Approval Tests for C++” • Consulting & training – https://claremacrae.co.uk • All links from this talk via: – github.com/claremacrae/talks
  3. 3. 3
  4. 4. 4
  5. 5. 5 “Approval Tests allow you to verify a chunk of output (such as a file) in one operation as opposed to writing test assertions for each element”
  6. 6. 6 Demo 1: Hello Approvals
  7. 7. 7 Main Methods • Approvals::verify(std::string, Options) – 2 other overloads take template types • Approvals::verifyAll() – For use with containers – Writes one element at a time • All are in namespace ApprovalTests
  8. 8. 8 object verify() writes "received" reads "approved" Same? Pass Fail Reporter Stages of Approvals::verify()
  9. 9. 9 Easy to: • … Add tests quickly • … Visualise differences • … Update output • Convenience – Separates test data from test code – Ignore end-of-line differences
  10. 10. 10 Easy setup • Single header – https://github.com/approvals/ApprovalTests.cpp/releases • Finds diff tool automatically • Sensible filenames automatically – source_directory/test_filename.test_name.approved.txt • “It just works”
  11. 11. 11 Separation • Separates test data from test code
  12. 12. 12 Supported Test Frameworks • Lots to choose from: • Get to know chosen one well!
  13. 13. 13 Demo 2: Legacy Code: Gilded Rose
  14. 14. 14 Approvals and Legacy Code • Before you start refactoring… • Achieve good coverage • verifyAllCombinations() – Not just for legacy code
  15. 15. 15 Lots of String Conversion Options • String Conversions – Pass in a std::string – Use Approvals::verify( object, lambda ) – Write custom operator<<( std::ostream, … ) – Specialize ApprovalTests::StringMaker::toString( … ) – Use TApprovals<YourStringConvertingClass> • How to Use the Fmt Library To Print Objects – Use {fmt} library via FmtApprovals::verify()
  16. 16. 16 String Design Guidelines • Objects print their relevant data • The data is consistent between runs – (no times, pointers, random) • The data is easy to read, at a glance: • Advice: Tips for Designing Strings [0] = [x: 4 y: 50 width: 100 height: 61] [1] = [x: 50 y: 5200 width: 400 height: 62] [2] = [x: 60 y: 3 width: 7 height: 63] (x,y,width,height) = (4,50,100,61) (x,y,width,height) = (50,5200,400,62) (x,y,width,height) = (60,3,7,63)
  17. 17. 17 Demo 3: Log Files
  18. 18. 18 Key Points • Approvals::verifyExistingFile() – For when the file is already written • Scrubbers for non-deterministic output – More scrubber options coming soon…
  19. 19. 19 Customisation Points Overview
  20. 20. 20 Scrubber Namer Writer Comparator Pass Fail Reporter Stages of Approvals::verify()
  21. 21. 21 Everything is customisable • We take great pride in the docs • All examples generated from compiled, tested code github.com/approvals/ApprovalTests.cpp/blob/master/doc/ approvaltestscpp.readthedocs.io/en/latest/
  22. 22. 22 Demo 4: SVG Files
  23. 23. 23 Key Points • Custom Reporters can help understand failures • Make it easy to visualise differences • No need to version control .png files
  24. 24. 24 Common Challenges
  25. 25. 25 External Resources • I/O, Networking, System API calls… • Be creative! • Can you intercept and log calls? – And save them as “approved” results? – Then intercept again during later runs, and compare results? • You can even use this data as inputs to later tests! • Excellent example from Angie Jones: – https://angiejones.tech/verifying-entire-api-responses/
  26. 26. 26 Embedded Systems • Separate as much logic as possible • Test the logic on desktop machines and CI • Then if you get a failure on device, you’ve got less to test • It may become possible to run Approval Tests from cross-built code
  27. 27. 27 Output is OS-specific • Include the OS in the file-names • So verify() compares against output from same platform • For example – TestQtDialog.loginScreen.onMacOSX.approved.png – TestQtDialog.loginScreen.onWindows.approved.png – TestQtDialog.loginScreen.onLinux.approved.png • See Multiple output files per test
  28. 28. 28 Approving is fiddly or takes too long • Approving images? • Approving hundreds of files? • Try one of: – AutoApproveIfMissingReporter – AutoApproveReporter (and compare the diffs in version control!)
  29. 29. 29 “Approval Tests allow you to verify a chunk of output (such as a file) in one operation as opposed to writing test assertions for each element”
  30. 30. 30 How to use Approval Tests for C++ Effectively • All links from this talk, and more, via: – github.com/claremacrae/talks • Sustainable and efficient testing and refactoring of legacy code • Consulting & Training – https://claremacrae.co.uk – clare@claremacrae.co.uk • Any Questions?

×