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......
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......
Well, just replace the J from Java by a C from... C
Althought maybe CUT is more generic.
Finally I might be byased because I really like NetBSD, but you should also give ATF a try
Glib has built-in testing framework: http://library.gnome.org/devel/glib/stable/glib-Testing.html
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.
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"));
}