Next: , Previous: Testing Signal Handling, Up: Advanced Features



4.6 Looping Tests

Looping tests are tests that are called with a new context for each loop iteration. This makes them ideal for table based tests. If loops are used inside ordinary tests to test multiple values, only the first error will be shown before the test exits. However, looping tests allow for all errors to be shown at once, which can help out with debugging.

Adding a normal test with tcase_add_loop_test() instead of tcase_add_test() will make the test function the body of a for loop, with the addition of a fork before each call. The loop variable _i is available for use inside the test function; for example, it could serve as an index into a table. For failures, the iteration which caused the failure is available in error messages and logs.

Start and end values for the loop are supplied when adding the test. The values are used as in a normal for loop. Below is some pseudo-code to show the concept:

     
     for (_i = tfun->loop_start; _i < tfun->loop_end; _i++)
     {
       fork();      /* New context */
       tfun->f(_i);  /* Call test function */
       wait();      /* Wait for child to terminate */
     }

An example of looping test usage follows:

     
     static const int primes[5] = {2,3,5,7,11};
     
     START_TEST (check_is_prime)
     {
       fail_unless (is_prime(_i));
     }
     END_TEST
     
     ...
     
     tcase_add_loop_test (tcase, check_is_prime, 0, 5);

Looping tests work in CK_NOFORK mode as well, but without the forking. This means that only the first error will be shown.