Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing C/C++ source code [duplicate]

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".

like image 467
Yanick Rochon Avatar asked Sep 25 '10 02:09

Yanick Rochon


3 Answers

Boost has an excellent unit test library.

like image 136
Sam Miller Avatar answered Nov 06 '22 03:11

Sam Miller


cppunit is what we use.

like image 32
dicroce Avatar answered Nov 06 '22 04:11

dicroce


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

  • http://code.google.com/p/googletest/
  • http://cpptest.sourceforge.net/

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.

like image 23
Evan Avatar answered Nov 06 '22 02:11

Evan