SlideShare ist ein Scribd-Unternehmen logo
1 von 58
1
Quickly and Effectively
Testing Legacy C++ Code with Approval Tests
Clare Macrae (She/her)
clare@claremacrae.co.uk
6 August 2020
MUC++
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
Goal
Share the Power of
Approval Tests
4
Contents
•Intro
• Getting Started
• Legacy Example
• Non-deterministic Output
• Customisability
• Visualising Differences
• Summary
5
6
Quickly and Effectively Testing
Legacy Code
7
What does Legacy Code really mean?
• Dictionary
–“money or property that you receive from someone after they die ”
• Michael Feathers
–“code without unit tests”
• J.B. Rainsberger
–“profitable code that we feel afraid to change.”
• Kate Gregory
–“any code that you want to change, but are afraid to”
8
Typical Scenario
• I've inherited some
legacy code
• It's valuable
• I need to add feature
• Or fix bug
• How can I ever break
out of this loop?
Need to
change
the code
No tests
Not
designed
for testing
Needs
refactoring
to add
tests
Can’t
refactor
without
tests
9
Typical Scenario
•
•
•
•
•
Need to
change
the code
No tests
Not
designed
for testing
Needs
refactoring
to add
tests
Can’t
refactor
without
tests
Topics of
this talk
10
Assumptions
• Value of automated testing
• Evaluate existing tests
– Code coverage tools
– Mutation testing
• No worries about types of tests
– (unit, integration, regression)
11
12
13
“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”
14
Questions?
15
Contents
• Intro
•Getting Started
• Legacy Example
• Non-deterministic Output
• Customisability
• Visualising Differences
• Summary
16
Demo 1:
Hello Approvals
17
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
18
object verify()
writes
"received"
reads
"approved"
Same?
Pass
Fail Reporter
Stages of Approvals::verify()
19
Easy to:
• … Add tests quickly
• … Visualise differences
• … Update output
• Convenience
– Separates test data from test code
– Ignore end-of-line differences
20
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”
21
Supported Test Frameworks
• Lots to choose from:
• Get to know chosen one well!
22
Questions?
23
Contents
• Intro
• Getting Started
•Legacy Example
• Non-deterministic Output
• Customisability
• Visualising Differences
• Summary
24
Demo 2:
Legacy Code: Gilded Rose
25
Approvals and Legacy Code
• Before you start refactoring…
• Achieve good coverage
• verifyAllCombinations()
– Not just for legacy code
26
CombinationApprovals::verifyAllCombinations()
• One line from GildedRose .approved.txt file:
(Wibble, 0, 0) => name: Wibble, sellIn: -1, quality: 0
verifyAllCombinations() writes
one set of input values
and then the result of the lambda for
that input set: here, an Item object
27
Lots of String Conversion Options
• String Conversions
– Pass in a std::string
– Write custom operator<<( std::ostream, … )
– Use Approvals::verify( object, lambda )
– Specialize ApprovalTests::StringMaker::toString( … )
– Use TApprovals<YourStringConvertingClass>
• How to Use the Fmt Library To Print Objects
– Use {fmt} library via FmtApprovals::verify()
28
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)
29
Questions?
30
Contents
• Intro
• Getting Started
• Legacy Example
•Non-deterministic Output
• Customisability
• Visualising Differences
• Summary
31
Demo 3:
Log Files
32
Key Points
• Approvals::verifyExistingFile()
– For when the file is already written
• Scrubbers for non-deterministic output
– More scrubber options coming soon…
33
Questions?
34
Contents
• Intro
• Getting Started
• Legacy Example
• Non-deterministic Output
•Customisability
• Visualising Differences
• Summary
35
Customisation Points Overview
36
Scrubber Namer Writer Comparator
Pass
Fail Reporter
Stages of Approvals::verify()
37
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/
38
Questions?
39
Contents
• Intro
• Getting Started
• Legacy Example
• Non-deterministic Output
• Customisability
•Visualising Differences
• Summary
40
Demo 4:
SVG Files
41
Key Points
• Custom Reporters can help understand failures
• Make it easy to visualise differences
• No need to version control .png files
42
See Also ImageApprovals
• github.com/p-podsiadly/ImageApprovals
• Built on ApprovalTests.cpp
• Not yet released
43
Questions?
44
Contents
• Intro
• Getting Started
• Legacy Example
• Non-deterministic Output
• Customisability
• Visualising Differences
•Summary
45
Summary
46
“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”
47
Approval Tests for C++
• Easy to use
• Convenient
• Powerful
• Flexible
• It just works!
48
49
50
51
Quickly & Effectively
Test Legacy C++ Code
with Approval Tests
52
References
• All links from this talk, and more, via:
– github.com/claremacrae/talks
• Sustainable and efficient testing and refactoring of legacy code
• Consulting & Training
– claremacrae.co.uk
– clare@claremacrae.co.uk
Any Questions?
53
Contents
• Intro
• Getting Started
• Legacy Example
• Non-deterministic Output
• Customisability
• Visualising Differences
• Summary
•Appendix: Challenges
54
Appendix: Common Challenges
55
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/
56
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
57
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
58
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!)

