SlideShare a Scribd company logo
1
How to use Approval Tests for C++ Effectively
Clare Macrae (She/her)
clare@claremacrae.co.uk
23 June 2020
CppEurope
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
4
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
Demo 1:
Hello Approvals
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
object verify()
writes
"received"
reads
"approved"
Same?
Pass
Fail Reporter
Stages of Approvals::verify()
9
Easy to:
• … Add tests quickly
• … Visualise differences
• … Update output
• Convenience
– Separates test data from test code
– Ignore end-of-line differences
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
Separation
• Separates test data from test code
12
Supported Test Frameworks
• Lots to choose from:
• Get to know chosen one well!
13
Demo 2:
Legacy Code: Gilded Rose
14
Approvals and Legacy Code
• Before you start refactoring…
• Achieve good coverage
• verifyAllCombinations()
– Not just for legacy code
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
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
Demo 3:
Log Files
18
Key Points
• Approvals::verifyExistingFile()
– For when the file is already written
• Scrubbers for non-deterministic output
– More scrubber options coming soon…
19
Customisation Points Overview
20
Scrubber Namer Writer Comparator
Pass
Fail Reporter
Stages of Approvals::verify()
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
Demo 4:
SVG Files
23
Key Points
• Custom Reporters can help understand failures
• Make it easy to visualise differences
• No need to version control .png files
24
Common Challenges
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
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
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
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
“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
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?

More Related Content

PPTX
Quickly and Effectively Testing Legacy C++ Code with Approval Tests
PPT
Mxunit
PDF
Testing with Express, Mocha & Chai
PPTX
Unit tests = maintenance hell ?
PPT
Unit testing with Spock Framework
PPTX
Unit testing presentation
PPT
Mocha Testing
PPTX
Automation patterns on practice
Quickly and Effectively Testing Legacy C++ Code with Approval Tests
Mxunit
Testing with Express, Mocha & Chai
Unit tests = maintenance hell ?
Unit testing with Spock Framework
Unit testing presentation
Mocha Testing
Automation patterns on practice

What's hot (20)

PPT
JavaScript Testing: Mocha + Chai
PPTX
Memcached-инъекции - они существуют и работают, Иван Новиков (ONsec)
PDF
Ruxmon cve 2012-2661
PDF
Finding Needles in Haystacks
PDF
Ln monitoring repositories
PPTX
Smarter Testing With Spock
PDF
Spock: Test Well and Prosper
PDF
Ruxmon feb 2013 what happened to rails
PPTX
PDF
Spock Framework
PDF
Head First Java Chapter 1
PPTX
Unit testing patterns for concurrent code
PDF
Spock framework
PPTX
Professional Help for PowerShell Modules
PPTX
Test your tests with PIT framework
PPTX
Unit testing on mobile apps
PPT
Powershell Seminar @ ITWorx CuttingEdge Club
PPTX
J unit스터디슬라이드
ODP
Good Practices On Test Automation
PDF
C++ Windows Forms L01 - Intro
JavaScript Testing: Mocha + Chai
Memcached-инъекции - они существуют и работают, Иван Новиков (ONsec)
Ruxmon cve 2012-2661
Finding Needles in Haystacks
Ln monitoring repositories
Smarter Testing With Spock
Spock: Test Well and Prosper
Ruxmon feb 2013 what happened to rails
Spock Framework
Head First Java Chapter 1
Unit testing patterns for concurrent code
Spock framework
Professional Help for PowerShell Modules
Test your tests with PIT framework
Unit testing on mobile apps
Powershell Seminar @ ITWorx CuttingEdge Club
J unit스터디슬라이드
Good Practices On Test Automation
C++ Windows Forms L01 - Intro
Ad

Similar to How to use Approval Tests for C++ Effectively (20)

