c++ unit testing - the good, the bad & the ugly
TRANSCRIPT
C++ Unit testingThe good, the bad and the uglyDror Helper | @dhelper | http://helpercode.com
Consultant @CodeValue
Developing software since 2002Clean Coder & Test Driven Developer
Pluralsight authorhttps://www.pluralsight.com/authors/dror-helper
B: http://helpercode.comT: @dhelper
About.ME
An ode to C++
Multi-paradigm Performance & Speed Truly multi-platform Plays well with others
Extendible and powerful Low level programmingMy 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 feedbackAvoid
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)
C++ Unit Testing Frameworks
The Good
• Many choices• Many Styles
The Bad
• Documentation may vary• Not all maintained
The Ugly
• Deployment• No Reflection…
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
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 TYPESOF PEOPLE MY FRIEND.THOSE WITH UNIT TESTSAND THOSE WHO BLAME.
WRITE UNIT TESTS!