Next: , Previous: Designing unit tests, Up: Unit testing


8.2.2 Test files

The test files contain collections of unit tests associated with a function. Each test file is named function-name.c, where function-name is the name of the function under testing with underscores replaced with dashes.

For example, the source file containing unit tests for the pdf_stm_new function would be called stm-new.c.

A minimal test file looks like this:

     /*
      * Include the check library.
      */
     #include <check.h>
     
     /*
      * START_TEST and END_TEST are macros provided by the check
      * library.
      */
     START_TEST(FUNCTION_NAME_NNN)
     {
       /* Check a condition.  One of several types of check
          available in the check library.
        */
       fail_if(0 == 1);
     }
     END_TEST
     
     
     /*
      * Provide this function to gather all the tests together.
      */
     TCase* test_FUNCTION_NAME (void)
     {
       TCase* tc = tcase_create ("FUNCTION_NAME");
       tcase_add_test(tc, FUNCTION_NAME_NNN);
       return tc;
     }

Note the test_FUNCTION_NAME function. It is the function called by the test driver in order to perform all the tests implemented in the test file.