Set up a dummy 'library' and unit test

In this video I go through and set up an example unit test FOOexample with an example FOO library:

In the FOO directory we will set up these two files:

#ifndef __FOO_BAR_H__ #define __FOO_BAR_H__ //----------------------------------------------------------------------------- // Copyright (C) 1997-2019 iNTERFACEWARE Inc. All Rights Reserved // // Module: FOObar // // Description // // Example FOObar library // // Author: Eliot Muir //----------------------------------------------------------------------------- class COLstring; void FOObarAppend(const COLstring& Input, COLstring* pOutput); #endif // end of defensive include

Lnes 1,2 and 19 in the file are how we do defensive includes.

Line 15 is an example of using a type pre-declaration instead of including the COLstring header file.

Line 17 is showing the usage of this convention of using const references for inputs and non const pointers for outputs.

This is the contents of FOObar.cpp:

//--------------------------------------------------------------------------- // Copyright (C) 1997-2019 iNTERFACEWARE Inc. All Rights Reserved // // Module: FOObar // // Description // // Implementation //--------------------------------------------------------------------------- #include <COL/COLstring.h> #include <COL/COLlog.h> COL_LOG_MODULE; void FOObarAppend(const COLstring& Input, COLstring* pOutput){ COL_FUNCTION(FOObarAppend); COL_VAR(Input); COLstring& Out = *pOutput; Out = "<--" + Input + "-->"; COL_VAR(Out); }

This shows the usage of our tracing system. The function in this case is trivial.

Now in the FOOexample directory we will create four files:

  • makefile - the make file for the unit test

  • main.cpp - the main function for the unit test program.

  • testFOObar.h - header file exposing the collection function of unit tests.

  • testFOObar.cpp - cpp file with the unit test implementation in it.

Let’s go through the files. First the makefile:

TARGET=FOOexample DIRS=\ FOO\ UNIT\ CMD\ FIL\ SCK\ IPG\ IPA\ libpcre\ COL\ include ../make/makefile.core

Just a basic makefile to create a binary with the libraries we need. UNIT is our unit test framework.

The main.cpp file has the boilerplate code required to use the unit test framework:

This code should mainly be familiar by now.

The header file testFOObar.h just exposes a single function:

Line 18 exposes the function which we use to add to UNITapp object which has a collection of unit tests.

And here is the implementation:

On the whole I think this is the best practice for using the unit test framework. The unit test framework runs with multiple threads by default. Because of that it doesn’t support having ‘setup’ and ‘teardown’ functions.

Â