tags:

views:

75

answers:

2

I am maintaining a server application written in C many years ago. It has very few unit tests (cxxtest) and I am thinking about developing more.

I read on the subject of unit testing C programs in the last days and tried a few things also. The right thing to do appears to be to include the .c file containing the function to test from my test suite. However, because that .c file contains many functions calling many others, there are lots dependencies I have to deal with before I can start the real work.

What I would like to do is to "turn off" all functions I'm not interested with for a particular test. I would stub those called by my function under test. I am considering putting ifdef around my functions if it's what it takes.

Is there a better solution or a framework addressing this problem?

+2  A: 

A prerequisite to writing solid unit tests is the ability to test code in isolation. i.e., invoke this method without involving any other logic. It sounds like you may need to do some refactoring to remove the mentioned dependencies before being able to accomplish this.

As far as a unit testing framework for C take a look here for a similar discussion: http://stackoverflow.com/questions/65820/unit-testing-c-code

tQuarella
I have func_to_test and other_func in the same unit. Having other_func around when I need to test func_to_test adds the number of dependencies I have to deal with: other_func calls fnA, fnB, etc. If I turn off other_func off, the problem goes away. Refactoring at this stage is not the solution.
Philippe A.
Refactoring can be the solution: split the file in two to isolate func_to_test from other_func.
dolmen
+1  A: 

Cmock solves most of my problem. I gave up on the idea of isolating a single function. I will be fine if I can isolate one C file from another. Cmock allows me to do that quite conveniently (and even more).

Philippe A.
Cmock: http://sourceforge.net/apps/trac/cmock/wiki
dolmen