8

Possible Duplicates:
Suggestion for UnitTest tools for C++
Choosing a C++ unit testing tool/framework
Unit Testing C Code

This is something I've been wondering now that we have to use C/C++ as base language for some of our university projects :

In Java there's JUnit,

In PHP there's PHPUnit

etc.

How are unit testing done in C/C++? This is probably a silly question, but I don't think I ever read exactly how applications (source code) are unit tested--if there's even such a thing in C/C++--other than "check if the code compiles".

Community
  • 1
  • 1
Yanick Rochon
  • 47,885
  • 24
  • 119
  • 191

6 Answers6

4

Boost has an excellent unit test library.

Sam Miller
  • 23,458
  • 4
  • 65
  • 86
1

cppunit is what we use.

dicroce
  • 42,886
  • 27
  • 97
  • 139
1

There are quite a few frameworks but to name a few:

Some people will just role their own using #ifdefs and a single test.c or test.cpp file:

#ifdef TEST_1
int main(int argc, char** argv) { /*test code for 1*/ }
#endif

#ifdef TEST_2
int main(int argc, char** argv) { /*test code for 2*/ }
#endif

At compile time a you can generate the test by defining TEST_x (where x is a number) this generates executables for each test. Maybe not ideal but very simple.

Evan
  • 6,101
  • 1
  • 23
  • 43
1

We use cunit:

http://cunit.sourceforge.net/

It allows one to logically (or functionally) group tests, produce test output in XML format for automated publishing of results, etc.

1

anther one is UnitTest++. Header-only, lightweight and simple operation, yet does everything needed.

stijn
  • 33,119
  • 13
  • 101
  • 149
0

I've used CxxTest and CppUnit and found CxxTest to be easier to use (it automates generating some of the skeleton code - on the downside it needs Python installed) and lightweight (no libraries, entirely header based).

Eugen Constantin Dinca
  • 8,813
  • 2
  • 31
  • 51