You are not logged in.

#1 2013-11-14 17:13:16

jakobcreutzfeldt
Member
Registered: 2011-05-12
Posts: 1,041

[solved] Using external frameworks for unit/system testing C code

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

#2 2013-11-14 19:38:21

dolik.rce
Member
From: Czech republic
Registered: 2011-05-04
Posts: 43

Re: [solved] Using external frameworks for unit/system testing C code

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

#3 2013-11-14 20:07:52

jakobcreutzfeldt
Member
Registered: 2011-05-12
Posts: 1,041

Re: [solved] Using external frameworks for unit/system testing C code

dolik.rce wrote:

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

#4 2013-11-14 20:16:55

kaszak696
Member
Registered: 2009-05-26
Posts: 543

Re: [solved] Using external frameworks for unit/system testing C code

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

#5 2013-11-14 20:28:19

dolik.rce
Member
From: Czech republic
Registered: 2011-05-04
Posts: 43

Re: [solved] Using external frameworks for unit/system testing C code

jakobcreutzfeldt wrote:

It's for C++, though. My program is in C.

Oops, overlooked that... Sorry.

Offline

#6 2013-11-14 20:49:10

SahibBommelig
Member
From: Germany
Registered: 2010-05-28
Posts: 80

Re: [solved] Using external frameworks for unit/system testing C code

fctx is a one header "framework" for C/C++.

(And therefore supposed to be shipped with your project)

Last edited by SahibBommelig (2013-11-14 20:49:53)

Offline

#7 2013-11-14 22:39:37

tom5760
Member
From: Philadelphia, PA, USA
Registered: 2006-02-05
Posts: 283
Website

Re: [solved] Using external frameworks for unit/system testing C code

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

#8 2013-11-15 09:03:46

jakobcreutzfeldt
Member
Registered: 2011-05-12
Posts: 1,041

Re: [solved] Using external frameworks for unit/system testing C code

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

#9 2013-11-15 09:47:56

Cloudef
Member
Registered: 2010-10-12
Posts: 636

Re: [solved] Using external frameworks for unit/system testing C code

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

#10 2013-11-15 11:32:14

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [solved] Using external frameworks for unit/system testing C code

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

#11 2013-11-15 11:40:25

jakobcreutzfeldt
Member
Registered: 2011-05-12
Posts: 1,041

Re: [solved] Using external frameworks for unit/system testing C code

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

#12 2013-11-17 13:58:26

jakobcreutzfeldt
Member
Registered: 2011-05-12
Posts: 1,041

Re: [solved] Using external frameworks for unit/system testing C code

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

Board footer

Powered by FluxBB