Appendix C: A development environment for unit testing

Note: the following instructions assume that you are connected to the Internet and that you are using GCC on a POSIX compatible operating system. All unix shell commands are assumed to be running from your home directory ($HOME). Finally, any command that begins with sudo assums that you have administrative rights on your machine. If you do not — please ask your system administrator about installing the software you need.

Placing doctest.h for easy access

doctest is a C++ testing framework that is light on resources and easy to use, making it an ideal tool for beginners learning C++.

Configuring CPATH for Local Libraries

CPATH is an environment variable that specifies the search path that the preprocessor uses to look for somelibrary.h in a:

#include <somelibrary.h>

directive.

To set our CPATH, we need to:

  1. create a directory where C/C++ libraries will be stored.

  2. set the CPATH environment variable to the path to that directory.

On a Debian GNU/Linux system this can be accomplished by:

  1. running: $ mkdir -p ~/.local/lib/gcc/include

  2. adding the line:

    export CPATH=$HOME/.local/lib/gcc/include/
    

    to your .bashrc file

    Note

    MacOS users will have to add this to .zshrc instead.

After copying doctest.h to ~/.local/lib/gcc/include, you will now be able to simply add:

#include <doctest.h>

to any program in which you want to use doctest.

To test your configuration, add the following to a file named test_doctest.cpp:

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest.h>

TEST_CASE("testing doctest") {
    CHECK(2 == 1);
}

Then compile it and run it. Success will mean getting output like this:

test_doctest.cpp:4:
TEST CASE:  testing doctest

test_doctest.cpp:5: ERROR: CHECK( 2 == 1 ) is NOT correct!
  values: CHECK( 2 == 1 )

===============================================================================
[doctest] test cases: 1 | 0 passed | 1 failed | 0 skipped
[doctest] assertions: 1 | 0 passed | 1 failed |
[doctest] Status: FAILURE!

Note

doctest.h uses C++ language features from the C++11 standard. If your compiler is set to default to another C++ standard (as are the school issued MacBooks where I teach), you have to add --std=c++11 to the end of your compiler instruction to use doctest.