Which C unit test framework?
I have just began a new C project, using gcc (currently 4.5.2). Now, before it grows too big, I want to start unit testing it. After some googling I've realized that there are numerou开发者_如何学Cs of frameworks to choose from. I cannot afford testing them all.
Note that I don't mind using g++ for the unit tests, as long as I can use gcc for the project itself. It won't hurt if it has good VIM integration, though not mandatory.
So, simply, which should I choose and why?
I've played with libcheck, which is pretty good. It's been a while since I last used it, but it does the job, and is just straight C.
I'm personally a fan of the Google C++ Testing Framework. It does require g++ (I don't think there's a way around it), but it handles test fixtures very well and has very useful test output. This is the framework used by Chrome. You also don't have to worry about installing it on your system, since it can be built alongside the rest of your source.
A test file can be as simple as this:
#include "gtest/gtest.h"
#include "random.h"
TEST( TestGetRandomInt, ReturnsARandomNumber ){
ASSERT_EQ( 4, getRandomInt() );
}
精彩评论