tags:

views:

119

answers:

5

Is there any unit testing framework for C like JUnit and Nunit for java and .NET? Or how do we test a piece of code written in C for different scenarios?

Thanks in advance......

A: 

Well, just replace the J from Java by a C from... C

Cunit

Althought maybe CUT is more generic.

Finally I might be byased because I really like NetBSD, but you should also give ATF a try

Francisco Garcia
+1  A: 

Glib has built-in testing framework: http://library.gnome.org/devel/glib/stable/glib-Testing.html

el.pescado
+1  A: 

I'm still new to unit testing frameworks, but I've recently tried cut, check, and cunit. This seemed to counter some others' experiences (see http://stackoverflow.com/questions/65820/unit-testing-c-code for a previous question), but I found cunit the easiest to get going. This also seems a good choice for me, as cunit is supposed to align well with the other xunit frameworks and I switch languages somewhat frequently.

GreenMatt
+1  A: 

I was very happy with CuTest the last time I needed to unit test C. It's only one .c/.h pair, comes with a small shell script which automatically finds all the tests to build the test suite and the assertion errors aren't entirely unhelpful.

Here is an example of one of my tests:

void TestBadPaths(CuTest *tc) {
    // Directory doesn't exist
    char *path = (char *)"/foo/bar";
    CuAssertPtrEquals(tc, NULL, searchpath(path, "sh"));

    // A binary which isn't found
    path = (char *)"/bin";
    CuAssertPtrEquals(tc, NULL, searchpath(path, "foobar"));
}   
David Wolever
+2  A: 

I have worked some with Check and its very easy to setup. Its used by some big active projects like GStreamer. Here is a simple example of fail if line:

fail_if (0 == get_element_position(spot), "Position should not be 0");
tilljoel