You are not logged in.
I have a program in C that I should have been writing tests for all along. I'm used to the unittest library in Python, which is built-in, so everyone has it. However, it seems like writing tests in C either requires re-inventing the wheel or using an external system. How unreasonable is it to expect the user to install some testing framework in order to test your program?
For example, from searching around, it seems like check is great for unittesting of the internal functions while DejaGnu is pretty flexible for doing system testing (i.e. testing the output of the program). However, if I depend on these two, the user would have to download the two frameworks plus their dependencies (expect and tcl for DejaGnu) in order to test my program.
What do people usually do in C? Just hand-write all of their tests? It seems like, in the case of check, it handles a lot of important boiler plate regarding process and memory management. In the case of DejaGnu, it conforms to a posix standard for test output. I don't really feel like re-inventing the wheel in either case. I just want to write some test cases.
Any other recommendations would be welcome.
Last edited by jakobcreutzfeldt (2013-11-17 13:58:38)
Offline
Have a look at unit++, it might be what you are after. It is LGPL licensed and small enough to be distributed together with your code...
Offline
Have a look at unit++, it might be what you are after. It is LGPL licensed and small enough to be distributed together with your code...
It's for C++, though. My program is in C.
Offline
Check seems to be preety lightweight.
EDIT Oops i didn't notice you already listed that, sorry.
Last edited by kaszak696 (2013-11-14 20:30:49)
'What can be asserted without evidence can also be dismissed without evidence.' - Christopher Hitchens
'There's no such thing as addiction, there's only things that you enjoy doing more than life.' - Doug Stanhope
GitHub Junkyard
Offline
It's for C++, though. My program is in C.
Oops, overlooked that... Sorry.
Offline
Offline
For C, I recommend cmocka. It's a bit larger than some of the super-tiny testing frameworks out there, but this is a bit more powerful.
Depending on how you structure your code, you can use cmocka to create mock versions of your functions; so you can test in isolation, etc. Check out this article (search for "ld wrapper support"), which shows a way of replacing symbols at link time for testing. I've used it to test my code for malloc failures for example.
Last edited by tom5760 (2013-11-14 22:43:27)
Offline
Cool, thanks for the recommendations. Both seem pretty interesting but now I'm lost as to which one to choose. I guess I have some reading to do!
So, as for my main question, though, I guess that it's generally accepted that testing might require external software (though not in all cases, like fctx)?
Offline
Though, my case probably isn't extreme as yours.
I just recently used CTest and self-written test.c with asserts for each module and it does the job perfectly.
https://github.com/Cloudef/chck/blob/ma … fer/test.c
https://github.com/Cloudef/chck/blob/ma … eLists.txt
Last edited by Cloudef (2013-11-15 09:48:42)
Offline
I suspect the answer to your question is yes, people generally hand-write all their tests (if they do automated testing at all). I don't have any statistics to back that up, though, just an impression. Most of my personal projects in C have been small enough to debug by inspection.
Offline
I think I've decided that in my case, since the programs are rather small and are mostly front-ends to other libraries, unit testing isn't really necessary. And since user interaction with the tools is pretty basic, a simple shell script to do system testing will be sufficient. In the future, though, I may have larger programs, so it's good to know about this in general.
Offline
Just to formally wrap this up: I discovered that Automake has built-in support for the the TAP test protocol and can automatically include a test harness. Since I was already using Automake, it was relatively simple to set this up for some basic system testing. If I implement unit tests, I'll probably use Check and the Automake TAP test harness.
Offline