C++ is a powerful language when used by a seasoned developer. It is being used everywhere from operating systems and compilers to cross platform mobile apps development.
But this power comes with a cost - C++ programming is not simple nor easy – with memory leaks, stack overflows and strange exceptions, a software developer needs all of the power he can use.
By harnessing the power of unit tests a software developer can find defects quickly, avoid regression and speed up development time.
In this talk I’ll show how to use the right unit testing tools and practices available that will help you get your C++ code under control and even test legacy code which no one ever want to touch.
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
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)
10. C++ Unit Testing Frameworks
The Good
• Many choices
• Many Styles
The Bad
• Documentation may vary
• Not all maintained
The Ugly
• Deployment
• No Reflection…
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”
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!