Main Page | Modules | Data Structures | Directories | File List | Data Fields

Unit testing


Defines

#define U_TEST_MODULE(name)   int u_test_run_ ## name (void)
 Define a test module.
#define U_TEST_RUN(f)
 Define a test function.
#define U_TEST_MODULE_USE(name)
 Import a test module in the test program.

Typedefs

typedef int(* test_runner_t )(void)

Functions

int u_test_run (int argc, char **argv)
 Run tests.

Variables

test_runner_t _mods []
test_runner_t_top
char * _mods_nm []
char ** _top_nm
int _test_cnt
int _test_ok
int _test_fail
int _verbose

Detailed Description

Tests are bundles in test modules defined by the U_TEST_MODULE macro; each module contain one or more test functions; such functions will be considered succesfull when returning 0. Function should be run from within the module main function wrapped by the U_TEST_RUN macro.

Example: mymodule.c

          int test_feature_1(void)
          {
              dbg_err_if(...)

              return 0; 
          err:
              return ~0;
          }

          int test_feature_2(void)
          {
              dbg_err_if(

              return 0; 
          err:
              return ~0;
          }

          U_TEST_MODULE( mymodule )
          {
              U_TEST_RUN( test_feature_1 );
              U_TEST_RUN( test_feature_2 );
          
              return 0;                                                
          }

To build the 'runtest' executable define a main() function like the example below and import your test modules using the U_TEST_USE_MODULE macro.

Example: main.c

          #include <u/libu.h>

          int facility = LOG_LOCAL0;

          int main(int argc, char **argv)
          {
              U_TEST_USE_MODULE( mymodule );
              U_TEST_USE_MODULE( mymodule2 );
          
              return u_test_run(argc, argv);
          }

To run all tests call the built executable with no arguments. At the end of execution it will print the number of tests run and the number ot tests failed.

 *          ./runtest # runs all modules' test
 *          

If you want to run just selected modules add their name to the 'runtest' command line:

 *          ./runtest mymodule # runs just 'mymodule' tests
 *          ./runtest mymodule othermod # runs 'mymodule' and 'othermod' tests
 *          

Define Documentation

#define U_TEST_MODULE name   )     int u_test_run_ ## name (void)
 

Defines a test module named name.

Parameters:
name module name

Definition at line 87 of file test.h.

#define U_TEST_MODULE_USE name   ) 
 

Value:

do {    \
        int u_test_run_ ## name (void); \
        *_top = u_test_run_ ## name; ++_top; *_top = NULL; \
        *_top_nm = u_strdup( #name ); ++_top_nm; *_top_nm = NULL; \
    } while(0)
Import a test module to be use by the test program.

Parameters:
name the name of the module

Definition at line 110 of file test.h.

#define U_TEST_RUN  ) 
 

Value:

if( f () ) { _test_cnt++; _test_fail++; con("%s: failed", #f); } \
    else { _test_cnt++; _test_ok++; if(_verbose) con("%s: ok", #f); }
Defines a test function.

Parameters:
f the function name
Returns:
0 on success, not zero on failure (i.e. test failed)

Definition at line 98 of file test.h.


Function Documentation

int u_test_run int  argc,
char **  argv
 

Run tests. Must be called by the main() function. Returns when all tests have been run.

Parameters:
argc main() argc argument
argv main() argv argument
Returns:
0 on success, not zero on failure

Definition at line 83 of file test.c.

References _mods, _test_cnt, _test_fail, and _top.


←Products
© 2005-2007 - KoanLogic S.r.l. - All rights reserved