views:

371

answers:

8

After doing some work with Ruby, Rails, and RSpec last summer and I learned to TATFT. Now I can't write code without writing tests first.

I'm taking a programming course in C next year, and I would like to learn to write C test-driven. Is it a good idea (or even possible) to do TDD with C? If so, are there any good testing frameworks compatible with C?

+1  A: 

There are a number of unit testing harnesses for C. Wikipedia has a much better list than I could assemble here.

MattK
+2  A: 

We use "check" from http://check.sourceforge.net/, it provides basic functionality for testsuites and tests (similiar to junit), but is fairly lightweight. On feature I like is that it handles if your test dumps code and considers that a failure.

Also note "check" is a "C" based framework rather than a "C++" one.

Aaron
+1  A: 

If you are actually using a C++ compiler, but using it in 'C' mode by compiling .c files, then, also, any of the C++ unit test frameworks will work OK.

Take a look at the original list of xUnit frameworks at http://www.xprogramming.com/software.htm

quamrana
A: 

So a proper C programmer will tell you that because C is statically typed it catches all bugs you might have and therefore you don't need a unit test framework.

They are full of shit, but that's the argument for statically type languages like C.

I think you should probably take the approach that Adobe did with Photoshop. Write a series of core libraries in C, and then all the glue and real logic of the application should be in a higher level language. Photoshop is mostly written in Lua, but many languages work for this.

rabble
Sorry, java is statically typed, and I've never heard the argument that you shouldn't unit test your java code. I agree that anyone saying that is full of it, but I just haven't heard that argument with regard to static/dynamic typing and I think it confuses the issue.
Justin Standard
I don't know which C programmers you work with who claim that, but they ain't proper C programmers by my standards. It is trivial to have bugs in C that are unrelated to type (as in every language) and unit testing helps no end in finding and eradicating them.
Jonathan Leffler
+2  A: 

I just discovered CSpec, which does BDD in C. Doesn't look very mature, but it reminds me of RSpec.

maxhawkins
+4  A: 

Is it a good idea (or even possible) to do TDD with C?

Yes, it obviously is a good idea, as with other languages. However, due to the procedural nature of the language, it comes with its some more difficulties.

  • Static functions quickly get in the way. This can be solved by including the source file under test, or defining a STATIC macro that only means static when compiling the code for production - not unit test

    #if defined(UNIT_TEST)
    #define STATIC
    #else
    #define STATIC static
    #endif
    
  • There is no isolation: there is only one global context. With an OO language you can just instantiate one object (or a cluster of collaborating objects) to test it (them), you can also use mock objects. With C, you can, however, override functions just by re-defining them in your unit tests. This works fine on Unix-like systems where the linker invokes the first function he is finding - I'm not sure on Windows.


If so, are there any good testing frameworks compatible with C?

You can start small with minunit. The learning curve is flat as it only is four macros long.

EDIT: There are two lists of UT frameworks for the C language, that were mentioned in other answers and I didn't repeat : one on Wikepedia and another one on xprogramming.com.

philippe
+1  A: 

This similar question also has a lot of answers "Unit Testing C Code"

I used RCUNIT, it is mature and has everything I need. I have also used ipl canata which is great but is very expensive so that is probability not what you want.

Gerhard
A: 

You certainly can do unit testing in C (I do). The framework I use (for the Windows platform) is CunitWin32

Dushara