Unit tests » History » Revision 5

« Previous | Revision 5/18 (diff) | Next »
Rogers, Chris, 12 April 2011 12:40

Unit Tests

The idea behind unit tests is "does this function do what I think it's doing". We want to check that, when we write a function, we haven't put any subtle (or not so subtle) errors in the code. Also we want to check that when we change some other function, it doesn't break existing code. So for each function (or maybe pair of functions) we write a test function. These are known as "unit tests".

C++ unit tests

In C++ we use the google testing framework which has some convenience functions for e.g. equality testing in the presence of floating point errors, setting up common test data, etc. The google test framework should have been installed during the third party libraries. The C++ tests are found at

Sconstruct builds a unit test application in build/test_cpp_unit that you can just run from the command line.

Go ahead and edit the tests if you need to. We should have one source file for each header file in the main body of the code. Each directory in ${MAUS_ROOT_DIR}/src/common/* should be a directory in ${MAUS_ROOT_DIR}/tests/cpp_unit. If you want to add an extra test, you need to make sure that it includes gtest

#include "gtest/gtest.h"
It should be automatically added to the test executable by scons.

Google tests documentation

Python unit tests

Python unit tests are found at


A Bit More on Unit Test Concept

The idea behind unit tests is to test at the level of the smallest unit that the code does what we think. We test at the smallest unit so that:

  • test coverage is high
  • tests are quick to run
  • we get logically separated functions

Let's consider each of these points individually

  • test coverage is high - if we imagine the execution path following some branch structure, then we get many more possible execution paths for longer code snippets. So maintaining high test coverage becomes very difficult, and we need many more tests to have good test coverage.
  • tests are quick to run - the execution time goes as the (number_of_tests)*(length_of_each_test). Now, we have many more tests to keep a good test coverage, and each test is longer because they are testing bigger code snippets. This means tests are slowwww. You want to actually run the tests, and an essential part of this is making sure they are quick enough.
  • we get logically separated functions - functions that do simple, understandable things are less likely to be buggy than functions that do complicated or difficult things. The process of really testing if a function does what you intended forces us to make code simple and understandable - otherwise the test becomes difficult to write.

Updated by Rogers, Chris over 12 years ago · 5 revisions