Unit Testing framework based on http://cpputest.github.io/
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.
src/CppUTest/TestHarness_c.cpp@2:82161d9e7b36, 2014-06-19 (annotated)
- Committer:
- Rohit Grover
- Date:
- Thu Jun 19 08:24:31 2014 +0100
- Revision:
- 2:82161d9e7b36
- Parent:
- 1:4769360130ed
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?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rohit Grover |
1:4769360130ed | 1 | /* |
Rohit Grover |
1:4769360130ed | 2 | * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde |
Rohit Grover |
1:4769360130ed | 3 | * All rights reserved. |
Rohit Grover |
1:4769360130ed | 4 | * |
Rohit Grover |
1:4769360130ed | 5 | * Redistribution and use in source and binary forms, with or without |
Rohit Grover |
1:4769360130ed | 6 | * modification, are permitted provided that the following conditions are met: |
Rohit Grover |
1:4769360130ed | 7 | * * Redistributions of source code must retain the above copyright |
Rohit Grover |
1:4769360130ed | 8 | * notice, this list of conditions and the following disclaimer. |
Rohit Grover |
1:4769360130ed | 9 | * * Redistributions in binary form must reproduce the above copyright |
Rohit Grover |
1:4769360130ed | 10 | * notice, this list of conditions and the following disclaimer in the |
Rohit Grover |
1:4769360130ed | 11 | * documentation and/or other materials provided with the distribution. |
Rohit Grover |
1:4769360130ed | 12 | * * Neither the name of the <organization> nor the |
Rohit Grover |
1:4769360130ed | 13 | * names of its contributors may be used to endorse or promote products |
Rohit Grover |
1:4769360130ed | 14 | * derived from this software without specific prior written permission. |
Rohit Grover |
1:4769360130ed | 15 | * |
Rohit Grover |
1:4769360130ed | 16 | * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY |
Rohit Grover |
1:4769360130ed | 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
Rohit Grover |
1:4769360130ed | 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Rohit Grover |
1:4769360130ed | 19 | * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY |
Rohit Grover |
1:4769360130ed | 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
Rohit Grover |
1:4769360130ed | 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
Rohit Grover |
1:4769360130ed | 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
Rohit Grover |
1:4769360130ed | 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
Rohit Grover |
1:4769360130ed | 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
Rohit Grover |
1:4769360130ed | 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Rohit Grover |
1:4769360130ed | 26 | */ |
Rohit Grover |
1:4769360130ed | 27 | |
Rohit Grover |
1:4769360130ed | 28 | #include "CppUTest/TestHarness.h" |
Rohit Grover |
1:4769360130ed | 29 | #include "CppUTest/MemoryLeakDetector.h" |
Rohit Grover |
1:4769360130ed | 30 | #include "CppUTest/TestMemoryAllocator.h" |
Rohit Grover |
1:4769360130ed | 31 | #include "CppUTest/PlatformSpecificFunctions.h" |
Rohit Grover |
1:4769360130ed | 32 | #include "CppUTest/TestHarness_c.h" |
Rohit Grover |
1:4769360130ed | 33 | |
Rohit Grover |
1:4769360130ed | 34 | extern "C" |
Rohit Grover |
1:4769360130ed | 35 | { |
Rohit Grover |
1:4769360130ed | 36 | |
Rohit Grover |
1:4769360130ed | 37 | |
Rohit Grover |
1:4769360130ed | 38 | void CHECK_EQUAL_C_INT_LOCATION(int expected, int actual, const char* fileName, int lineNumber) |
Rohit Grover |
1:4769360130ed | 39 | { |
Rohit Grover |
1:4769360130ed | 40 | UtestShell::getCurrent()->assertLongsEqual((long)expected, (long)actual, fileName, lineNumber, TestTerminatorWithoutExceptions()); |
Rohit Grover |
1:4769360130ed | 41 | } |
Rohit Grover |
1:4769360130ed | 42 | |
Rohit Grover |
1:4769360130ed | 43 | void CHECK_EQUAL_C_REAL_LOCATION(double expected, double actual, double threshold, const char* fileName, int lineNumber) |
Rohit Grover |
1:4769360130ed | 44 | { |
Rohit Grover |
1:4769360130ed | 45 | UtestShell::getCurrent()->assertDoublesEqual(expected, actual, threshold, fileName, lineNumber, TestTerminatorWithoutExceptions()); |
Rohit Grover |
1:4769360130ed | 46 | } |
Rohit Grover |
1:4769360130ed | 47 | |
Rohit Grover |
1:4769360130ed | 48 | void CHECK_EQUAL_C_CHAR_LOCATION(char expected, char actual, const char* fileName, int lineNumber) |
Rohit Grover |
1:4769360130ed | 49 | { |
Rohit Grover |
1:4769360130ed | 50 | UtestShell::getCurrent()->assertEquals(((expected) != (actual)), StringFrom(expected).asCharString(), StringFrom(actual).asCharString(), fileName, lineNumber, TestTerminatorWithoutExceptions()); |
Rohit Grover |
1:4769360130ed | 51 | } |
Rohit Grover |
1:4769360130ed | 52 | |
Rohit Grover |
1:4769360130ed | 53 | void CHECK_EQUAL_C_STRING_LOCATION(const char* expected, const char* actual, const char* fileName, int lineNumber) |
Rohit Grover |
1:4769360130ed | 54 | { |
Rohit Grover |
1:4769360130ed | 55 | UtestShell::getCurrent()->assertCstrEqual(expected, actual, fileName, lineNumber, TestTerminatorWithoutExceptions()); |
Rohit Grover |
1:4769360130ed | 56 | } |
Rohit Grover |
1:4769360130ed | 57 | |
Rohit Grover |
1:4769360130ed | 58 | void FAIL_TEXT_C_LOCATION(const char* text, const char* fileName, int lineNumber) |
Rohit Grover |
1:4769360130ed | 59 | { |
Rohit Grover |
1:4769360130ed | 60 | UtestShell::getCurrent()->fail(text, fileName, lineNumber, TestTerminatorWithoutExceptions()); |
Rohit Grover |
1:4769360130ed | 61 | } |
Rohit Grover |
1:4769360130ed | 62 | |
Rohit Grover |
1:4769360130ed | 63 | void FAIL_C_LOCATION(const char* fileName, int lineNumber) |
Rohit Grover |
1:4769360130ed | 64 | { |
Rohit Grover |
1:4769360130ed | 65 | UtestShell::getCurrent()->fail("", fileName, lineNumber, TestTerminatorWithoutExceptions()); |
Rohit Grover |
1:4769360130ed | 66 | } |
Rohit Grover |
1:4769360130ed | 67 | |
Rohit Grover |
1:4769360130ed | 68 | void CHECK_C_LOCATION(int condition, const char* conditionString, const char* fileName, int lineNumber) |
Rohit Grover |
1:4769360130ed | 69 | { |
Rohit Grover |
1:4769360130ed | 70 | UtestShell::getCurrent()->assertTrue(condition != 0, "CHECK_C", conditionString, fileName, lineNumber, TestTerminatorWithoutExceptions()); |
Rohit Grover |
1:4769360130ed | 71 | } |
Rohit Grover |
1:4769360130ed | 72 | |
Rohit Grover |
1:4769360130ed | 73 | enum { NO_COUNTDOWN = -1, OUT_OF_MEMORRY = 0 }; |
Rohit Grover |
1:4769360130ed | 74 | static int malloc_out_of_memory_counter = NO_COUNTDOWN; |
Rohit Grover |
1:4769360130ed | 75 | static int malloc_count = 0; |
Rohit Grover |
1:4769360130ed | 76 | |
Rohit Grover |
1:4769360130ed | 77 | void cpputest_malloc_count_reset(void) |
Rohit Grover |
1:4769360130ed | 78 | { |
Rohit Grover |
1:4769360130ed | 79 | malloc_count = 0; |
Rohit Grover |
1:4769360130ed | 80 | } |
Rohit Grover |
1:4769360130ed | 81 | |
Rohit Grover |
1:4769360130ed | 82 | int cpputest_malloc_get_count() |
Rohit Grover |
1:4769360130ed | 83 | { |
Rohit Grover |
1:4769360130ed | 84 | return malloc_count; |
Rohit Grover |
1:4769360130ed | 85 | } |
Rohit Grover |
1:4769360130ed | 86 | |
Rohit Grover |
1:4769360130ed | 87 | void cpputest_malloc_set_out_of_memory() |
Rohit Grover |
1:4769360130ed | 88 | { |
Rohit Grover |
1:4769360130ed | 89 | setCurrentMallocAllocator(NullUnknownAllocator::defaultAllocator()); |
Rohit Grover |
1:4769360130ed | 90 | } |
Rohit Grover |
1:4769360130ed | 91 | |
Rohit Grover |
1:4769360130ed | 92 | void cpputest_malloc_set_not_out_of_memory() |
Rohit Grover |
1:4769360130ed | 93 | { |
Rohit Grover |
1:4769360130ed | 94 | malloc_out_of_memory_counter = NO_COUNTDOWN; |
Rohit Grover |
1:4769360130ed | 95 | setCurrentMallocAllocatorToDefault(); |
Rohit Grover |
1:4769360130ed | 96 | } |
Rohit Grover |
1:4769360130ed | 97 | |
Rohit Grover |
1:4769360130ed | 98 | void cpputest_malloc_set_out_of_memory_countdown(int count) |
Rohit Grover |
1:4769360130ed | 99 | { |
Rohit Grover |
1:4769360130ed | 100 | malloc_out_of_memory_counter = count; |
Rohit Grover |
1:4769360130ed | 101 | if (malloc_out_of_memory_counter == OUT_OF_MEMORRY) |
Rohit Grover |
1:4769360130ed | 102 | cpputest_malloc_set_out_of_memory(); |
Rohit Grover |
1:4769360130ed | 103 | } |
Rohit Grover |
1:4769360130ed | 104 | |
Rohit Grover |
1:4769360130ed | 105 | void* cpputest_malloc(size_t size) |
Rohit Grover |
1:4769360130ed | 106 | { |
Rohit Grover |
1:4769360130ed | 107 | return cpputest_malloc_location(size, "<unknown>", 0); |
Rohit Grover |
1:4769360130ed | 108 | } |
Rohit Grover |
1:4769360130ed | 109 | |
Rohit Grover |
1:4769360130ed | 110 | void* cpputest_calloc(size_t num, size_t size) |
Rohit Grover |
1:4769360130ed | 111 | { |
Rohit Grover |
1:4769360130ed | 112 | return cpputest_calloc_location(num, size, "<unknown>", 0); |
Rohit Grover |
1:4769360130ed | 113 | } |
Rohit Grover |
1:4769360130ed | 114 | |
Rohit Grover |
1:4769360130ed | 115 | void* cpputest_realloc(void* ptr, size_t size) |
Rohit Grover |
1:4769360130ed | 116 | { |
Rohit Grover |
1:4769360130ed | 117 | return cpputest_realloc_location(ptr, size, "<unknown>", 0); |
Rohit Grover |
1:4769360130ed | 118 | } |
Rohit Grover |
1:4769360130ed | 119 | |
Rohit Grover |
1:4769360130ed | 120 | void cpputest_free(void* buffer) |
Rohit Grover |
1:4769360130ed | 121 | { |
Rohit Grover |
1:4769360130ed | 122 | cpputest_free_location(buffer, "<unknown>", 0); |
Rohit Grover |
1:4769360130ed | 123 | } |
Rohit Grover |
1:4769360130ed | 124 | |
Rohit Grover |
1:4769360130ed | 125 | static void countdown() |
Rohit Grover |
1:4769360130ed | 126 | { |
Rohit Grover |
1:4769360130ed | 127 | if (malloc_out_of_memory_counter <= NO_COUNTDOWN) |
Rohit Grover |
1:4769360130ed | 128 | return; |
Rohit Grover |
1:4769360130ed | 129 | |
Rohit Grover |
1:4769360130ed | 130 | if (malloc_out_of_memory_counter == OUT_OF_MEMORRY) |
Rohit Grover |
1:4769360130ed | 131 | return; |
Rohit Grover |
1:4769360130ed | 132 | |
Rohit Grover |
1:4769360130ed | 133 | malloc_out_of_memory_counter--; |
Rohit Grover |
1:4769360130ed | 134 | |
Rohit Grover |
1:4769360130ed | 135 | if (malloc_out_of_memory_counter == OUT_OF_MEMORRY) |
Rohit Grover |
1:4769360130ed | 136 | cpputest_malloc_set_out_of_memory(); |
Rohit Grover |
1:4769360130ed | 137 | } |
Rohit Grover |
1:4769360130ed | 138 | |
Rohit Grover |
1:4769360130ed | 139 | void* cpputest_malloc_location(size_t size, const char* file, int line) |
Rohit Grover |
1:4769360130ed | 140 | { |
Rohit Grover |
1:4769360130ed | 141 | countdown(); |
Rohit Grover |
1:4769360130ed | 142 | malloc_count++; |
Rohit Grover |
1:4769360130ed | 143 | return cpputest_malloc_location_with_leak_detection(size, file, line); |
Rohit Grover |
1:4769360130ed | 144 | } |
Rohit Grover |
1:4769360130ed | 145 | |
Rohit Grover |
1:4769360130ed | 146 | void* cpputest_calloc_location(size_t num, size_t size, const char* file, int line) |
Rohit Grover |
1:4769360130ed | 147 | { |
Rohit Grover |
1:4769360130ed | 148 | void* mem = cpputest_malloc_location(num * size, file, line); |
rgrover1 | 0:0b799af9d58e | 149 | if (mem) |
Rohit Grover |
1:4769360130ed | 150 | PlatformSpecificMemset(mem, 0, num*size); |
Rohit Grover |
1:4769360130ed | 151 | return mem; |
Rohit Grover |
1:4769360130ed | 152 | } |
Rohit Grover |
1:4769360130ed | 153 | |
Rohit Grover |
1:4769360130ed | 154 | void* cpputest_realloc_location(void* memory, size_t size, const char* file, int line) |
Rohit Grover |
1:4769360130ed | 155 | { |
Rohit Grover |
1:4769360130ed | 156 | return cpputest_realloc_location_with_leak_detection(memory, size, file, line); |
Rohit Grover |
1:4769360130ed | 157 | } |
Rohit Grover |
1:4769360130ed | 158 | |
Rohit Grover |
1:4769360130ed | 159 | void cpputest_free_location(void* buffer, const char* file, int line) |
Rohit Grover |
1:4769360130ed | 160 | { |
Rohit Grover |
1:4769360130ed | 161 | cpputest_free_location_with_leak_detection(buffer, file, line); |
Rohit Grover |
1:4769360130ed | 162 | } |
Rohit Grover |
1:4769360130ed | 163 | |
Rohit Grover |
1:4769360130ed | 164 | } |