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/TestResult.h"
Rohit Grover 1:4769360130ed 30 #include "CppUTest/TestFailure.h"
Rohit Grover 1:4769360130ed 31 #include "CppUTest/TestOutput.h"
Rohit Grover 1:4769360130ed 32 #include "CppUTest/PlatformSpecificFunctions.h"
Rohit Grover 1:4769360130ed 33
Rohit Grover 1:4769360130ed 34 TestResult::TestResult(TestOutput& p) :
Rohit Grover 1:4769360130ed 35 output_(p), testCount_(0), runCount_(0), checkCount_(0), failureCount_(0), filteredOutCount_(0), ignoredCount_(0), totalExecutionTime_(0), timeStarted_(0), currentTestTimeStarted_(0),
Rohit Grover 1:4769360130ed 36 currentTestTotalExecutionTime_(0), currentGroupTimeStarted_(0), currentGroupTotalExecutionTime_(0)
Rohit Grover 1:4769360130ed 37 {
Rohit Grover 1:4769360130ed 38 }
Rohit Grover 1:4769360130ed 39
Rohit Grover 1:4769360130ed 40 void TestResult::setProgressIndicator(const char* indicator)
Rohit Grover 1:4769360130ed 41 {
Rohit Grover 1:4769360130ed 42 output_.setProgressIndicator(indicator);
Rohit Grover 1:4769360130ed 43 }
Rohit Grover 1:4769360130ed 44
Rohit Grover 1:4769360130ed 45 TestResult::~TestResult()
Rohit Grover 1:4769360130ed 46 {
Rohit Grover 1:4769360130ed 47 }
Rohit Grover 1:4769360130ed 48
Rohit Grover 1:4769360130ed 49 void TestResult::currentGroupStarted(UtestShell* test)
Rohit Grover 1:4769360130ed 50 {
Rohit Grover 1:4769360130ed 51 output_.printCurrentGroupStarted(*test);
Rohit Grover 1:4769360130ed 52 currentGroupTimeStarted_ = GetPlatformSpecificTimeInMillis();
Rohit Grover 1:4769360130ed 53 }
Rohit Grover 1:4769360130ed 54
Rohit Grover 1:4769360130ed 55 void TestResult::currentGroupEnded(UtestShell* /*test*/)
Rohit Grover 1:4769360130ed 56 {
Rohit Grover 1:4769360130ed 57 currentGroupTotalExecutionTime_ = GetPlatformSpecificTimeInMillis() - currentGroupTimeStarted_;
Rohit Grover 1:4769360130ed 58 output_.printCurrentGroupEnded(*this);
Rohit Grover 1:4769360130ed 59 }
Rohit Grover 1:4769360130ed 60
Rohit Grover 1:4769360130ed 61 void TestResult::currentTestStarted(UtestShell* test)
Rohit Grover 1:4769360130ed 62 {
Rohit Grover 1:4769360130ed 63 output_.printCurrentTestStarted(*test);
Rohit Grover 1:4769360130ed 64 currentTestTimeStarted_ = GetPlatformSpecificTimeInMillis();
Rohit Grover 1:4769360130ed 65 }
Rohit Grover 1:4769360130ed 66
Rohit Grover 1:4769360130ed 67 void TestResult::print(const char* text)
Rohit Grover 1:4769360130ed 68 {
Rohit Grover 1:4769360130ed 69 output_.print(text);
Rohit Grover 1:4769360130ed 70 }
Rohit Grover 1:4769360130ed 71
Rohit Grover 1:4769360130ed 72 void TestResult::currentTestEnded(UtestShell* /*test*/)
Rohit Grover 1:4769360130ed 73 {
Rohit Grover 1:4769360130ed 74 currentTestTotalExecutionTime_ = GetPlatformSpecificTimeInMillis() - currentTestTimeStarted_;
Rohit Grover 1:4769360130ed 75 output_.printCurrentTestEnded(*this);
Rohit Grover 1:4769360130ed 76
Rohit Grover 1:4769360130ed 77 }
Rohit Grover 1:4769360130ed 78
Rohit Grover 1:4769360130ed 79 void TestResult::addFailure(const TestFailure& failure)
Rohit Grover 1:4769360130ed 80 {
Rohit Grover 1:4769360130ed 81 output_.print(failure);
Rohit Grover 1:4769360130ed 82 failureCount_++;
Rohit Grover 1:4769360130ed 83 }
Rohit Grover 1:4769360130ed 84
Rohit Grover 1:4769360130ed 85 void TestResult::countTest()
Rohit Grover 1:4769360130ed 86 {
Rohit Grover 1:4769360130ed 87 testCount_++;
Rohit Grover 1:4769360130ed 88 }
Rohit Grover 1:4769360130ed 89
Rohit Grover 1:4769360130ed 90 void TestResult::countRun()
Rohit Grover 1:4769360130ed 91 {
Rohit Grover 1:4769360130ed 92 runCount_++;
Rohit Grover 1:4769360130ed 93 }
Rohit Grover 1:4769360130ed 94
Rohit Grover 1:4769360130ed 95 void TestResult::countCheck()
Rohit Grover 1:4769360130ed 96 {
Rohit Grover 1:4769360130ed 97 checkCount_++;
Rohit Grover 1:4769360130ed 98 }
Rohit Grover 1:4769360130ed 99
Rohit Grover 1:4769360130ed 100 void TestResult::countFilteredOut()
Rohit Grover 1:4769360130ed 101 {
Rohit Grover 1:4769360130ed 102 filteredOutCount_++;
Rohit Grover 1:4769360130ed 103 }
Rohit Grover 1:4769360130ed 104
Rohit Grover 1:4769360130ed 105 void TestResult::countIgnored()
Rohit Grover 1:4769360130ed 106 {
Rohit Grover 1:4769360130ed 107 ignoredCount_++;
Rohit Grover 1:4769360130ed 108 }
Rohit Grover 1:4769360130ed 109
Rohit Grover 1:4769360130ed 110 void TestResult::testsStarted()
Rohit Grover 1:4769360130ed 111 {
Rohit Grover 1:4769360130ed 112 timeStarted_ = GetPlatformSpecificTimeInMillis();
Rohit Grover 1:4769360130ed 113 output_.printTestsStarted();
Rohit Grover 1:4769360130ed 114 }
Rohit Grover 1:4769360130ed 115
Rohit Grover 1:4769360130ed 116 void TestResult::testsEnded()
Rohit Grover 1:4769360130ed 117 {
Rohit Grover 1:4769360130ed 118 long timeEnded = GetPlatformSpecificTimeInMillis();
Rohit Grover 1:4769360130ed 119 totalExecutionTime_ = timeEnded - timeStarted_;
Rohit Grover 1:4769360130ed 120 output_.printTestsEnded(*this);
Rohit Grover 1:4769360130ed 121 }
Rohit Grover 1:4769360130ed 122
Rohit Grover 1:4769360130ed 123 long TestResult::getTotalExecutionTime() const
Rohit Grover 1:4769360130ed 124 {
Rohit Grover 1:4769360130ed 125 return totalExecutionTime_;
Rohit Grover 1:4769360130ed 126 }
Rohit Grover 1:4769360130ed 127
Rohit Grover 1:4769360130ed 128 void TestResult::setTotalExecutionTime(long exTime)
Rohit Grover 1:4769360130ed 129 {
Rohit Grover 1:4769360130ed 130 totalExecutionTime_ = exTime;
Rohit Grover 1:4769360130ed 131 }
Rohit Grover 1:4769360130ed 132
Rohit Grover 1:4769360130ed 133 long TestResult::getCurrentTestTotalExecutionTime() const
Rohit Grover 1:4769360130ed 134 {
Rohit Grover 1:4769360130ed 135 return currentTestTotalExecutionTime_;
Rohit Grover 1:4769360130ed 136 }
Rohit Grover 1:4769360130ed 137
Rohit Grover 1:4769360130ed 138 long TestResult::getCurrentGroupTotalExecutionTime() const
Rohit Grover 1:4769360130ed 139 {
Rohit Grover 1:4769360130ed 140 return currentGroupTotalExecutionTime_;
Rohit Grover 1:4769360130ed 141 }
Rohit Grover 1:4769360130ed 142