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.

Files at this revision

API Documentation at this revision

Comitter:
Rohit Grover
Date:
Thu Jun 19 08:24:31 2014 +0100
Parent:
1:4769360130ed
Commit message:
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.

Changed in this revision

src/CppUTest/CommandLineTestRunner.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/src/CppUTest/CommandLineTestRunner.cpp	Tue Jun 17 15:52:54 2014 +0100
+++ b/src/CppUTest/CommandLineTestRunner.cpp	Thu Jun 19 08:24:31 2014 +0100
@@ -53,19 +53,19 @@
 	int result = 0;
 	ConsoleTestOutput output;
 
-	// MemoryLeakWarningPlugin memLeakWarn(DEF_PLUGIN_MEM_LEAK);
-	// memLeakWarn.destroyGlobalDetectorAndTurnOffMemoryLeakDetectionInDestructor(true);
-	// TestRegistry::getCurrentRegistry()->installPlugin(&memLeakWarn);
+	MemoryLeakWarningPlugin memLeakWarn(DEF_PLUGIN_MEM_LEAK);
+	memLeakWarn.destroyGlobalDetectorAndTurnOffMemoryLeakDetectionInDestructor(true);
+	TestRegistry::getCurrentRegistry()->installPlugin(&memLeakWarn);
 
 	{
 		CommandLineTestRunner runner(ac, av, &output, TestRegistry::getCurrentRegistry());
 		result = runner.runAllTestsMain();
 	}
 
-	// if (result == 0) {
-	// 	output << memLeakWarn.FinalReport(0);
-	// }
-	// TestRegistry::getCurrentRegistry()->removePluginByName(DEF_PLUGIN_MEM_LEAK);
+	if (result == 0) {
+		output << memLeakWarn.FinalReport(0);
+	}
+	TestRegistry::getCurrentRegistry()->removePluginByName(DEF_PLUGIN_MEM_LEAK);
 	return result;
 }