Getting started with C++ unit testing

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.

The Books

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:

Modern C++ Programming with Test-Driven Development (code better, sleep better)

lotdd.jpgYou 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.

Working effectively with legacy code

51H6SHy6g2L._SX374_BO1,204,203,200_.jpg

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.

The frameworks

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.

Mocking frameworks

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. On top of that there’s HippoMocks – although I’ve yet to try it in a client environment seems like another option C++ developers (myself included) might want to look into.

If you want to see the syntax of each check this repository on GitHub and you should check the recording of my NDC Oslo session: C++ Unit testing – the good, the bad & the ugly.

Other resources

Conferences

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 Meeting C++):

One more session by a framework creator:

Online courses

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:

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…

4 thoughts on “Getting started with C++ unit testing

  1. Thanks for the shout-out to Catch 🙂
    It’s a shame you picked probably my worst Catch talk to use as an example. I’d probably go with my Meeting C++, or NDC Oslo ones (https://www.youtube.com/watch?v=C2LcIp56i-8 and https://vimeo.com/131632252).
    Note I’m also back on the road with an updated Catch talk – particularly talking about Catch2.

    Also missing here is mention of HippoMocks – another great mocking framework for C++ (Peter Bindels (HippoMocks), Björn Fahller (Trompeloeil) and I know each other quite well and have been talking about better integrating our respective frameworks).

    1. Sorry about that, I’ll update the link, as for HyppoMock I think I need more experience using it before I can talk about it but I’ll add a link as well. BTW Did you had the chance to look into FakeIt, I’ve been using it for the last few months and it works without any boilerplate code – but has a few limitations the other frameworks do not have.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s