Programming in C++

Unit Testing

Gerald Senarclens de Grancy

Purpose of Unit Tests

What is a Unit Test

  • Allows automated testing of independent code units
  • No replacement for other test categories
  • Write them as early as possible - if possible before the code it tests
  • Tests can serve as useful requirements specification
  • Create a test for each bug you encounter (refacturing!)

Which problems does unit testing solve?

Unit tests...

  • are a first line of defense against bugs
  • improve code quality by dictating testable code
  • allow releasing higher quality early and often
  • give more confidence to programmers
  • are a prerequisit to refactoring
  • can be used as a contract with regard to desired behavior
  • prevent regressions (bugs in features priorly working properly)

Which problems does unit testing cause?

Essentially none, but writing unit tests ...

  • seems to cost time
  • demands discipline
  • is far from trivial
  • does not substitute other kinds of testing

What is a Test(case)

  • Answers a single question about the code it is testing
  • Run completely by itself (automation)
  • Results must not require human interpretation
  • Should be independent of other tests and their order of execution
  • Can serve as documentation of how (not) to use your code

Flaky Tests

  • Have different outcomes without changes to the code
  • Erode confidence in testing processes
  • Fix them (mute / skip them until they are fixed) or delete them

Software Quality

User's view (visible to the user)

  • Functionality
  • Reliability
  • Usability
  • Performance/ efficiency

Producer's view

  • Portability
  • Maintainability
  • Testability (vs. legacy code)
  • Transparency (is the code readable/ understandable?)

We Need a Unit Testing Framework

For writing, compiling and running unit tests, a unit testing framework is required.

There are many concurring unit testing frameworks for C++.

At the time of this writing, Boost Test and GoogleTest stand out in terms of quality and features.

Example: Refactor a Function

// Return the sum of all values from 1 to n.
unsigned long long add_to(unsigned n) {
unsigned long long sum = 0;
for (unsigned i = 1; i <=n; ++i) {
sum += i;
return sum;
Download before_refactoring.cpp

After experiencing performance problems, we want to use a faster algorithm

// Return the sum of all values from 1 to n.
unsigned long long add_to(unsigned n) {
return (unsigned long long) (n / 2.0) * (n + 1);
Download after_refactoring.cpp

How can we apply the change without risking to destroy our application?

  1. Define good test cases
  2. Implement test cases using your project's unit testing library
  3. Run the tests before the change to ensure they pass
  4. Apply the change, then run the tests again

1. Define Good Test Cases

Using pencil and paper / brainstorming: define relevant test cases

  • All code paths should be used
  • Add one or more common cases
  • Include relevant edge cases

25 is a possible common case. Add 26 as an even input. The edge cases should include 0 and 4294967295.
Expected results for these are 325, 351, 0 and 9223372034707292160.

Let's also add 500000000 (expected result 125000000250000000) as 4294967295 does not terminate with the current implementation.

2. Implement defined test cases

#include "gtest/gtest.h"

unsigned long long add_to(unsigned n); // usually in header file

TEST(sum_to, fast_test) {
EXPECT_EQ(add_to(0), 0);
EXPECT_EQ(add_to(25), 325);
EXPECT_EQ(add_to(26), 351);
EXPECT_EQ(add_to(500000000), 125000000250000000);

// does not terminate for the old code version due to an overflow of the
// loop counter (the loop would terminate only at UINT_MAX + 1)
TEST(sum_to, slow_test) {
EXPECT_EQ(add_to(4294967295), 9223372034707292160);
Download test_sum_to.cpp

3. Run the Unittests Before the Change

clang++ before_refactoring.cpp test_sum_to.cpp -lgtest_main -lgtest -o run_tests
./run_tests # does not terminate; interrupt via [Ctrl]+[C]

What to do now?

./run_tests --help
./run_tests --gtest_list_tests # list all available tests
./run_tests --gtest_filter=sum_to.fast_test

4. Apply the Change, then Run the Tests Again

For the sake of the presentation, instead of applying the change, we use another source file.

clang++ after_refactoring.cpp test_sum_to.cpp -lgtest_main -lgtest -o run_tests

Now the tests are fast, but they are failing. Fix the code, then compile and run the tests again.



sudo apt install googletest
sudo mkdir /usr/src/googletest/build
sudo chown $USER /usr/src/googletest/build
cd /usr/src/googletest/build
cmake ..
make -j4
sudo make install
sudo ldconfig

For other operating systems, follow the instructions in project's README.


Comprehensive documentation can be found in GoogleTest User’s Guide.

Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.
– Martin Fowler

and feedback...