How to implement a unit test

About

In computer programming, unit testing is a procedure used to validate that individual units of source code are working properly. A unit is the smallest testable part of an application.

Unlike the Design by contract contracts and tests can a unit test alter the state significantly. You must therefore always fully clean your test up in a teardown method. Because of this you need to set your test up in a setup method before launching the test itself.

Add the suite to the corrrect library header, for example check_libtinymailui.h in libtinymail-test

+ Suite *create_tny_type_suite (void);

Create a file called tny-type-test.c in libtinymail-test

#include "check_libtinymailui.h"

#include <tny-type.h>
#include <tny-detail-type.h>

static TnyType *iface = NULL;
static gchar *str;

static void
tny_type_test_setup (void)
{
	iface = tny_detail_type_new ();

	return;
}

static void 
tny_type_test_teardown (void)
{
	g_object_unref (G_OBJECT (iface));

	return;
}

START_TEST (tny_type_test_set_property)
{
	PropType *property = ..., *gprop;
	tny_type_set_property (iface, property);
	gprop = tny_type_get_property (iface);
	    
	str = g_strdup_printf ("Property can't be set\n");
	fail_unless (property == gprop, str);
	g_free (str);
    
    	return;
}
END_TEST


Suite *
create_tny_type_suite (void)
{
     Suite *s = suite_create ("Type");

     TCase *tc = tcase_create ("Set Property");
     tcase_add_checked_fixture (tc, tny_type_test_setup, tny_type_test_teardown);
     tcase_add_test (tc, tny_type_test_set_property);
     suite_add_tcase (s, tc);

     return s;
}

Adapt check_libtinymailui_main.c in libtinymail-test

+ srunner_add_suite (sr, (Suite *) create_tny_type_suite ());

Adapt Makefile.am in libtinymail-test

Add tny-type-test.c to the check_libtinymailui_SOURCES target

check_libtinymailui_SOURCES = \
	check_libtinymailui.h \
	check_libtinymailui_main.c \
	tny-platform-factory-test.c \
+       tny-type-test.c

References to related other documentation