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
- The CppUTest manual can be found at CppUTest.org
- If you have any question, check out the Google Groups
- The sources from which this library was derived may be found at the main github page
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.
include/CppUTest/CppUTestConfig.h
- Committer:
- rgrover1
- Date:
- 2014-01-28
- Revision:
- 0:0b799af9d58e
- Child:
- 1:4769360130ed
File content as of revision 0:0b799af9d58e:
/* * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the <organization> nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef CPPUTESTCONFIG_H_ #define CPPUTESTCONFIG_H_ #ifdef HAVE_CONFIG_H #include "config.h" #endif /* * This file is added for some specific CppUTest configurations that earlier were spread out into multiple files. * * The goal of this file is to stay really small and not to include other things, but mainly to remove duplication * from other files and resolve dependencies in #includes. * */ /* * Lib C dependencies that are currently still left: * * stdarg.h -> We use formatting functions and va_list requires to include stdarg.h in SimpleString * stdlib.h -> The TestHarness_c.h includes this to try to avoid conflicts in its malloc #define. This dependency can * easily be removed by not enabling the MALLOC overrides. * * Lib C++ dependencies are all under the CPPUTEST_USE_STD_CPP_LIB. * The only dependency is to <new> which has the bad_alloc struct * */ /* Do we use Standard C or not? When doing Kernel development, standard C usage is out. */ #ifndef CPPUTEST_USE_STD_C_LIB #ifdef CPPUTEST_STD_C_LIB_DISABLED #define CPPUTEST_USE_STD_C_LIB 0 #else #define CPPUTEST_USE_STD_C_LIB 1 #endif #endif #if NEED_TO_ENABLE_CPP_SUPPORT /* disabled by default because mbed's * toolchain doesn't support exceptions. */ /* Do we use Standard C++ or not? */ #ifndef CPPUTEST_USE_STD_CPP_LIB #ifdef CPPUTEST_STD_CPP_LIB_DISABLED #define CPPUTEST_USE_STD_CPP_LIB 0 #else #define CPPUTEST_USE_STD_CPP_LIB 1 #endif #endif #endif /* #if NEED_TO_ENABLE_CPP_SUPPORT */ /* Is memory leak detection enabled? * Controls the override of the global operator new/deleted and malloc/free. * Without this, there will be no memory leak detection in C/C++. */ #if NEED_TO_ENABLE_MEMORY_LEAK_DETECTION #ifndef CPPUTEST_USE_MEM_LEAK_DETECTION #ifdef CPPUTEST_MEM_LEAK_DETECTION_DISABLED #define CPPUTEST_USE_MEM_LEAK_DETECTION 0 #else #define CPPUTEST_USE_MEM_LEAK_DETECTION 1 #endif #endif #endif /* #if NEED_TO_ENABLE_MEMORY_LEAK_DETECTION */ /* Create a __no_return__ macro, which is used to flag a function as not returning. * Used for functions that always throws for instance. * * This is needed for compiling with clang, without breaking other compilers. */ #ifndef __has_attribute #define __has_attribute(x) 0 #endif #if __has_attribute(noreturn) #define __no_return__ __attribute__((noreturn)) #else #define __no_return__ #endif #if __has_attribute(format) #define __check_format__(type, format_parameter, other_parameters) __attribute__ ((format (type, format_parameter, other_parameters))) #else #define __check_format__(type, format_parameter, other_parameters) /* type, format_parameter, other_parameters */ #endif /* * When we don't link Standard C++, then we won't throw exceptions as we assume the compiler might not support that! */ #if CPPUTEST_USE_STD_CPP_LIB #if defined(__cplusplus) && __cplusplus >= 201103L #define UT_THROW(exception) #define UT_NOTHROW noexcept #else #define UT_THROW(exception) throw (exception) #define UT_NOTHROW throw() #endif #else #define UT_THROW(exception) #define UT_NOTHROW #endif /* * CLang's operator delete requires an NOTHROW block. For now, when we use CLang, then have an empty exception specifier. * However, this ought to be done inside the configure.ac in the future. */ #ifdef __clang__ #undef UT_NOTHROW #define UT_NOTHROW throw() #endif /* * g++-4.7 with stdc++11 enabled On MacOSX! will have a different exception specifier for operator new (and thank you!) * I assume they'll fix this in the future, but for now, we'll change that here. * (This should perhaps also be done in the configure.ac) */ #ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __APPLE__ #ifdef _GLIBCXX_THROW #undef UT_THROW #define UT_THROW(exception) _GLIBCXX_THROW(exception) #endif #endif #endif /* * Detection of different 64 bit environments */ #if defined(__LP64__) || defined(_LP64) || (defined(__WORDSIZE) && (__WORDSIZE == 64 )) || defined(__x86_64) || defined(_WIN64) #define CPPUTEST_64BIT #if defined(_WIN64) #define CPPUTEST_64BIT_32BIT_LONGS #endif #endif #if defined(__cplusplus) && __cplusplus >= 201103L #define _override override #else #define _override #endif /* Should be the only #include here. Standard C library wrappers */ #include "StandardCLibrary.h" #endif