PPTX
Quickly and Effectively Testing Legacy c++ Code with Approval Tests mu cpp
PPTX
Quickly Testing Legacy Cpp Code - ACCU Cambridge 2019
PPTX
Quickly Testing Legacy C++ Code with Approval Tests
PPTX
C++ Testing Techniques Tips and Tricks - C++ London
PPTX
Quickly testing legacy code
PPTX
Quickly Testing Legacy Code - ACCU York April 2019
PPTX
Quickly testing legacy code cppp.fr 2019 - clare macrae
PPTX
Cpp Testing Techniques Tips and Tricks - Cpp Europe
PDF
How to make a large C++-code base manageable
PPTX
Effective C++
PPTX
Testing Superpowers: Using CLion to Add Tests Easily
PPTX
Quality assurance of large c++ projects
PDF
Hardware Verification With C A Practitioners Handbook 1st Edition Mike Mintz ...
PDF
Mastering Modern C++: C++11, C++14, C++17, C++20, C++23
PDF
C++ annotations version
PDF
Trip Report from Meeting C++ 2017: It's Way More Than C++
PDF
Static Code Analysis and Cppcheck
PDF
Francois fleuret -_c++_lecture_notes
PPTX
How to Adopt Modern C++17 into Your C++ Code
PPTX
How to Adopt Modern C++17 into Your C++ Code
Quickly and Effectively Testing Legacy c++ Code with Approval Tests mu cpp
Quickly Testing Legacy Cpp Code - ACCU Cambridge 2019
Quickly Testing Legacy C++ Code with Approval Tests
C++ Testing Techniques Tips and Tricks - C++ London
Quickly testing legacy code
Quickly Testing Legacy Code - ACCU York April 2019
Quickly testing legacy code cppp.fr 2019 - clare macrae
Cpp Testing Techniques Tips and Tricks - Cpp Europe
How to make a large C++-code base manageable
Effective C++
Testing Superpowers: Using CLion to Add Tests Easily
Quality assurance of large c++ projects
Hardware Verification With C A Practitioners Handbook 1st Edition Mike Mintz ...
Mastering Modern C++: C++11, C++14, C++17, C++20, C++23
C++ annotations version
Trip Report from Meeting C++ 2017: It's Way More Than C++
Static Code Analysis and Cppcheck
Francois fleuret -_c++_lecture_notes
How to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ Code
Ad

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Digital Strategies for Manufacturing Companies
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Transform Your Business with a Software ERP System
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
top salesforce developer skills in 2025.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Essential Infomation Tech presentation.pptx
PDF
System and Network Administration Chapter 2
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Digital Strategies for Manufacturing Companies
Wondershare Filmora 15 Crack With Activation Key [2025
Navsoft: AI-Powered Business Solutions & Custom Software Development
Reimagine Home Health with the Power of Agentic AI​
Transform Your Business with a Software ERP System
Internet Downloader Manager (IDM) Crack 6.42 Build 41
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
top salesforce developer skills in 2025.pdf
Softaken Excel to vCard Converter Software.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
How to Choose the Right IT Partner for Your Business in Malaysia
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Design an Analysis of Algorithms II-SECS-1021-03
Odoo POS Development Services by CandidRoot Solutions
Essential Infomation Tech presentation.pptx
System and Network Administration Chapter 2

How to use Approval Tests for C++ Effectively

  • 1. 1 How to use Approval Tests for C++ Effectively Clare Macrae (She/her) clare@claremacrae.co.uk 23 June 2020 CppEurope
  • 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
  • 4. 4
  • 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”
  • 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
  • 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 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 Separation • Separates test data from test code
  • 12. 12 Supported Test Frameworks • Lots to choose from: • Get to know chosen one well!
  • 13. 13 Demo 2: Legacy Code: Gilded Rose
  • 14. 14 Approvals and Legacy Code • Before you start refactoring… • Achieve good coverage • verifyAllCombinations() – Not just for legacy code
  • 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 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)
  • 18. 18 Key Points • Approvals::verifyExistingFile() – For when the file is already written • Scrubbers for non-deterministic output – More scrubber options coming soon…
  • 20. 20 Scrubber Namer Writer Comparator Pass Fail Reporter Stages of Approvals::verify()
  • 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/
  • 23. 23 Key Points • Custom Reporters can help understand failures • Make it easy to visualise differences • No need to version control .png files
  • 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 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 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 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 “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 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?

Editor's Notes

  • #31: A lot of this stuff you can get going on your own. If you get stuck, I can help you!