Weitere ähnliche Inhalte

Was ist angesagt?

The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional ProgrammerDave Cross
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In ActionJon Kruger
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And RefactoringNaresh Jain
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy CodeExcella
 
How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.Matt Eland
 
RESTful Microservices
RESTful MicroservicesRESTful Microservices
RESTful MicroservicesShaun Abram
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven DevelopmentTung Nguyen Thanh
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy CodeAndrea Polci
 
Finding a good development partner
Finding a good development partnerFinding a good development partner
Finding a good development partnerKevin Poorman
 
Test driven-development
Test driven-developmentTest driven-development
Test driven-developmentDavid Paluy
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)Rob Hale
 
Roy Osherove TDD From Scratch
Roy Osherove TDD From ScratchRoy Osherove TDD From Scratch
Roy Osherove TDD From ScratchRoy Osherove
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy codeLars Thorup
 
Keyword Driven Testing
Keyword Driven TestingKeyword Driven Testing
Keyword Driven TestingMaveryx
 
Common Challenges & Best Practices for TDD on iOS
Common Challenges & Best Practices for TDD on iOSCommon Challenges & Best Practices for TDD on iOS
Common Challenges & Best Practices for TDD on iOSDerek Lee Boire
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit TestingShaun Abram
 

Was ist angesagt? (20)

The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional Programmer
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
PHPUnit - Unit testing
PHPUnit - Unit testingPHPUnit - Unit testing
PHPUnit - Unit testing
 
Unit Testing in Swift
Unit Testing in SwiftUnit Testing in Swift
Unit Testing in Swift
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.
 
RESTful Microservices
RESTful MicroservicesRESTful Microservices
RESTful Microservices
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven Development
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
 
Finding a good development partner
Finding a good development partnerFinding a good development partner
Finding a good development partner
 
Test driven-development
Test driven-developmentTest driven-development
Test driven-development
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 
Roy Osherove TDD From Scratch
Roy Osherove TDD From ScratchRoy Osherove TDD From Scratch
Roy Osherove TDD From Scratch
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
 
Keyword Driven Testing
Keyword Driven TestingKeyword Driven Testing
Keyword Driven Testing
 
Presentation
PresentationPresentation
Presentation
 
Common Challenges & Best Practices for TDD on iOS
Common Challenges & Best Practices for TDD on iOSCommon Challenges & Best Practices for TDD on iOS
Common Challenges & Best Practices for TDD on iOS
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit Testing
 
Test box bdd
Test box bddTest box bdd
Test box bdd
 

Ähnlich wie Quickly and Effectively Testing Legacy c++ Code with Approval Tests mu cpp

Quickly and Effectively Testing Legacy C++ Code with Approval Tests
Quickly and Effectively Testing Legacy C++ Code with Approval TestsQuickly and Effectively Testing Legacy C++ Code with Approval Tests
Quickly and Effectively Testing Legacy C++ Code with Approval TestsClare Macrae
 
C++ Testing Techniques Tips and Tricks - C++ London
C++ Testing Techniques Tips and Tricks - C++ LondonC++ Testing Techniques Tips and Tricks - C++ London
C++ Testing Techniques Tips and Tricks - C++ LondonClare Macrae
 
How to use Approval Tests for C++ Effectively
How to use Approval Tests for C++ EffectivelyHow to use Approval Tests for C++ Effectively
How to use Approval Tests for C++ EffectivelyClare Macrae
 
Quickly Testing Legacy Cpp Code - ACCU Cambridge 2019
Quickly Testing Legacy Cpp Code - ACCU Cambridge 2019Quickly Testing Legacy Cpp Code - ACCU Cambridge 2019
Quickly Testing Legacy Cpp Code - ACCU Cambridge 2019Clare Macrae
 
