Test fixtures

Check Tutorial
Prev | Chapter 4. Advanced Features | Next

# 4.3. Test fixtures

We may want multiple tests that all use the same Money. In such cases, rather than setting up and tearing down objects for each unit test, it may be convenient to add some setup that is constant across all the tests in a test case. In the extreme programming approach to unit tests, such setup is called a test fixture.

A fixture is created by defining a setup and/or a teardown function, and associating it with a test case. There are two kinds of test fixtures in Check: checked and unchecked fixtures. These are defined as follows:

Checked fixtures

  are run inside the address space created by the fork to create the unit test. Before each unit test in a test case, the setup function is run, if defined. After each unit test, the teardown function is run, if defined. Because they run inside the forked address space, if checked fixtures signal or otherwise fail, they will be caught and reported by the SRunner.

Unchecked fixtures

  are run in the same address space as the test program. Therefore they may not signal or exit, but may use the fail functions. The unchecked setup, if defined, is run before the test case is started. The unchecked teardown, if defined, is run after the test case is done.

## 4.3.1. Test fixture examples

We create a test fixture in Check as follows:

1. Define the global variables, and functions to setup and teardown the globals. The functions both take void and return void

       Money *five_dollars;
       void setup(void) {
         five_dollars = money_create(5, "USD");
       }

       void teardown(void)
       {
         money_free(five_dollars);
       }

2. Add the setup and teardown functions to the test case with “tcase_add_checked_fixture” (this belongs in the suite setup function “money_suite”:

       tcase_add_checked_fixture(tc_core, setup, teardown);

3. We can now rewrite the first test we wrote as follows:

       START_TEST (test_create)
       {
          fail_unless(money_amount(five_dollars) == 5,
                      "Amount not set correctly on creation");
          fail_unless (strcmp(money_currency(five_dollars), "USD") == 0,
                       "Currency not set correctly on creation");
       }
       END_TEST

## 4.3.2. Checked vs Unchecked fixtures

Because checked fixtures run once per unit test, they should not be used for expensive setup. Because unchecked fixtures may take down the entire test program, they should only be used if they are known to be safe.

Additionally, checked and uncheck fixtures may behave differently in CK_NOFORK mode. Because the fork creates a separate address space, in CK_FORK mode unit tests may abuse the objects created in an unchecked fixture with impunity, without affecting other unit tests in the same test case. However, in CK_NOFORK mode, all tests live in the same address space, and side effects in one test will affect the unchecked fixture for other tests. A checked fixture, however, will generally not be affected by unit test side effects, since the setup is run before each unit test. (There is an exception for side effects to the total environment in which the test program lives: e.g., if the setup function initializes a file that a unit test then changes, the combination of the teardown function and setup fuction must be able to restore the environment for the next unit test).

If the setup function in a fixture fails, in either checked or unchecked fixtures, the unit tests for the test case, and the teardown function for the fixture will not be run. A fixture error will be created and reported to the SRunner.

Prev | Home | Next
No fork mode | Up | Multiple suites run through the same SRunner