Unit Testing framework based on http://cpputest.github.io/

CppUTest

Where to find more information

Getting test reports on the console

You may need to tailor the file src/Platforms/mbed/UtestPlatform.cpp to your needs. In particular, if you want console output, you might want to look at the function PlatformSpecificPutchar().

Quick introduction (some code!)

To write your first test, all you need is a new cpp file with a TEST_GROUP and a TEST, like:

#include "CppUTest/TestHarness.h"

TEST_GROUP(FirstTestGroup)
{
};

TEST(FirstTestGroup, FirstTest)
{
   FAIL("Fail me!");
}

This test will fail.

You can add new tests to the test group by just writing more tests in the file, like this:

TEST(FirstTestGroup, SecondTest)
{
   STRCMP_EQUAL("hello", "world");
   LONGS_EQUAL(1, 2);
   CHECK(false);
}

You do need to trigger the tests from somewhere in your program. It could look something like:

#include "CppUTest/TestRegistry.h"
#include "CppUTest/CommandLineTestRunner.h"

int main(int ac, char** av)
{
    ....
    unsigned failureCount = 0;
    {
        ConsoleTestOutput output;
        CommandLineTestRunner runner(ac, av, &output, TestRegistry::getCurrentRegistry());
        failureCount = runner.runAllTestsMain();
    }

    if (failureCount == 0) {
        console.printf("PASSED\r\n");
    }
    ...
}

For more information, We’d recommend to read the manual or, even better, check some existing tests such as SimpleStringTest or (a bit more complicated) MemoryLeakDetectorTest or the mocking tests or just check out the Cheat Sheet.

Committer:
Rohit Grover
Date:
Thu Jun 19 08:24:31 2014 +0100
Revision:
2:82161d9e7b36
Parent:
0:0b799af9d58e
uncomment the code in CommandLineTestRunner::RunAllTests() having to do with MemoryLeakWarningPlugin

Bas Vodde, from the CppUTest development team has suggested an alternate way to run tests.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:0b799af9d58e 1
rgrover1 0:0b799af9d58e 2 #ifndef STANDARDCLIBRARY_H_
rgrover1 0:0b799af9d58e 3 #define STANDARDCLIBRARY_H_
rgrover1 0:0b799af9d58e 4
rgrover1 0:0b799af9d58e 5 #include "CppUTestConfig.h"
rgrover1 0:0b799af9d58e 6
rgrover1 0:0b799af9d58e 7 #if CPPUTEST_USE_STD_C_LIB
rgrover1 0:0b799af9d58e 8
rgrover1 0:0b799af9d58e 9 /* Needed for size_t */
rgrover1 0:0b799af9d58e 10 #include <stddef.h>
rgrover1 0:0b799af9d58e 11
rgrover1 0:0b799af9d58e 12 /* Sometimes the C++ library does an #undef in stdlib of malloc and free. We want to prevent that */
rgrover1 0:0b799af9d58e 13 #ifdef __cplusplus
rgrover1 0:0b799af9d58e 14 #if CPPUTEST_USE_STD_CPP_LIB
rgrover1 0:0b799af9d58e 15 #include <cstdlib>
rgrover1 0:0b799af9d58e 16 #endif
rgrover1 0:0b799af9d58e 17 #endif
rgrover1 0:0b799af9d58e 18
rgrover1 0:0b799af9d58e 19 /* Needed for malloc */
rgrover1 0:0b799af9d58e 20 #include <stdlib.h>
rgrover1 0:0b799af9d58e 21
rgrover1 0:0b799af9d58e 22 /* Needed for ... */
rgrover1 0:0b799af9d58e 23 #include <stdarg.h>
rgrover1 0:0b799af9d58e 24
rgrover1 0:0b799af9d58e 25 #else
rgrover1 0:0b799af9d58e 26
rgrover1 0:0b799af9d58e 27 #ifdef __KERNEL__
rgrover1 0:0b799af9d58e 28
rgrover1 0:0b799af9d58e 29 /* Unfinished and not working! Hacking hacking hacking. Why bother make the header files C++ safe! */
rgrover1 0:0b799af9d58e 30 #define false kernel_false
rgrover1 0:0b799af9d58e 31 #define true kernel_true
rgrover1 0:0b799af9d58e 32 #define bool kernel_bool
rgrover1 0:0b799af9d58e 33 #define new kernel_new
rgrover1 0:0b799af9d58e 34 #define _Bool int
rgrover1 0:0b799af9d58e 35 #include <linux/acpi.h>
rgrover1 0:0b799af9d58e 36 #include <linux/types.h>
rgrover1 0:0b799af9d58e 37 #undef false
rgrover1 0:0b799af9d58e 38 #undef true
rgrover1 0:0b799af9d58e 39 #undef bool
rgrover1 0:0b799af9d58e 40 #undef new
rgrover1 0:0b799af9d58e 41
rgrover1 0:0b799af9d58e 42 #else
rgrover1 0:0b799af9d58e 43
rgrover1 0:0b799af9d58e 44 /*
rgrover1 0:0b799af9d58e 45 * #warning "These definitions in StandardCLibrary.h are pure (educated, from linux kernel) guesses at the moment. Replace with your platform includes."
rgrover1 0:0b799af9d58e 46 * Not on as warning are as errors :P
rgrover1 0:0b799af9d58e 47 */
rgrover1 0:0b799af9d58e 48
rgrover1 0:0b799af9d58e 49 #ifdef __SIZE_TYPE__
rgrover1 0:0b799af9d58e 50 typedef __SIZE_TYPE__ size_t;
rgrover1 0:0b799af9d58e 51 #else
rgrover1 0:0b799af9d58e 52 typedef long unsigned int size_t;
rgrover1 0:0b799af9d58e 53 #endif
rgrover1 0:0b799af9d58e 54
rgrover1 0:0b799af9d58e 55 typedef char* va_list;
rgrover1 0:0b799af9d58e 56 #define NULL (0)
rgrover1 0:0b799af9d58e 57 extern void* malloc(size_t);
rgrover1 0:0b799af9d58e 58 extern void free(void *);
rgrover1 0:0b799af9d58e 59
rgrover1 0:0b799af9d58e 60 #define _bnd(X, bnd) (((sizeof (X)) + (bnd)) & (~(bnd)))
rgrover1 0:0b799af9d58e 61 #define va_start(ap, A) (void) ((ap) = (((char *) &(A)) + (_bnd (A,sizeof(int)-1))))
rgrover1 0:0b799af9d58e 62 #define va_end(ap) (void) 0
rgrover1 0:0b799af9d58e 63
rgrover1 0:0b799af9d58e 64 #endif
rgrover1 0:0b799af9d58e 65
rgrover1 0:0b799af9d58e 66 #endif
rgrover1 0:0b799af9d58e 67
rgrover1 0:0b799af9d58e 68 #endif