Writing Unit Tests for AbiWord

Hubert Figuière <hfiguiere@teaser.fr>

December 31st 2004

Introduction

Unit testing has been a proven to test functionnality of individual elements of a software. The lack of unit testing in AbiWord has been plagging us with various breakage all along the development cycle of AbiWord.

This document will explain de unit test framework in place, when to write such a test, how to write them and how to use it.

Since unit test framework scope is single piece of code, it should not be considered the only way to test. It comes as the first step. Mainly, regression testing will be covered later as they remain to be developped, and are seriously in need for it.

When to write tests ?

Ideally each class and function should have a unit test written for. You should write a unit test when you write a new class, a new method for a class, a new function or find a lack in the current unit testing. The other case is when you fix a bug with a strange corner case that you can reproduce with a simple test.

The Testing framework

A new subtree abi/src/af/tf/ appeared. It contains all the testing framework. Currently only unit testing, but I expect regression tests helpers to come over.

It is based on the WvTest class found in the WvStreams LGPL library. Initial version will probably be a simple rip-off of that code, but with time, will be improved to fill into the whole Abi framework.

This framework provide the core infrastructure for running the test, simplifying at most the work needed for people to write tests.

How to write tests

Find the source file where the code to test is. There should be a directory t that contains all the tests. The unit tests will be by convention in t/my_source.t.cpp.

Here is the skeleton of the test file:

#include "tf_test.h"

#include "my_source.h"

TFTEST_MAIN("test decription")

{

my_class c;

TFPASS(c.foo());

TFFAIL(c.has_error());

}

TFTEST_MAIN() macro is a unit test. TFPASS() macro test for true condition. TFFAIL() macro test for false condition. They both cause a test failure.

If you created the file, don't forget to add it to the t/GNUmakefile.am to have it compiled. That is all, the test framework handles everything else.

If the test comes from a bugzilla issue, please mention the bug # in the test description.

Running the tests

The test binary is src/wp/test/xp/AbiWord-test. Just run it.

Other alternatives

We could have used cxxtest but one of the major issues is that it requires some Perl preprocessing and that it does everything in the header, make compilation time a real pain.

cxxtest: http://cxxtest.sourceforge.net/