Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Frameworks for C [closed]

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?

like image 660
maxhawkins Avatar asked Dec 14 '22 05:12

maxhawkins


1 Answers

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.

like image 158
philant Avatar answered Dec 27 '22 07:12

philant