Port of schuchert.wikispaces.com


cpptraining.GettingCppUTestRunning

cpptraining.GettingCppUTestRunning

Introduction

Now it is time to get a simple example running to verify everything works.

Before Continuing

Make sure you have already installed the CDT and built CppUTest. For the remainder of this discussion, I’ll assume that CppUTest was downloaded and built in the following directory: c:\workspaces\CppUTest2_1


title: cpptraining.SettingUpInitialProject —

Setting up Initial Project

  • Start Eclipse and create a new workspace. I’ll usec:\workspaces\firstexample for my workspace.
  • Close the welcome screen.
  • Create a new C++ project (File:New:C++ Project).
  • Enter a name, I’ll useCppUTestHasItsSmoke
  • UnderExecutable, selectHello World C++ Project
  • ClickFinish.
  • Right-click on your project (CppUTestHasItsSmoke), selectRun As:Local C++ Application
  • Notice the output in the console:
!!!Hello World!!!

title: cpptraining.ConfiguringTheProjectForCppUTest —

Configuring the Project for CppUTest

  • Edit your project’s properties (right-click, properties)
  • SelectC/C++ Build:Settings #include Directories
  • UnderGCC C++ Compiler:Includes enter the include directory of CppUTest.
    • Click the page with the green plus.
    • SelectFile system…
    • Enter or search to the directory. For my install location, the directory is: C:\workspaces\CppUTest2_1\includes.

      CppUTest Library

  • UnderMinGW C++ Linker:Libraries, enter both a library path as well as a library
  • UnderLibraries (-l), click on the page with a green plus
  • Enter the name of the library (minus “lib” and “.lib”):CppUTest

    Library Path

  • UnderLibrary search path (-L), click on the page with a green plus
  • Enter the directory where the library is located. On my machine it isC:\workspaces\CppUTest2_1\lib
  • Click OK

    Update main

    You won’t notice any changes unless you use CppUTest.

  • CppUTest uses the main() to execute its tests. So update the file with main() (CppUTestHasItsSmoke.cpp):
#include <CppUTest/CommandLineTestRunner.h>

int main(int argc, char **argv) {
	return CommandLineTestRunner::RunAllTests(argc, argv);
}
  • You can run your program as a Local C++ Application again:
OK (0 tests, 0 ran, 0 checks, 0 ignored, 0 filtered out, 0 ms)

Alternative main()

If you’d like to see a list of tests and the time each takes to run, you can either:

  • Provide command-line arguments when you run the program in Eclipse
  • Use an updated main to make it happen every time:
#include <CppUTest/CommandLineTestRunner.h>

int main() {
	const char* args[] = { "", "-v" };
	return CommandLineTestRunner::RunAllTests(2, args);
}

Creating A Test

#include <CppUTest/TestHarness.h>

TEST_GROUP(Foo) {
};

TEST(Foo, 1Equals1) {
	LONGS_EQUAL(1, 1);
}
OK (1 tests, 1 ran, 1 checks, 0 ignored, 0 filtered out, 0 ms)


Comments

" Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.