Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

CppUTest tests step by step

offa edited this page Mar 19, 2016 · 10 revisions

A step-by-step guide to setup and run CppUTest tests with NetBeans.

For a faster guide using the file wizard see: CppUTest tests the easy way

1. Setup CppUTest

Setup CppUTest as described here.

2. Create a C/C++ NetBeans Project

Create a new C/C++ Project:

New Project -> C/C++ -> Select a project type

Optional: Create a test folder / logical folder within Test Files.

In this example, a test folder cpputest is used.

3. Add CppUTest to NetBeans

As other libraries, CppUTest has to be added to the NetBeans project. This includes:

  • Include path (<cpputest dir>/include/)
  • Library (libCppUTest from <cpputest binary dir>/CppUTest/)

If the CppUTest extensions are used, libCppUTestExt from <cpputest binary dir>/CppUTestExt/ is added too.

The settings can be added to the whole project, or just to the Test Files. As a recommendation add those settings to Test Files only.

3.1 Include path

Add the include path to ...

Project / Test files properties -> C++ Compiler -> Include directories

3.2 Library

Add the libraries to ...

Project / Test files properties -> Linker -> Libraries and Additional Library Directories.

4. Create Tests files

Beside regular test source files, CppUTest requires a single test main. For the first, two C++ files are created:

  • AllTests.cpp - test main
  • ExampleSuite.cpp - test suite

Project structure

4.1 Test main

Create a new C++ source file AllTests.cpp within the test directory. This file provides the main for the tests.

CppUTest doesn't run with verbose output by default. Therefore the verbose output must be enabled in order to provide enough information about the tests ran.

A possible way to do so is to extends the commandline arguments of main with -v.

AllTests.cpp

#include <vector>
#include <CppUTest/CommandLineTestRunner.h>

int main(int argc, char** argv)
{
    std::vector<const char*> args(argv, argv + argc); // Insert all arguments
    args.push_back("-v"); // Set verbose mode
    args.push_back("-c"); // Set color output (OPTIONAL)

    // Run all tests
    return RUN_ALL_TESTS(args.size(), &args[0]);
}

Note: This file is included in examples/cpputest and can be used as boilerplate for further test suites.

4.2 Test suite

Create a second C++ source file ExampleSuite.cpp which will be filled with some simple examples.

ExampleSuite.cpp

#include <CppUTest/TestHarness.h>

TEST_GROUP(ExampleSuite)
{
};


TEST(ExampleSuite, testInt)
{
    int i = 3;
    
    CHECK_EQUAL(3, i);
}

TEST(ExampleSuite, testString)
{
    const char* str = "example";
    
    STRCMP_EQUAL("example", str);
    STRCMP_NOCASE_EQUAL("EXAMPLE", str);
}

TEST(ExampleSuite, testDouble)
{
    double d = 3.67;
    
    DOUBLES_EQUAL(3.64, d, 0.1);
}

TEST(ExampleSuite, testPointer)
{
    int i = 3;
    int *p2 = &i;
    
    POINTERS_EQUAL(&i, p2);
}

TEST(ExampleSuite, testWillFail)
{
    int wrong = 3;

    CHECK_EQUAL(762, wrong); // This will fail!
}

Note: For more information about writing tests with CppUTest, please refer to the CppUTest manual.

While the first tests should pass, the last one is intended to fail.

5. Run tests

The are started by either clicking Test on the project, the test folder or pressing the shortcut.

If everything is ok, the tests are compiled and executed. Finally the test result window is shown (if not, a balloon-tip will appear; the result window can be set to show up per default after tests are exuted).

Test results - one failed

As expected the testcase testWillFail failed.

The "bug" needs not much to fix, change ...

TEST(ExampleSuite, testWillFail)
{
    int wrong = 3;

    CHECK_EQUAL(762, wrong); // This will fail!
}

to ...

TEST(ExampleSuite, testWillFail)
{
    int wrong = 762;

    CHECK_EQUAL(762, wrong); // Ok now
}

Running the test suite again will confirm that everything is ok now.

Test results - all ok

6. Add another test suite

Add another C++ source file AnotherSuite.pp.

#include <CppUTest/TestHarness.h>

TEST_GROUP(AnotherSuite)
{   
};


TEST(AnotherSuite, testOne)
{
    CHECK_TRUE(true);
}

TEST(AnotherSuite, testTwo)
{
    CHECK_FALSE(false);
}

The new test suite is added automatically. Running the tests again will show the second suite. Every test passed!

Test results - second test suite