Reputation: 8343
I am trying to rewrite some legacy C code and would like to have some tests in place before actually starting the rewrite. FOr this I took a look at CppUTest and tried a sample application consisting of a header file chrtostr.h
, an implementation file chrtostr.c
and a test file called test_chrtostr.c
which contents is listed bellow:
#include <CppUTest/CommandLineTestRunner.h>
#include "chrtostr.h"
TEST_GROUP(chrtostr)
{
}
TEST(chrtostr, test_chrtostr)
{
CHECK_EQUAL(chrtostr('n'), "sfsdfds");
}
int main(int ac, char **av)
{
return CommandLineTestRunner::RunAllTests(ac, av);
}
And the corresponding Makefile.am
:
AUTOMAKE_OPTIONS = foreign
CPPUTEST_HOME = ./cpputest
CFLAGS = -g -Wall -I$(CPPUTEST_HOME)/include
LDFLAGS = -L$(CPPUTEST_HOME)/lib -lCppUTest
bin_PROGRAMS = chrtostr test_chrtostr
chrtostr_SOURCES = chrtostr.c chrtostr.h main.c
test_chrtostr_SOURCES = test_chrtostr.c
The issue is that each time I try to run make
I get the following traceback which doesn't actually help me too much: http://pastebin.com/BK9ts3vk
Upvotes: 3
Views: 5643
Reputation: 106
I was just looking at this again. There were a few problems with your code. C++ errors don't always help clear them up.
I added comment before the things i fixed.
#include "CppUTest/TestHarness.h"
//The test file is c++. YOu have to tell it when you are linking to C code
extern "C"
{
#include "chrtostr.h"
}
//A test group needs to have a ';' after it. Under the hood, this macro
//create a base class for the test cases of the same name
TEST_GROUP(Chrtostr)
{
};
//CHECK_EQUAL uses ==. STRCMP_EQUAL actually compares c-strings
TEST(Chrtostr, wrong)
{
STRCMP_EQUAL(chrtostr('n'), "sfsdfds");
}
Upvotes: 1
Reputation: 106
You should probably start by getting one of the demos going. You could see how CppUTest is intended to be used with C. My book, Test-Driven Development for Embedded C, will help you get started too. The first few chapters use a C-Only test harness. Later examples use CppUTest (I'm one of the authors of CppUTest). I also describe the advantages of a C++ test harness for C.
James
p.s. - for more information on CppUTest, look at CppUTest.org
Upvotes: 7
Reputation: 9
Unfortunately, the "HelloWorld" example in CppUTest is undocumented and while the Appendix in "Test Driven Development for Embedded C" lists only 11 condition checks, I am finding that there are a lot more undocumented helper functions (all pretty much undocumented). I wouldn't recommend CppuTest unless you are trying to understand the concepts of TDD.
I would look for more of a commercial product or you are going to pick up a lot of bad TDD habits or get really frustrated and just move on.
Upvotes: 0
Reputation: 206851
That test driver is written in C++. You'll need to compile that as C++, so rename your file to .cpp and make sure g++
is called to drive the compile/link (rather than gcc
).
Upvotes: 4