SlideShare a Scribd company logo
C++ Unit testing
The good, the bad and the
ugly
Dror Helper | @dhelper | http://helpercode.com
Consultant @CodeValue
Developing software since 2002
Clean Coder & Test Driven Developer
Pluralsight author
https://www.pluralsight.com/authors/dror-helper
B: http://helpercode.com
T: @dhelper
About.ME
An ode to C++
Multi-paradigm Performance & Speed Truly multi-platform Plays well with others
Extendible and powerful Low level programming
My compiler compiled
your compiler’s compiler
“C makes it easy to shoot yourself
in the foot;
C++ makes it harder, but when you
do it blows your whole leg off.”
Bjarne Stroustrup
Avoid stupid
bugs
Early feedback
Avoid
Regression
Write
Sustainable,
manageable
code
Route to
modular OO
design
In code
documentation
Have you tried unit testing your code?
Definition: Unit Test (by me)
A method (code)
that tests specific functionality,
Has clear pass/fail criteria
and runs in isolation
This is a unit test (GTest)
TEST(MovieTests, WhatWouldTucoDo)
{
Charcter tuco;
tuco.SetHaveToShoot(true);
ASSERT_FALSE(tuco.GetTalk());
}
What you’ll need
Developer’s
Machine
• Unit testing framework
• Mocking framework
• Code coverage tool
• Other (IDE, Refactoring tools)
Continuous
Integration
• CI Server
• Build Agent(s)
Unit testing framework capabilities
Tests &
Fixtures
Assertio
ns
Test
Runners
C++ Unit Testing Frameworks
The Good
• Many choices
• Many Styles
The Bad
• Documentation may vary
• Not all maintained
The Ugly
• Deployment
• No Reflection…
* at least until C++/2020
C++ Unit Testing
Frameworks
GTest
MSTest
Catch
CppUnit
Boost.Test
DocTest
lest
xUnit++
Mettle
A Short, incomplete list…
Multiple asserts
• Lose information
• Complex tests
One assert per test
• Writing many tests for
the same scenario
• See Above
The multiple assertion dilemma
Innovation in C++
Enabling readable and maintainable tests
• Failures without exceptions
• Better failure messages
• Single, powerful assert
• Rethinking test fixture - Sections
16
What is a “Mock”?
“mock objects are simulated objects that
mimic the behavior of real objects in
controlled ways”
[From Wikipedia]
I prefer to call them “Fakes”
Mocking frameworks responsibilities
1. Create Fake/Mock objects
2. Set behavior on fakes
3. Verify that methods were called
C++ Mocking Frameworks
The Good
• C++ has mocking frameworks
The Bad
• I meant a framework (at least until recently)
• Beware of dead projects
The Ugly
• Most one step better than hand rolled mocks
• Most are not AAA compliant
C++ Mocking
Frameworks
GMock
Trompeloeil
HippoMocks
FakeIt
*Based on my own experience
Don’t use obsolete tools!
1. Check when was the last commit!
2. How extensive is the documentation
C++ is changing and so should your testing framework
C++ Development in Windows
• Multi-platform – develop on windows, compile to run everywhere
• Remote debug/run on Linux (VS2017)
• Use test runner (or develop your own)
• Code coverage support
How to choose your unit testing tools
Multi-platform vs. mono-platform
Ease of use
What happens when test fails
Integration points
Runner Support
Ease of deployment
Personal taste
IN THIS WORLD THERE’S
TWO TYPES
OF PEOPLE MY FRIEND.
THOSE WITH UNIT TESTS
AND THOSE WHO BLAME.
WRITE UNIT TESTS!
Thank you
Dror Helper | @dhelper | http://helpercode.com

More Related Content

PPTX
Cpp unit
PDF
C++ Unit Test with Google Testing Framework
PDF
Unit testing best practices
PDF
An introduction to unit testing
PPT
PDF
Java conditional statements
PPTX
JUNit Presentation
PPT
Google test training
Cpp unit
C++ Unit Test with Google Testing Framework
Unit testing best practices
An introduction to unit testing
Java conditional statements
JUNit Presentation
Google test training

What's hot (20)