CBDW2014 - Behavior Driven Development with TestBox
CBDW2014 - Behavior Driven Development with TestBoxCBDW2014 - Behavior Driven Development with TestBox
CBDW2014 - Behavior Driven Development with TestBoxOrtus Solutions, Corp
 
TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)Peter Kofler
 
Practical unit testing in c & c++
Practical unit testing in c & c++Practical unit testing in c & c++
Practical unit testing in c & c++Matt Hargett
 
Developers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonDevelopers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonIneke Scheffers
 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...Uma Ghotikar
 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...Ortus Solutions, Corp
 
Lean-Agile Development with SharePoint - Bill Ayers
Lean-Agile Development with SharePoint - Bill AyersLean-Agile Development with SharePoint - Bill Ayers
Lean-Agile Development with SharePoint - Bill AyersSPC Adriatics
 
Полезные метрики покрытия. Практический опыт и немного теории
Полезные метрики покрытия. Практический опыт и немного теорииПолезные метрики покрытия. Практический опыт и немного теории
Полезные метрики покрытия. Практический опыт и немного теорииSQALab
 
Unit Testing and Tools
Unit Testing and ToolsUnit Testing and Tools
Unit Testing and ToolsWilliam Simms
 
Test parallelization using Jenkins
Test parallelization using JenkinsTest parallelization using Jenkins
Test parallelization using JenkinsRogue Wave Software
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)Thierry Gayet
 
Quickly Testing Legacy C++ Code with Approval Tests
Quickly Testing Legacy C++ Code with Approval TestsQuickly Testing Legacy C++ Code with Approval Tests
Quickly Testing Legacy C++ Code with Approval TestsClare Macrae
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentMeilan Ou
 
How to become a testing expert
How to become a testing expertHow to become a testing expert
How to become a testing expertgaoliang641
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best PracticesTomaš Maconko
 

Ähnlich wie Quickly and Effectively Testing Legacy c++ Code with Approval Tests mu cpp (20)

Quickly and Effectively Testing Legacy C++ Code with Approval Tests
Quickly and Effectively Testing Legacy C++ Code with Approval TestsQuickly and Effectively Testing Legacy C++ Code with Approval Tests
Quickly and Effectively Testing Legacy C++ Code with Approval Tests
 
C++ Testing Techniques Tips and Tricks - C++ London
C++ Testing Techniques Tips and Tricks - C++ LondonC++ Testing Techniques Tips and Tricks - C++ London
C++ Testing Techniques Tips and Tricks - C++ London
 
How to use Approval Tests for C++ Effectively
How to use Approval Tests for C++ EffectivelyHow to use Approval Tests for C++ Effectively
How to use Approval Tests for C++ Effectively
 
Quickly Testing Legacy Cpp Code - ACCU Cambridge 2019
Quickly Testing Legacy Cpp Code - ACCU Cambridge 2019Quickly Testing Legacy Cpp Code - ACCU Cambridge 2019
Quickly Testing Legacy Cpp Code - ACCU Cambridge 2019
 
CBDW2014 - Behavior Driven Development with TestBox
CBDW2014 - Behavior Driven Development with TestBoxCBDW2014 - Behavior Driven Development with TestBox
CBDW2014 - Behavior Driven Development with TestBox
 
TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)
 
Practical unit testing in c & c++
Practical unit testing in c & c++Practical unit testing in c & c++
Practical unit testing in c & c++
 
Building XWiki
Building XWikiBuilding XWiki
Building XWiki
 
Developers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonDevelopers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomon
 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
 
Lean-Agile Development with SharePoint - Bill Ayers
Lean-Agile Development with SharePoint - Bill AyersLean-Agile Development with SharePoint - Bill Ayers
Lean-Agile Development with SharePoint - Bill Ayers
 
Полезные метрики покрытия. Практический опыт и немного теории
Полезные метрики покрытия. Практический опыт и немного теорииПолезные метрики покрытия. Практический опыт и немного теории
Полезные метрики покрытия. Практический опыт и немного теории
 
Unit Testing and Tools
Unit Testing and ToolsUnit Testing and Tools
Unit Testing and Tools
 
Test parallelization using Jenkins
Test parallelization using JenkinsTest parallelization using Jenkins
Test parallelization using Jenkins
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)
 
Quickly Testing Legacy C++ Code with Approval Tests
Quickly Testing Legacy C++ Code with Approval TestsQuickly Testing Legacy C++ Code with Approval Tests
Quickly Testing Legacy C++ Code with Approval Tests
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
How to become a testing expert
How to become a testing expertHow to become a testing expert
How to become a testing expert
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
 

