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

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

  • 1.
    C++ Unit testing Thegood, the bad and the ugly Dror Helper | @dhelper | http://helpercode.com
  • 2.
    Consultant @CodeValue Developing softwaresince 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 toC++ 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 iteasy 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 Routeto 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 aunit 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 frameworkcapabilities Tests & Fixtures Assertio ns Test Runners
  • 10.
    C++ Unit TestingFrameworks The Good • Many choices • Many Styles The Bad • Documentation may vary • Not all maintained The Ugly • Deployment • No Reflection…
  • 11.
    * at leastuntil C++/2020
  • 12.
  • 13.
    Multiple asserts • Loseinformation • Complex tests One assert per test • Writing many tests for the same scenario • See Above The multiple assertion dilemma
  • 14.
    Innovation in C++ Enablingreadable 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 TheGood • 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
  • 18.
  • 19.
    Don’t use obsoletetools! 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 inWindows • 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 chooseyour 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 WORLDTHERE’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