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:
Tue Jun 17 15:52:54 2014 +0100
Revision:
1:4769360130ed
Parent:
0:0b799af9d58e
updating to the latest version of cppUtest; tested against nordic's m-kit

Who changed what in which revision?

UserRevisionLine numberNew 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/TestOutput.h"
Rohit Grover 1:4769360130ed 30 #include "CppUTest/PlatformSpecificFunctions.h"
Rohit Grover 1:4769360130ed 31
Rohit Grover 1:4769360130ed 32 TestOutput::WorkingEnvironment TestOutput::workingEnvironment_ = TestOutput::detectEnvironment;
Rohit Grover 1:4769360130ed 33
Rohit Grover 1:4769360130ed 34 void TestOutput::setWorkingEnvironment(TestOutput::WorkingEnvironment workEnvironment)
Rohit Grover 1:4769360130ed 35 {
Rohit Grover 1:4769360130ed 36 workingEnvironment_ = workEnvironment;
Rohit Grover 1:4769360130ed 37 }
Rohit Grover 1:4769360130ed 38
Rohit Grover 1:4769360130ed 39 TestOutput::WorkingEnvironment TestOutput::getWorkingEnvironment()
Rohit Grover 1:4769360130ed 40 {
Rohit Grover 1:4769360130ed 41 if (workingEnvironment_ == TestOutput::detectEnvironment)
Rohit Grover 1:4769360130ed 42 return PlatformSpecificGetWorkingEnvironment();
Rohit Grover 1:4769360130ed 43 return workingEnvironment_;
Rohit Grover 1:4769360130ed 44 }
Rohit Grover 1:4769360130ed 45
Rohit Grover 1:4769360130ed 46
Rohit Grover 1:4769360130ed 47 TestOutput::TestOutput() :
Rohit Grover 1:4769360130ed 48 dotCount_(0), verbose_(false), progressIndication_(".")
Rohit Grover 1:4769360130ed 49 {
Rohit Grover 1:4769360130ed 50 }
Rohit Grover 1:4769360130ed 51
Rohit Grover 1:4769360130ed 52 TestOutput::~TestOutput()
Rohit Grover 1:4769360130ed 53 {
Rohit Grover 1:4769360130ed 54 }
Rohit Grover 1:4769360130ed 55
Rohit Grover 1:4769360130ed 56 void TestOutput::verbose()
Rohit Grover 1:4769360130ed 57 {
Rohit Grover 1:4769360130ed 58 verbose_ = true;
Rohit Grover 1:4769360130ed 59 }
Rohit Grover 1:4769360130ed 60
Rohit Grover 1:4769360130ed 61 void TestOutput::print(const char* str)
Rohit Grover 1:4769360130ed 62 {
Rohit Grover 1:4769360130ed 63 printBuffer(str);
Rohit Grover 1:4769360130ed 64 }
Rohit Grover 1:4769360130ed 65
Rohit Grover 1:4769360130ed 66 void TestOutput::print(long n)
Rohit Grover 1:4769360130ed 67 {
Rohit Grover 1:4769360130ed 68 print(StringFrom(n).asCharString());
Rohit Grover 1:4769360130ed 69 }
Rohit Grover 1:4769360130ed 70
Rohit Grover 1:4769360130ed 71 void TestOutput::printDouble(double d)
Rohit Grover 1:4769360130ed 72 {
Rohit Grover 1:4769360130ed 73 print(StringFrom(d).asCharString());
Rohit Grover 1:4769360130ed 74 }
Rohit Grover 1:4769360130ed 75
Rohit Grover 1:4769360130ed 76 void TestOutput::printHex(long n)
Rohit Grover 1:4769360130ed 77 {
Rohit Grover 1:4769360130ed 78 print(HexStringFrom(n).asCharString());
Rohit Grover 1:4769360130ed 79 }
Rohit Grover 1:4769360130ed 80
Rohit Grover 1:4769360130ed 81 TestOutput& operator<<(TestOutput& p, const char* s)
Rohit Grover 1:4769360130ed 82 {
Rohit Grover 1:4769360130ed 83 p.print(s);
Rohit Grover 1:4769360130ed 84 return p;
Rohit Grover 1:4769360130ed 85 }
Rohit Grover 1:4769360130ed 86
Rohit Grover 1:4769360130ed 87 TestOutput& operator<<(TestOutput& p, long int i)
Rohit Grover 1:4769360130ed 88 {
Rohit Grover 1:4769360130ed 89 p.print(i);
Rohit Grover 1:4769360130ed 90 return p;
Rohit Grover 1:4769360130ed 91 }
Rohit Grover 1:4769360130ed 92
Rohit Grover 1:4769360130ed 93 void TestOutput::printCurrentTestStarted(const UtestShell& test)
Rohit Grover 1:4769360130ed 94 {
Rohit Grover 1:4769360130ed 95 if (verbose_) print(test.getFormattedName().asCharString());
Rohit Grover 1:4769360130ed 96 }
Rohit Grover 1:4769360130ed 97
Rohit Grover 1:4769360130ed 98 void TestOutput::printCurrentTestEnded(const TestResult& res)
Rohit Grover 1:4769360130ed 99 {
Rohit Grover 1:4769360130ed 100 if (verbose_) {
Rohit Grover 1:4769360130ed 101 print(" - ");
Rohit Grover 1:4769360130ed 102 print(res.getCurrentTestTotalExecutionTime());
Rohit Grover 1:4769360130ed 103 print(" ms\n");
Rohit Grover 1:4769360130ed 104 }
Rohit Grover 1:4769360130ed 105 else {
Rohit Grover 1:4769360130ed 106 printProgressIndicator();
Rohit Grover 1:4769360130ed 107 }
Rohit Grover 1:4769360130ed 108 }
Rohit Grover 1:4769360130ed 109
Rohit Grover 1:4769360130ed 110 void TestOutput::printProgressIndicator()
Rohit Grover 1:4769360130ed 111 {
Rohit Grover 1:4769360130ed 112 print(progressIndication_);
Rohit Grover 1:4769360130ed 113 if (++dotCount_ % 50 == 0) print("\n");
Rohit Grover 1:4769360130ed 114 }
Rohit Grover 1:4769360130ed 115
Rohit Grover 1:4769360130ed 116 void TestOutput::setProgressIndicator(const char* indicator)
Rohit Grover 1:4769360130ed 117 {
Rohit Grover 1:4769360130ed 118 progressIndication_ = indicator;
Rohit Grover 1:4769360130ed 119 }
Rohit Grover 1:4769360130ed 120
Rohit Grover 1:4769360130ed 121 void TestOutput::printTestsStarted()
Rohit Grover 1:4769360130ed 122 {
Rohit Grover 1:4769360130ed 123 }
Rohit Grover 1:4769360130ed 124
Rohit Grover 1:4769360130ed 125 void TestOutput::printCurrentGroupStarted(const UtestShell& /*test*/)
Rohit Grover 1:4769360130ed 126 {
Rohit Grover 1:4769360130ed 127 }
Rohit Grover 1:4769360130ed 128
Rohit Grover 1:4769360130ed 129 void TestOutput::printCurrentGroupEnded(const TestResult& /*res*/)
Rohit Grover 1:4769360130ed 130 {
Rohit Grover 1:4769360130ed 131 }
Rohit Grover 1:4769360130ed 132
Rohit Grover 1:4769360130ed 133 void TestOutput::flush()
Rohit Grover 1:4769360130ed 134 {
Rohit Grover 1:4769360130ed 135 }
Rohit Grover 1:4769360130ed 136
Rohit Grover 1:4769360130ed 137 void TestOutput::printTestsEnded(const TestResult& result)
Rohit Grover 1:4769360130ed 138 {
Rohit Grover 1:4769360130ed 139 if (result.getFailureCount() > 0) {
Rohit Grover 1:4769360130ed 140 print("\nErrors (");
Rohit Grover 1:4769360130ed 141 print(result.getFailureCount());
Rohit Grover 1:4769360130ed 142 print(" failures, ");
Rohit Grover 1:4769360130ed 143 }
Rohit Grover 1:4769360130ed 144 else {
Rohit Grover 1:4769360130ed 145 print("\nOK (");
Rohit Grover 1:4769360130ed 146 }
Rohit Grover 1:4769360130ed 147 print(result.getTestCount());
Rohit Grover 1:4769360130ed 148 print(" tests, ");
Rohit Grover 1:4769360130ed 149 print(result.getRunCount());
Rohit Grover 1:4769360130ed 150 print(" ran, ");
Rohit Grover 1:4769360130ed 151 print(result.getCheckCount());
Rohit Grover 1:4769360130ed 152 print(" checks, ");
Rohit Grover 1:4769360130ed 153 print(result.getIgnoredCount());
Rohit Grover 1:4769360130ed 154 print(" ignored, ");
Rohit Grover 1:4769360130ed 155 print(result.getFilteredOutCount());
Rohit Grover 1:4769360130ed 156 print(" filtered out, ");
Rohit Grover 1:4769360130ed 157 print(result.getTotalExecutionTime());
Rohit Grover 1:4769360130ed 158 print(" ms)\n\n");
Rohit Grover 1:4769360130ed 159 }
Rohit Grover 1:4769360130ed 160
Rohit Grover 1:4769360130ed 161 void TestOutput::printTestRun(int number, int total)
Rohit Grover 1:4769360130ed 162 {
Rohit Grover 1:4769360130ed 163 if (total > 1) {
Rohit Grover 1:4769360130ed 164 print("Test run ");
Rohit Grover 1:4769360130ed 165 print(number);
Rohit Grover 1:4769360130ed 166 print(" of ");
Rohit Grover 1:4769360130ed 167 print(total);
Rohit Grover 1:4769360130ed 168 print("\n");
Rohit Grover 1:4769360130ed 169 }
Rohit Grover 1:4769360130ed 170 }
Rohit Grover 1:4769360130ed 171
Rohit Grover 1:4769360130ed 172 void TestOutput::print(const TestFailure& failure)
Rohit Grover 1:4769360130ed 173 {
Rohit Grover 1:4769360130ed 174 if (failure.isOutsideTestFile() || failure.isInHelperFunction())
Rohit Grover 1:4769360130ed 175 printFileAndLineForTestAndFailure(failure);
Rohit Grover 1:4769360130ed 176 else
Rohit Grover 1:4769360130ed 177 printFileAndLineForFailure(failure);
Rohit Grover 1:4769360130ed 178
Rohit Grover 1:4769360130ed 179 printFailureMessage(failure.getMessage());
Rohit Grover 1:4769360130ed 180 }
Rohit Grover 1:4769360130ed 181
Rohit Grover 1:4769360130ed 182 void TestOutput::printFileAndLineForTestAndFailure(const TestFailure& failure)
Rohit Grover 1:4769360130ed 183 {
Rohit Grover 1:4769360130ed 184 printErrorInFileOnLineFormattedForWorkingEnvironment(failure.getTestFileName(), failure.getTestLineNumber());
Rohit Grover 1:4769360130ed 185 printFailureInTest(failure.getTestName());
Rohit Grover 1:4769360130ed 186 printErrorInFileOnLineFormattedForWorkingEnvironment(failure.getFileName(), failure.getFailureLineNumber());
Rohit Grover 1:4769360130ed 187 }
Rohit Grover 1:4769360130ed 188
Rohit Grover 1:4769360130ed 189 void TestOutput::printFileAndLineForFailure(const TestFailure& failure)
Rohit Grover 1:4769360130ed 190 {
Rohit Grover 1:4769360130ed 191 printErrorInFileOnLineFormattedForWorkingEnvironment(failure.getFileName(), failure.getFailureLineNumber());
Rohit Grover 1:4769360130ed 192 printFailureInTest(failure.getTestName());
Rohit Grover 1:4769360130ed 193 }
Rohit Grover 1:4769360130ed 194
Rohit Grover 1:4769360130ed 195 void TestOutput::printFailureInTest(SimpleString testName)
Rohit Grover 1:4769360130ed 196 {
Rohit Grover 1:4769360130ed 197 print(" Failure in ");
Rohit Grover 1:4769360130ed 198 print(testName.asCharString());
Rohit Grover 1:4769360130ed 199 }
Rohit Grover 1:4769360130ed 200
Rohit Grover 1:4769360130ed 201 void TestOutput::printFailureMessage(SimpleString reason)
Rohit Grover 1:4769360130ed 202 {
Rohit Grover 1:4769360130ed 203 print("\n");
Rohit Grover 1:4769360130ed 204 print("\t");
Rohit Grover 1:4769360130ed 205 print(reason.asCharString());
Rohit Grover 1:4769360130ed 206 print("\n\n");
Rohit Grover 1:4769360130ed 207 }
Rohit Grover 1:4769360130ed 208
Rohit Grover 1:4769360130ed 209 void TestOutput::printErrorInFileOnLineFormattedForWorkingEnvironment(SimpleString file, int lineNumber)
Rohit Grover 1:4769360130ed 210 {
Rohit Grover 1:4769360130ed 211 if (TestOutput::getWorkingEnvironment() == TestOutput::vistualStudio)
Rohit Grover 1:4769360130ed 212 printVistualStudioErrorInFileOnLine(file, lineNumber);
Rohit Grover 1:4769360130ed 213 else
Rohit Grover 1:4769360130ed 214 printEclipseErrorInFileOnLine(file, lineNumber);
Rohit Grover 1:4769360130ed 215 }
Rohit Grover 1:4769360130ed 216
Rohit Grover 1:4769360130ed 217 void TestOutput::printEclipseErrorInFileOnLine(SimpleString file, int lineNumber)
Rohit Grover 1:4769360130ed 218 {
Rohit Grover 1:4769360130ed 219 print("\n");
Rohit Grover 1:4769360130ed 220 print(file.asCharString());
Rohit Grover 1:4769360130ed 221 print(":");
Rohit Grover 1:4769360130ed 222 print(lineNumber);
Rohit Grover 1:4769360130ed 223 print(":");
Rohit Grover 1:4769360130ed 224 print(" error:");
Rohit Grover 1:4769360130ed 225 }
Rohit Grover 1:4769360130ed 226
Rohit Grover 1:4769360130ed 227 void TestOutput::printVistualStudioErrorInFileOnLine(SimpleString file, int lineNumber)
Rohit Grover 1:4769360130ed 228 {
Rohit Grover 1:4769360130ed 229 print("\n");
Rohit Grover 1:4769360130ed 230 print(file.asCharString());
Rohit Grover 1:4769360130ed 231 print("(");
Rohit Grover 1:4769360130ed 232 print(lineNumber);
Rohit Grover 1:4769360130ed 233 print("):");
Rohit Grover 1:4769360130ed 234 print(" error:");
Rohit Grover 1:4769360130ed 235 }
Rohit Grover 1:4769360130ed 236
Rohit Grover 1:4769360130ed 237 void ConsoleTestOutput::printBuffer(const char* s)
Rohit Grover 1:4769360130ed 238 {
Rohit Grover 1:4769360130ed 239 while (*s) {
Rohit Grover 1:4769360130ed 240 PlatformSpecificPutchar(*s);
Rohit Grover 1:4769360130ed 241 s++;
Rohit Grover 1:4769360130ed 242 }
Rohit Grover 1:4769360130ed 243 flush();
Rohit Grover 1:4769360130ed 244 }
Rohit Grover 1:4769360130ed 245
Rohit Grover 1:4769360130ed 246 void ConsoleTestOutput::flush()
Rohit Grover 1:4769360130ed 247 {
Rohit Grover 1:4769360130ed 248 PlatformSpecificFlush();
Rohit Grover 1:4769360130ed 249 }
Rohit Grover 1:4769360130ed 250
Rohit Grover 1:4769360130ed 251 StringBufferTestOutput::~StringBufferTestOutput()
Rohit Grover 1:4769360130ed 252 {
Rohit Grover 1:4769360130ed 253 }