I have been “living” in the C++ world since the very beginning (of my career). But lately I’ve noticed a rising interest in C++ unit testing and even got to speaking about it in NDC Oslo. But there’s more to it than testing 10 year old C++ legacy code – it is my opinion that C++ is the most innovative language as far as unit testing goes. So if you’re not a C++ developer (and survived this post so far) you should check some of the innovation in the C++ unit testing space.
Because I couldn’t find one I’ve decided to compile a list of resources for C++ developers who wants to learn more about TDD and unit testing, based on my own experience and preferences.
I love reading books, I know I might be a part of a dying breed but I think they contain knowledge in an organized way that is missing from other resources (present medium included). In the C++ unit testing space there are two books I read many years ago but I still find useful in my daily work:
You gotta love this book. It has a great name and good content. It takes by the hand showing you how to TDD your code – one step at a time (as TDD should be practiced). The book focus mainly on GTest and some of the later chapters use CppUnit.
Although I don’t agree with everything in this book, I found it to be useful for anyone who wants to start learning about TDD and unit testing in the C++ world. Keep in mind that things have changed a bit in the time that passed since this book was published but the basics are still the same. I wrote a review of this book three years ago – and I still stand by it.
This book is a timeless classic – and it has helped me solve hard, difficult and frustrating problems – many times. Once you learn about unit testing and TDD you would want to apply your new powers in the real world – and then you must read this book.
You don’t have to be a C++ developer I but it helps since some of the examples and techniques are in C++.
This book shows you how unit testing can save your code from a slow and painful death and teaches developers what to do when facing a huge, old, complex and convoluted legacy code.
To get you up and running you’ll need a good unit testing framework and probably a good mocking framework as well. The good news is that there are many styles to choose from with many innovative abilities which transcend the classic unit testing framework styles. I have to confess that I haven’t used all of the possible unit testing frameworks out there but I have used quite a lot of them – and the following list is built using only my (incomplete) experience
Unit testing frameworks
- GTest/Gmock – this power couple has been the standard de-facto of C++ unit testing. In fact for along long time GMock was the only production grade mocking framework in the C++ world. The advantage is that if you have GMock you get GTest fully integrated in the same package. The downside is that you need to build GTest/GMock using the exact configuration as the project you’re trying to test – which can be a pain.
- Catch – This is my new favorite unit testing framework (not just in C++). Deployment is done by including a single header file in your test project and you’re ready to go. Catch has many cool features – a single, powerful assert, strings for test names and an amazing feature called “sections” – you can read more about it in a blog post I wrote: “Catch – multi-paradigm automated test framework for C++“. Catch has made such an impact with it’s simple yet powerful capabilities that several C++ framework such as DocTest and lest have adopted them and “catch like C++ unit testing framework is becoming a way to categorize a framework just like “xUnit testing framework” was used until now.
- Mettle – many good ideas in this framework, which is a bit different from others. I like the assertions (fully customization) and different syntax it brings with it. I think that this is the kind of framework that would help us better understand and use unit testing.
- Boost.Test – part of boost library and used in some projects. I have not had a lot of time to use it but I found it a bit too verbose but I like to give it another try before deciding.
For a long long time GMock was the go to framework for C++ mocking. It comes boundled with GTest and for crying out loud – it was created by the all mighty Google.
Today there are other options such as Trompeloeil which is deployed by including a single header file and has syntax I enjoy using. and FakeIt which is a cool, amazing library which helps developers write fake objects with very minimal code in a syntax that might be familiar to you if you ever using Mockito (Java) or any .NET mocking framework out there.
There are many good talks about C++ unit testing and I wish I could see them all – or at least know about all of them, if you know of a recording I forgot to mention – please let me know in the comments section.
My C++ unit testing/testing legacy C++ code sessions can be found at the recording section of this blog.
Yes it’s Catch again – explained by it’s creator Phil Nash (from CppCon):
One more session by a framework creator:
There are not many (read: none that I know of) courses on C++ unit testing and so a year ago I’ve decided to remedy that buy recording two courses for Pluralsight:
- C++ Unit Testing Fundamentals Using Catch – an introduction to the world of unit testing (in C++) using the Catch framework.
- Advanced C++ Mocking Using Google Mock – using GTest and GMock to test real-world code and take control over legacy code.
I’ve enjoyed creating those courses and I hope other developers find them usful.
Is that it?
This is the resource I thought would be useful for C++ developers who what to start unit testing (TDD’ing) their code and do not know where to start. I’m sure there are other good resources – which I would like to learn about. So in case I forgot or more likely was not aware of a cool library, online course or guru – just let me know.
And until then – happy coding…