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/CommandLineArguments.h"
Rohit Grover 1:4769360130ed 30 #include "CppUTest/PlatformSpecificFunctions.h"
Rohit Grover 1:4769360130ed 31
Rohit Grover 1:4769360130ed 32 CommandLineArguments::CommandLineArguments(int ac, const char** av) :
Rohit Grover 1:4769360130ed 33 ac_(ac), av_(av), verbose_(false), runTestsAsSeperateProcess_(false), repeat_(1), groupFilter_(""), nameFilter_(""), outputType_(OUTPUT_ECLIPSE), packageName_("")
Rohit Grover 1:4769360130ed 34 {
Rohit Grover 1:4769360130ed 35 }
Rohit Grover 1:4769360130ed 36
Rohit Grover 1:4769360130ed 37 CommandLineArguments::~CommandLineArguments()
Rohit Grover 1:4769360130ed 38 {
Rohit Grover 1:4769360130ed 39 }
Rohit Grover 1:4769360130ed 40
Rohit Grover 1:4769360130ed 41 bool CommandLineArguments::parse(TestPlugin* plugin)
Rohit Grover 1:4769360130ed 42 {
Rohit Grover 1:4769360130ed 43 bool correctParameters = true;
Rohit Grover 1:4769360130ed 44 for (int i = 1; i < ac_; i++) {
Rohit Grover 1:4769360130ed 45 SimpleString argument = av_[i];
Rohit Grover 1:4769360130ed 46 if (argument == "-v") verbose_ = true;
Rohit Grover 1:4769360130ed 47 else if (argument == "-p") runTestsAsSeperateProcess_ = true;
Rohit Grover 1:4769360130ed 48 else if (argument.startsWith("-r")) SetRepeatCount(ac_, av_, i);
Rohit Grover 1:4769360130ed 49 else if (argument.startsWith("-g")) SetGroupFilter(ac_, av_, i);
Rohit Grover 1:4769360130ed 50 else if (argument.startsWith("-sg")) SetStrictGroupFilter(ac_, av_, i);
Rohit Grover 1:4769360130ed 51 else if (argument.startsWith("-n")) SetNameFilter(ac_, av_, i);
Rohit Grover 1:4769360130ed 52 else if (argument.startsWith("-sn")) SetStrictNameFilter(ac_, av_, i);
Rohit Grover 1:4769360130ed 53 else if (argument.startsWith("TEST(")) SetTestToRunBasedOnVerboseOutput(ac_, av_, i, "TEST(");
Rohit Grover 1:4769360130ed 54 else if (argument.startsWith("IGNORE_TEST(")) SetTestToRunBasedOnVerboseOutput(ac_, av_, i, "IGNORE_TEST(");
Rohit Grover 1:4769360130ed 55 else if (argument.startsWith("-o")) correctParameters = SetOutputType(ac_, av_, i);
Rohit Grover 1:4769360130ed 56 else if (argument.startsWith("-p")) correctParameters = plugin->parseAllArguments(ac_, av_, i);
Rohit Grover 1:4769360130ed 57 else if (argument.startsWith("-k")) SetPackageName(ac_, av_, i);
Rohit Grover 1:4769360130ed 58 else correctParameters = false;
Rohit Grover 1:4769360130ed 59
Rohit Grover 1:4769360130ed 60 if (correctParameters == false) {
Rohit Grover 1:4769360130ed 61 return false;
Rohit Grover 1:4769360130ed 62 }
Rohit Grover 1:4769360130ed 63 }
Rohit Grover 1:4769360130ed 64 return true;
Rohit Grover 1:4769360130ed 65 }
Rohit Grover 1:4769360130ed 66
Rohit Grover 1:4769360130ed 67 const char* CommandLineArguments::usage() const
Rohit Grover 1:4769360130ed 68 {
Rohit Grover 1:4769360130ed 69 return "usage [-v] [-r#] [-g|sg groupName] [-n|sn testName] [-o{normal, junit}] [-k packageName]\n";
Rohit Grover 1:4769360130ed 70 }
Rohit Grover 1:4769360130ed 71
Rohit Grover 1:4769360130ed 72 bool CommandLineArguments::isVerbose() const
Rohit Grover 1:4769360130ed 73 {
Rohit Grover 1:4769360130ed 74 return verbose_;
Rohit Grover 1:4769360130ed 75 }
Rohit Grover 1:4769360130ed 76
Rohit Grover 1:4769360130ed 77 bool CommandLineArguments::runTestsInSeperateProcess() const
Rohit Grover 1:4769360130ed 78 {
Rohit Grover 1:4769360130ed 79 return runTestsAsSeperateProcess_;
Rohit Grover 1:4769360130ed 80 }
Rohit Grover 1:4769360130ed 81
Rohit Grover 1:4769360130ed 82
Rohit Grover 1:4769360130ed 83 int CommandLineArguments::getRepeatCount() const
Rohit Grover 1:4769360130ed 84 {
Rohit Grover 1:4769360130ed 85 return repeat_;
Rohit Grover 1:4769360130ed 86 }
Rohit Grover 1:4769360130ed 87
Rohit Grover 1:4769360130ed 88 TestFilter CommandLineArguments::getGroupFilter() const
Rohit Grover 1:4769360130ed 89 {
Rohit Grover 1:4769360130ed 90 return groupFilter_;
Rohit Grover 1:4769360130ed 91 }
Rohit Grover 1:4769360130ed 92
Rohit Grover 1:4769360130ed 93 TestFilter CommandLineArguments::getNameFilter() const
Rohit Grover 1:4769360130ed 94 {
Rohit Grover 1:4769360130ed 95 return nameFilter_;
Rohit Grover 1:4769360130ed 96 }
Rohit Grover 1:4769360130ed 97
Rohit Grover 1:4769360130ed 98 void CommandLineArguments::SetRepeatCount(int ac, const char** av, int& i)
Rohit Grover 1:4769360130ed 99 {
Rohit Grover 1:4769360130ed 100 repeat_ = 0;
Rohit Grover 1:4769360130ed 101
Rohit Grover 1:4769360130ed 102 SimpleString repeatParameter(av[i]);
Rohit Grover 1:4769360130ed 103 if (repeatParameter.size() > 2) repeat_ = PlatformSpecificAtoI(av[i] + 2);
Rohit Grover 1:4769360130ed 104 else if (i + 1 < ac) {
Rohit Grover 1:4769360130ed 105 repeat_ = PlatformSpecificAtoI(av[i + 1]);
Rohit Grover 1:4769360130ed 106 if (repeat_ != 0) i++;
Rohit Grover 1:4769360130ed 107 }
Rohit Grover 1:4769360130ed 108
Rohit Grover 1:4769360130ed 109 if (0 == repeat_) repeat_ = 2;
Rohit Grover 1:4769360130ed 110
Rohit Grover 1:4769360130ed 111 }
Rohit Grover 1:4769360130ed 112
Rohit Grover 1:4769360130ed 113 SimpleString CommandLineArguments::getParameterField(int ac, const char** av, int& i, const SimpleString& parameterName)
Rohit Grover 1:4769360130ed 114 {
Rohit Grover 1:4769360130ed 115 size_t parameterLength = parameterName.size();
Rohit Grover 1:4769360130ed 116 SimpleString parameter(av[i]);
Rohit Grover 1:4769360130ed 117 if (parameter.size() > parameterLength) return av[i] + parameterLength;
Rohit Grover 1:4769360130ed 118 else if (i + 1 < ac) return av[++i];
Rohit Grover 1:4769360130ed 119 return "";
Rohit Grover 1:4769360130ed 120 }
Rohit Grover 1:4769360130ed 121
Rohit Grover 1:4769360130ed 122 void CommandLineArguments::SetGroupFilter(int ac, const char** av, int& i)
Rohit Grover 1:4769360130ed 123 {
Rohit Grover 1:4769360130ed 124 groupFilter_ = TestFilter(getParameterField(ac, av, i, "-g"));
Rohit Grover 1:4769360130ed 125 }
Rohit Grover 1:4769360130ed 126
Rohit Grover 1:4769360130ed 127 void CommandLineArguments::SetStrictGroupFilter(int ac, const char** av, int& i)
Rohit Grover 1:4769360130ed 128 {
Rohit Grover 1:4769360130ed 129 groupFilter_ = TestFilter(getParameterField(ac, av, i, "-sg"));
Rohit Grover 1:4769360130ed 130 groupFilter_.strictMatching();
Rohit Grover 1:4769360130ed 131 }
Rohit Grover 1:4769360130ed 132
Rohit Grover 1:4769360130ed 133 void CommandLineArguments::SetNameFilter(int ac, const char** av, int& i)
Rohit Grover 1:4769360130ed 134 {
Rohit Grover 1:4769360130ed 135 nameFilter_ = getParameterField(ac, av, i, "-n");
Rohit Grover 1:4769360130ed 136 }
Rohit Grover 1:4769360130ed 137
Rohit Grover 1:4769360130ed 138 void CommandLineArguments::SetStrictNameFilter(int ac, const char** av, int& index)
Rohit Grover 1:4769360130ed 139 {
Rohit Grover 1:4769360130ed 140 nameFilter_ = getParameterField(ac, av, index, "-sn");
Rohit Grover 1:4769360130ed 141 nameFilter_.strictMatching();
Rohit Grover 1:4769360130ed 142 }
Rohit Grover 1:4769360130ed 143
Rohit Grover 1:4769360130ed 144 void CommandLineArguments::SetTestToRunBasedOnVerboseOutput(int ac, const char** av, int& index, const char* parameterName)
Rohit Grover 1:4769360130ed 145 {
Rohit Grover 1:4769360130ed 146 SimpleString wholename = getParameterField(ac, av, index, parameterName);
Rohit Grover 1:4769360130ed 147 SimpleString testname = wholename.subStringFromTill(',', ')');
Rohit Grover 1:4769360130ed 148 testname = testname.subString(2, testname.size());
Rohit Grover 1:4769360130ed 149 groupFilter_ = wholename.subStringFromTill(wholename.at(0), ',');
Rohit Grover 1:4769360130ed 150 nameFilter_ = testname;
Rohit Grover 1:4769360130ed 151 nameFilter_.strictMatching();
Rohit Grover 1:4769360130ed 152 groupFilter_.strictMatching();
Rohit Grover 1:4769360130ed 153 }
Rohit Grover 1:4769360130ed 154
Rohit Grover 1:4769360130ed 155 void CommandLineArguments::SetPackageName(int ac, const char** av, int& i)
Rohit Grover 1:4769360130ed 156 {
Rohit Grover 1:4769360130ed 157 SimpleString packageName = getParameterField(ac, av, i, "-k");
Rohit Grover 1:4769360130ed 158 if (packageName.size() == 0) return;
Rohit Grover 1:4769360130ed 159
Rohit Grover 1:4769360130ed 160 packageName_ = packageName;
Rohit Grover 1:4769360130ed 161 }
Rohit Grover 1:4769360130ed 162
Rohit Grover 1:4769360130ed 163 bool CommandLineArguments::SetOutputType(int ac, const char** av, int& i)
Rohit Grover 1:4769360130ed 164 {
Rohit Grover 1:4769360130ed 165 SimpleString outputType = getParameterField(ac, av, i, "-o");
Rohit Grover 1:4769360130ed 166 if (outputType.size() == 0) return false;
Rohit Grover 1:4769360130ed 167
Rohit Grover 1:4769360130ed 168 if (outputType == "normal" || outputType == "eclipse") {
Rohit Grover 1:4769360130ed 169 outputType_ = OUTPUT_ECLIPSE;
Rohit Grover 1:4769360130ed 170 return true;
Rohit Grover 1:4769360130ed 171 }
Rohit Grover 1:4769360130ed 172 if (outputType == "junit") {
Rohit Grover 1:4769360130ed 173 outputType_ = OUTPUT_JUNIT;
Rohit Grover 1:4769360130ed 174 return true;
Rohit Grover 1:4769360130ed 175 }
Rohit Grover 1:4769360130ed 176 return false;
Rohit Grover 1:4769360130ed 177 }
Rohit Grover 1:4769360130ed 178
Rohit Grover 1:4769360130ed 179 bool CommandLineArguments::isEclipseOutput() const
Rohit Grover 1:4769360130ed 180 {
Rohit Grover 1:4769360130ed 181 return outputType_ == OUTPUT_ECLIPSE;
Rohit Grover 1:4769360130ed 182 }
Rohit Grover 1:4769360130ed 183
Rohit Grover 1:4769360130ed 184 bool CommandLineArguments::isJUnitOutput() const
Rohit Grover 1:4769360130ed 185 {
Rohit Grover 1:4769360130ed 186 return outputType_ == OUTPUT_JUNIT;
Rohit Grover 1:4769360130ed 187 }
Rohit Grover 1:4769360130ed 188
Rohit Grover 1:4769360130ed 189 const SimpleString& CommandLineArguments::getPackageName() const
Rohit Grover 1:4769360130ed 190 {
Rohit Grover 1:4769360130ed 191 return packageName_;
Rohit Grover 1:4769360130ed 192 }
Rohit Grover 1:4769360130ed 193