PPT
Exception handling in java
PPTX
Unit tests & TDD
PPTX
Conditional Statement in C Language
PPTX
Unit Testing And Mocking
PDF
An introduction to Google test framework
PPTX
Java exception handling
PPSX
PDF
Clean code
PDF
C++ Files and Streams
PPTX
exception handling
DOCX
Java loops
PPTX
Unit Testing Concepts and Best Practices
PPTX
Clean code
PPSX
Exception Handling
PPTX
Operators and expressions in C++
PPTX
Introduction to JUnit
PPTX
TDD - Test Driven Development
PPTX
Exception handling in JAVA
PDF
The Power of Composition
PDF
Exception handling
Exception handling in java
Unit tests & TDD
Conditional Statement in C Language
Unit Testing And Mocking
An introduction to Google test framework
Java exception handling
Clean code
C++ Files and Streams
exception handling
Java loops
Unit Testing Concepts and Best Practices
Clean code
Exception Handling
Operators and expressions in C++
Introduction to JUnit
TDD - Test Driven Development
Exception handling in JAVA
The Power of Composition
Exception handling
Ad

Similar to C++ Unit testing - the good, the bad & the ugly (20)

PPTX
Testing & should i do it
PDF
I am afraid of no test! The power of BDD
PDF
Python and test
PPT
Assessing Unit Test Quality
PDF
Unit Testing in R with Testthat - HRUG
PDF
How ANDROID TESTING changed how we think about Death - Second Edition
PDF
How ANDROID TESTING changed how we think about Death - Second Edition
PDF
A. Sirota "Building an Automation Solution based on Appium"
PDF
Robotium at Android Only 2010-09-29
PDF
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP
PDF
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
PPTX
Dot all 2019 | Testing with Craft | Giel Tettelar
PPTX
Test-Driven Development In Action
PDF
Testing in a glance
PDF
TDD Workshop UTN 2012
KEY
Enterprise Strength Mobile JavaScript
PPTX
Containerize your Blackbox tests
PPTX
Bootstrapping Quality
PDF
Agile Testing Pasadena JUG Aug2009
PPTX
Unit Testing in Android
Testing & should i do it
I am afraid of no test! The power of BDD
Python and test
Assessing Unit Test Quality
Unit Testing in R with Testthat - HRUG
How ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second Edition
A. Sirota "Building an Automation Solution based on Appium"
Robotium at Android Only 2010-09-29
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
Dot all 2019 | Testing with Craft | Giel Tettelar
Test-Driven Development In Action
Testing in a glance
TDD Workshop UTN 2012
Enterprise Strength Mobile JavaScript
Containerize your Blackbox tests
Bootstrapping Quality
Agile Testing Pasadena JUG Aug2009
Unit Testing in Android
Ad

More from Dror Helper (20)

PPTX
Unit testing patterns for concurrent code
PPTX
The secret unit testing tools no one ever told you about
PPTX
Debugging with visual studio beyond 'F5'
PPTX
From clever code to better code
PPTX
From clever code to better code
PPTX
A software developer guide to working with aws
PPTX
The secret unit testing tools no one has ever told you about
PPTX
The role of the architect in agile
PDF
Harnessing the power of aws using dot net core
PPTX
Developing multi-platform microservices using .NET core
PPTX
Harnessing the power of aws using dot net
PPTX
Secret unit testing tools no one ever told you about
PPTX
Working with c++ legacy code
PPTX
Visual Studio tricks every dot net developer should know
PPTX
Secret unit testing tools
PPTX
Electronics 101 for software developers
PPTX
Navigating the xDD Alphabet Soup
PPTX
Building unit tests correctly
PPTX
Who’s afraid of WinDbg
PPTX
Unit testing patterns for concurrent code
Unit testing patterns for concurrent code
The secret unit testing tools no one ever told you about
Debugging with visual studio beyond 'F5'
From clever code to better code
From clever code to better code
A software developer guide to working with aws
The secret unit testing tools no one has ever told you about
The role of the architect in agile
Harnessing the power of aws using dot net core
Developing multi-platform microservices using .NET core
Harnessing the power of aws using dot net
Secret unit testing tools no one ever told you about
Working with c++ legacy code
Visual Studio tricks every dot net developer should know
Secret unit testing tools
Electronics 101 for software developers
Navigating the xDD Alphabet Soup
Building unit tests correctly
Who’s afraid of WinDbg
Unit testing patterns for concurrent code

Recently uploaded (20)

