Hello all.
TLDR version: I have a rather small library that ought to simplify testing
of C code [pcut]. I just tried to add it to HelenOS and convert some of our
tests to it. Result can be found in my [branch], interesting commits are
[libposix] and [bdsh]. I would be glad to get some feedback about it.
Thanks.
Long version:
Some time ago I was writing a small program in C and need to test that a
computation function works as expected. I admit that I am a bit spoiled by
JUnit for Java where you just add @Test to a method to create a unit test.
I hoped that something similar would be possible for C as well. But I was
not able to find a library/framework that would not require adding a lot of
extra code (e.g. [check]) or some kind of preprocessing (e.g. [aceunit]).
In the end I decided to try to implement something that would not need any
preprocessing (except $CC -E) and where adding new test would not require
changes at multiple places.
The result is PCUT [pcut]. There, writing a test is as simple as
#include <pcut/test.h>
PCUT_INIT
PCUT_TEST(test_name) {
/* Test code. */
}
PCUT_MAIN()
compiling and linking with libpcut.
I decided to bring this library to HelenOS because I think it might be
useful. Today, if you want to add a test of a (library) function to HelenOS
you can either write your own testing application (of course, sometimes it
is the best solution for various reasons) or extend app/tester. Both cases
means that the tests are far from the tested code and you need to do a lot
of setup. You either need to prepare a whole application and take care of
everything by yourself. Or you need to change (at least) 4 files in the
tester and hope that the environment would be okay after previous tests. I
guess this is (one of) the reason(s) why there are tests in libposix that
are commented out and are not run regularly [scanf].
The integration of this PCUT library to HelenOS can be found at Launchpad
[branch]. To build it, I recommend ia32 and do not forget to check the PCUT
option during configuration. The built tests are standalone applications
located in uspace/dist/test, their sources are under test/ together with
the library they ought to check. You can run them separately or run "batch
test/run_all" to run all the available tests. This run_all script not only
runs all the tests but also copies the output to /data/web and you can
access them through your web browser under /test.html. The output from the
test is in [tap] format (extended with standard output of the test).
The library itself is located in uspace/lib/pcut. It is a verbatim copy
from the Git repository and thus it includes also some files that are not
needed - such as Linux specific implementations. Having there only
HelenOS-related stuff would be better but that could be done later.
As an example, I created some tests in [libc], [libposix] and for [bdsh]
(yes, that is also possible as long as the functions are not static). The
tests for libc are rather hackish because of a great preprocessor abuse but
the other two are quite okay IMHO.
To briefly summarize why I think PCUT is a bit better than the current
state:
* each library or application can have separate tests that do not
interfere with each other
* moreover, each test case is spawned as a separate application and thus
the whole "thing" is immune against tests that really go bad (such as
null-pointer dereference)
* adding a test to a library is rather simple and requires minimal effort
(see [bdsh])
I would be glad if you would find some time to go through the examples and
comment on it. If you like it, I would merge this to mainline and convert
some of the existing tests to it (where it makes sense). If you have some
ideas how to improve this we can iterate to some better version. Or if you
think the whole idea is wrong, I would discard it and eliminate all the
bits and bytes of the branch ;-).
Thanks!
- Vojta
[aceunit] http://aceunit.sourceforge.net/manual#d3e35
[bdsh]
http://bazaar.launchpad.net/~vojtech-horky/helenos/pcut-testing/revision/2095
[branch] https://code.launchpad.net/~vojtech-horky/helenos/pcut-testing
[check]
http://check.sourceforge.net/doc/check_html/check_3.html#Creating-a-Suite
[libc]
http://bazaar.launchpad.net/~vojtech-horky/helenos/pcut-testing/revision/2094
[libposix]
http://bazaar.launchpad.net/~vojtech-horky/helenos/pcut-testing/revision/2097
[pcut] https://github.com/vhotspur/pcut
[scanf]
http://trac.helenos.org/browser/mainline/uspace/lib/posix/source/stdio/scanf.c#L1224
[tap] http://testanything.org/
_______________________________________________
HelenOS-devel mailing list
[email protected]
http://lists.modry.cz/listinfo/helenos-devel