Every module has its own unit test. This test is contained in the same file as module main implementation file. It is enclosed in between a conditional preprocessor directive so that for a release version of the software it could be removed by undefining a simple preprocessor value.
The unit test should at least test for correct initialization and freeing of resources. Then it should have a sub test for every exported (external visible) function. If a set of states is tested so that every code path is executed at least once then is considered enough.
Unittest conventions | Every module has its own unit test. |
Structure | The header of a module contains a single test section which contains one or more unittest declarations of the form »int unittest_NAME_OF_TEST(void);«. |
The header of a module contains a single test section which contains one or more unittest declarations of the form »int unittest_NAME_OF_TEST(void);«. See the following example
// group: test #ifdef KONFIG_UNITTEST /* function: unittest_platform_X11_x11window * Tests opening a window on local display. */ int unittest_platform_X11_x11window(void); #endif
The implementation file must include the test macro TEST defined in file »C-kern/api/test/unittest.h«.
It has a single parameter which is the condition to test for.
For the abort label »ONABORT« is used. The unit test returns 0 in case of success or an error code (> 0).
// section with #include "*.h" #ifdef KONFIG_UNITTEST #include "C-kern/api/test/unittest.h" #endif // ... module implementation ... #ifdef KONFIG_UNITTEST int unittest_NAME_OF_TEST() { // testtype_FREE makes it possible to call free_testtype without an error testtype_t t = testtype_FREE ; // TEST init, double free TEST(0 == init_testtype(&t)) ; TEST(t.field_name == VALUE_AFTER_INIT) TEST(0 == free_testtype(&t)) ; TEST(t.field_name == VALUE_AFTER_FREE) TEST(0 == free_testtype(&t)) ; TEST(t.field_name == VALUE_AFTER_FREE) // TEST other functionality ... // unit test success return 0; // If a test fails the macro TEST jumps to this label ONABORT: // free resource in case of abort (void) free_testtype(&t) ; // error occurred return EINVAL; } #endif