PPTX
L1 - Introduction to python Backend.pptx
PPTX
history of c programming in notes for students .pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
medical staffing services at VALiNTRY
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Essential Infomation Tech presentation.pptx
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Introduction to Artificial Intelligence
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Transform Your Business with a Software ERP System
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
L1 - Introduction to python Backend.pptx
history of c programming in notes for students .pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
medical staffing services at VALiNTRY
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Reimagine Home Health with the Power of Agentic AI​
Odoo Companies in India – Driving Business Transformation.pdf
Essential Infomation Tech presentation.pptx
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Introduction to Artificial Intelligence
Upgrade and Innovation Strategies for SAP ERP Customers
Design an Analysis of Algorithms I-SECS-1021-03
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
How to Choose the Right IT Partner for Your Business in Malaysia
Design an Analysis of Algorithms II-SECS-1021-03
Transform Your Business with a Software ERP System
CHAPTER 2 - PM Management and IT Context
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
wealthsignaloriginal-com-DS-text-... (1).pdf

C++ Unit testing - the good, the bad & the ugly

  • 1. C++ Unit testing The good, the bad and the ugly Dror Helper | @dhelper | http://helpercode.com
  • 2. Consultant @CodeValue Developing software since 2002 Clean Coder & Test Driven Developer Pluralsight author https://www.pluralsight.com/authors/dror-helper B: http://helpercode.com T: @dhelper About.ME
  • 3. An ode to C++ Multi-paradigm Performance & Speed Truly multi-platform Plays well with others Extendible and powerful Low level programming My compiler compiled your compiler’s compiler
  • 4. “C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.” Bjarne Stroustrup
  • 5. Avoid stupid bugs Early feedback Avoid Regression Write Sustainable, manageable code Route to modular OO design In code documentation Have you tried unit testing your code?
  • 6. Definition: Unit Test (by me) A method (code) that tests specific functionality, Has clear pass/fail criteria and runs in isolation
  • 7. This is a unit test (GTest) TEST(MovieTests, WhatWouldTucoDo) { Charcter tuco; tuco.SetHaveToShoot(true); ASSERT_FALSE(tuco.GetTalk()); }
  • 8. What you’ll need Developer’s Machine • Unit testing framework • Mocking framework • Code coverage tool • Other (IDE, Refactoring tools) Continuous Integration • CI Server • Build Agent(s)
  • 9. Unit testing framework capabilities Tests & Fixtures Assertio ns Test Runners
  • 10. C++ Unit Testing Frameworks The Good • Many choices • Many Styles The Bad • Documentation may vary • Not all maintained The Ugly • Deployment • No Reflection…
  • 11. * at least until C++/2020
  • 13. Multiple asserts • Lose information • Complex tests One assert per test • Writing many tests for the same scenario • See Above The multiple assertion dilemma
  • 14. Innovation in C++ Enabling readable and maintainable tests • Failures without exceptions • Better failure messages • Single, powerful assert • Rethinking test fixture - Sections
  • 15. 16 What is a “Mock”? “mock objects are simulated objects that mimic the behavior of real objects in controlled ways” [From Wikipedia] I prefer to call them “Fakes”
  • 16. Mocking frameworks responsibilities 1. Create Fake/Mock objects 2. Set behavior on fakes 3. Verify that methods were called
  • 17. C++ Mocking Frameworks The Good • C++ has mocking frameworks The Bad • I meant a framework (at least until recently) • Beware of dead projects The Ugly • Most one step better than hand rolled mocks • Most are not AAA compliant
  • 19. Don’t use obsolete tools! 1. Check when was the last commit! 2. How extensive is the documentation C++ is changing and so should your testing framework
  • 20. C++ Development in Windows • Multi-platform – develop on windows, compile to run everywhere • Remote debug/run on Linux (VS2017) • Use test runner (or develop your own) • Code coverage support
  • 21. How to choose your unit testing tools Multi-platform vs. mono-platform Ease of use What happens when test fails Integration points Runner Support Ease of deployment Personal taste
  • 22. IN THIS WORLD THERE’S TWO TYPES OF PEOPLE MY FRIEND. THOSE WITH UNIT TESTS AND THOSE WHO BLAME. WRITE UNIT TESTS!
  • 23. Thank you Dror Helper | @dhelper | http://helpercode.com

Editor's Notes

  • #8: A method (code) that tests specific functionality, Has clear pass/fail criteria and runs in isolation