Mehr von Clare Macrae

Cpp Testing Techniques Tips and Tricks - Cpp Europe
Cpp Testing Techniques Tips and Tricks - Cpp EuropeCpp Testing Techniques Tips and Tricks - Cpp Europe
Cpp Testing Techniques Tips and Tricks - Cpp EuropeClare Macrae
 
Quickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsQuickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsClare Macrae
 
Code samples that actually compile - Clare Macrae
Code samples that actually compile - Clare MacraeCode samples that actually compile - Clare Macrae
Code samples that actually compile - Clare MacraeClare Macrae
 
Quickly testing legacy code cppp.fr 2019 - clare macrae
Quickly testing legacy code   cppp.fr 2019 - clare macraeQuickly testing legacy code   cppp.fr 2019 - clare macrae
Quickly testing legacy code cppp.fr 2019 - clare macraeClare Macrae
 
Quickly Testing Legacy Code - ACCU York April 2019
Quickly Testing Legacy Code - ACCU York April 2019Quickly Testing Legacy Code - ACCU York April 2019
Quickly Testing Legacy Code - ACCU York April 2019Clare Macrae
 
Quickly testing legacy code
Quickly testing legacy codeQuickly testing legacy code
Quickly testing legacy codeClare Macrae
 
Escaping 5 decades of monolithic annual releases
Escaping 5 decades of monolithic annual releasesEscaping 5 decades of monolithic annual releases
Escaping 5 decades of monolithic annual releasesClare Macrae
 
CCDC’s (ongoing) Journey to Continuous Delivery - London Continuous Delivery ...
CCDC’s (ongoing) Journey to Continuous Delivery - London Continuous Delivery ...CCDC’s (ongoing) Journey to Continuous Delivery - London Continuous Delivery ...
CCDC’s (ongoing) Journey to Continuous Delivery - London Continuous Delivery ...Clare Macrae
 

Mehr von Clare Macrae (8)

Cpp Testing Techniques Tips and Tricks - Cpp Europe
Cpp Testing Techniques Tips and Tricks - Cpp EuropeCpp Testing Techniques Tips and Tricks - Cpp Europe
Cpp Testing Techniques Tips and Tricks - Cpp Europe
 
Quickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsQuickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop Applications
 
Code samples that actually compile - Clare Macrae
Code samples that actually compile - Clare MacraeCode samples that actually compile - Clare Macrae
Code samples that actually compile - Clare Macrae
 
Quickly testing legacy code cppp.fr 2019 - clare macrae
Quickly testing legacy code   cppp.fr 2019 - clare macraeQuickly testing legacy code   cppp.fr 2019 - clare macrae
Quickly testing legacy code cppp.fr 2019 - clare macrae
 
Quickly Testing Legacy Code - ACCU York April 2019
Quickly Testing Legacy Code - ACCU York April 2019Quickly Testing Legacy Code - ACCU York April 2019
Quickly Testing Legacy Code - ACCU York April 2019
 
Quickly testing legacy code
Quickly testing legacy codeQuickly testing legacy code
Quickly testing legacy code
 
Escaping 5 decades of monolithic annual releases
Escaping 5 decades of monolithic annual releasesEscaping 5 decades of monolithic annual releases
Escaping 5 decades of monolithic annual releases
 
CCDC’s (ongoing) Journey to Continuous Delivery - London Continuous Delivery ...
CCDC’s (ongoing) Journey to Continuous Delivery - London Continuous Delivery ...CCDC’s (ongoing) Journey to Continuous Delivery - London Continuous Delivery ...
CCDC’s (ongoing) Journey to Continuous Delivery - London Continuous Delivery ...
 

Kürzlich hochgeladen

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 

Kürzlich hochgeladen (20)

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 

