CppUTest is a C /C++ based unit xUnit test framework for unit testing and for test-driving your code.

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.

Revision:
0:0b799af9d58e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/CppUTest/StandardCLibrary.h	Tue Jan 28 09:27:41 2014 +0000
@@ -0,0 +1,68 @@
+
+#ifndef STANDARDCLIBRARY_H_
+#define STANDARDCLIBRARY_H_
+
+#include "CppUTestConfig.h"
+
+#if CPPUTEST_USE_STD_C_LIB
+
+/* Needed for size_t */
+#include <stddef.h>
+
+/* Sometimes the C++ library does an #undef in stdlib of malloc and free. We want to prevent that */
+#ifdef __cplusplus
+ #if CPPUTEST_USE_STD_CPP_LIB
+  #include <cstdlib>
+ #endif
+#endif
+
+/* Needed for malloc */
+#include <stdlib.h>
+
+/* Needed for ... */
+#include <stdarg.h>
+
+#else
+
+#ifdef __KERNEL__
+
+/* Unfinished and not working! Hacking hacking hacking. Why bother make the header files C++ safe! */
+#define false kernel_false
+#define true kernel_true
+#define bool kernel_bool
+#define new kernel_new
+#define _Bool int
+#include <linux/acpi.h>
+#include <linux/types.h>
+#undef false
+#undef true
+#undef bool
+#undef new
+
+#else
+
+/*
+ * #warning "These definitions in StandardCLibrary.h are pure (educated, from linux kernel) guesses at the moment. Replace with your platform includes."
+ * Not on as warning are as errors :P
+ */
+
+#ifdef __SIZE_TYPE__
+typedef __SIZE_TYPE__ size_t;
+#else
+typedef long unsigned int size_t;
+#endif
+
+typedef char* va_list;
+#define NULL (0)
+extern void*	malloc(size_t);
+extern void     free(void *);
+
+#define _bnd(X, bnd)            (((sizeof (X)) + (bnd)) & (~(bnd)))
+#define va_start(ap, A)         (void) ((ap) = (((char *) &(A)) + (_bnd (A,sizeof(int)-1))))
+#define va_end(ap)              (void) 0
+
+#endif
+
+#endif
+
+#endif