http://qs1969.pair.com?node_id=641507


in reply to Unit testing of C source code

Perl's testing framework doesn't actually require in any way that the thing being tested is perl. There's a reasonably well-defined protocol that governs what the test outputs, all you need is some code to output that, and it doesn't matter the code that outputs it is perl, C, python, or brainf*ck. There's a fairly good article on the Test Anything Protocol on WP, or just run any existing test with perl -Mblib t/foo.pl and observe what it outputs.


Warning: Do not use without understanding. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Replies are listed 'Best First'.
Re^2: Unit testing of C source code
by lima1 (Curate) on Sep 28, 2007 at 13:17 UTC
    Yeah, but it is also quite easy to implement a Test::More like testsuite in C with libtab (http://jc.ngo.org.uk/trac-bin/trac.cgi/wiki/LibTap). This library is pretty small, so you can ship it with your program. I just added a 't' directory in my autoconf based project and put the libtab header and sources there. Tests look pretty much the same as in Perl:
    # t/simple_tests.c #include <stdio.h> #include "tap.h" #include "something.h" int main(void) { plan_tests(1); /* * A simple example of a test. */ ok(1 + 1 == 2, "Basic arithmetic works"); return exit_status(); /* Return the correct exit code */ }
    A simple t/Makefile.am could look like this:
    # binaries check_PROGRAMS = simple_tests simple_tests_SOURCES = simple_tests.c tap.c ../src/something.c\ tap.h ../src/something.h simple_tests_CFLAGS = -Wall --pedantic -std=c99 -I ../src TESTS = simple_tests
    Then add 't' in your main Makefile.am:
    SUBDIRS = src t
    Then you can run the tests with 'make check'.
Re^2: Unit testing of C source code
by perlfan (Vicar) on Sep 28, 2007 at 14:30 UTC
    After learning the wonders of creating a CPAN module package, I'd highly consider using it to manage the building and testing of even non-Perl projects. I have not had to do this yet, but when I do it will be the first thing I try for sure.