Quickly and Effectively Testing Legacy c++ Code with Approval Tests mu cpp

  • 1. 1 Quickly and Effectively Testing Legacy C++ Code with Approval Tests Clare Macrae (She/her) clare@claremacrae.co.uk 6 August 2020 MUC++
  • 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 Goal Share the Power of Approval Tests
  • 4. 4 Contents •Intro • Getting Started • Legacy Example • Non-deterministic Output • Customisability • Visualising Differences • Summary
  • 5. 5
  • 6. 6 Quickly and Effectively Testing Legacy Code
  • 7. 7 What does Legacy Code really mean? • Dictionary –“money or property that you receive from someone after they die ” • Michael Feathers –“code without unit tests” • J.B. Rainsberger –“profitable code that we feel afraid to change.” • Kate Gregory –“any code that you want to change, but are afraid to”
  • 8. 8 Typical Scenario • I've inherited some legacy code • It's valuable • I need to add feature • Or fix bug • How can I ever break out of this loop? Need to change the code No tests Not designed for testing Needs refactoring to add tests Can’t refactor without tests
  • 9. 9 Typical Scenario • • • • • Need to change the code No tests Not designed for testing Needs refactoring to add tests Can’t refactor without tests Topics of this talk
  • 10. 10 Assumptions • Value of automated testing • Evaluate existing tests – Code coverage tools – Mutation testing • No worries about types of tests – (unit, integration, regression)
  • 11. 11
  • 12. 12
  • 13. 13 “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”
  • 15. 15 Contents • Intro •Getting Started • Legacy Example • Non-deterministic Output • Customisability • Visualising Differences • Summary
  • 17. 17 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
  • 19. 19 Easy to: • … Add tests quickly • … Visualise differences • … Update output • Convenience – Separates test data from test code – Ignore end-of-line differences
  • 20. 20 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”
  • 21. 21 Supported Test Frameworks • Lots to choose from: • Get to know chosen one well!
  • 23. 23 Contents • Intro • Getting Started •Legacy Example • Non-deterministic Output • Customisability • Visualising Differences • Summary
  • 24. 24 Demo 2: Legacy Code: Gilded Rose
  • 25. 25 Approvals and Legacy Code • Before you start refactoring… • Achieve good coverage • verifyAllCombinations() – Not just for legacy code
  • 26. 26 CombinationApprovals::verifyAllCombinations() • One line from GildedRose .approved.txt file: (Wibble, 0, 0) => name: Wibble, sellIn: -1, quality: 0 verifyAllCombinations() writes one set of input values and then the result of the lambda for that input set: here, an Item object
  • 27. 27 Lots of String Conversion Options • String Conversions – Pass in a std::string – Write custom operator<<( std::ostream, … ) – Use Approvals::verify( object, lambda ) – Specialize ApprovalTests::StringMaker::toString( … ) – Use TApprovals<YourStringConvertingClass> • How to Use the Fmt Library To Print Objects – Use {fmt} library via FmtApprovals::verify()
  • 28. 28 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)
  • 30. 30 Contents • Intro • Getting Started • Legacy Example •Non-deterministic Output • Customisability • Visualising Differences • Summary
  • 32. 32 Key Points • Approvals::verifyExistingFile() – For when the file is already written • Scrubbers for non-deterministic output – More scrubber options coming soon…
  • 34. 34 Contents • Intro • Getting Started • Legacy Example • Non-deterministic Output •Customisability • Visualising Differences • Summary
  • 36. 36 Scrubber Namer Writer Comparator Pass Fail Reporter Stages of Approvals::verify()
  • 37. 37 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/
  • 39. 39 Contents • Intro • Getting Started • Legacy Example • Non-deterministic Output • Customisability •Visualising Differences • Summary
  • 41. 41 Key Points • Custom Reporters can help understand failures • Make it easy to visualise differences • No need to version control .png files
  • 42. 42 See Also ImageApprovals • github.com/p-podsiadly/ImageApprovals • Built on ApprovalTests.cpp • Not yet released
  • 44. 44 Contents • Intro • Getting Started • Legacy Example • Non-deterministic Output • Customisability • Visualising Differences •Summary
  • 46. 46 “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”
  • 47. 47 Approval Tests for C++ • Easy to use • Convenient • Powerful • Flexible • It just works!
  • 48. 48
  • 49. 49
  • 50. 50
  • 51. 51 Quickly & Effectively Test Legacy C++ Code with Approval Tests
  • 52. 52 References • All links from this talk, and more, via: – github.com/claremacrae/talks • Sustainable and efficient testing and refactoring of legacy code • Consulting & Training – claremacrae.co.uk – clare@claremacrae.co.uk Any Questions?
  • 53. 53 Contents • Intro • Getting Started • Legacy Example • Non-deterministic Output • Customisability • Visualising Differences • Summary •Appendix: Challenges
  • 55. 55 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/
  • 56. 56 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
  • 57. 57 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
  • 58. 58 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!)

Hinweis der Redaktion

  1. Thank ou
  2. A lot of this stuff you can get going on your own. If you get stuck, I can help you!