Check Tutorial
Prev |  | Next

# Chapter 3. Tutorial: Basic unit testing

This tutorial will use the JUnit Test Infected article (see Test Infected) as a starting point. We will be creating a library to represent money, allowing conversions between different currency types. The development style will be “test a little, code a little” with unit test writing preceding coding. This constantly gives us insights into module usage, and also makes sure we are constantly thinking about how to test our code.

# 3.1. How to write a test

Test writing using Check is very simple. The file in which the checks are defined must include check.h as so:

    #include <check.h>

The basic unit test looks as follows:

    START_TEST (test_name)
    {
      /* unit test code */
    }
    END_TEST

The START_TEST/END_TEST pair are macros that setup basic structures to permit testing. It is a mistake to leave off the END_TEST marker; doing so produces all sorts of strange errors when the check is compiled.

Prev | Home | Next
Unit testing in C |  | Setting up the money tests