Unit Test
From AbiWiki
UnitTest is part of the TestingFramework. It performs basic testing of individual code units.
Contents |
How to use
On Unix, simply run
make test
It will run under Valgrind if configure did detect it.
Here is the sample output of a test from a real bug:
Testing "UT_LocaleTransactor" in ut_locale.t.cpp: ! ut_locale.t.cpp:35 strstr(msg, "1,0") == msg ok ! ut_locale.t.cpp:37 strcmp(setlocale(LC_NUMERIC, NULL), "fr_FR") == 0 ok ! ut_locale.t.cpp:41 NOT(strcmp(setlocale(LC_NUMERIC, NULL), "fr_FR") == 0) ok ! ut_locale.t.cpp:43 strstr(msg, "1.0") == msg ok ! ut_locale.t.cpp:44 strcmp(setlocale(LC_NUMERIC, NULL), "C") == 0 ok ! ut_locale.t.cpp:46 strcmp(setlocale(LC_NUMERIC, NULL), "fr_FR") == 0 FAILED ! ut_locale.t.cpp:48 strstr(msg, "1,0") == msg FAILED ! tf_test.cpp:119 new_valgrind_errs == old_valgrind_errs ok memleaks: sure:112 dubious:800 reachable:451080 suppress:0 ! tf_test.cpp:123 new_valgrind_leaks == old_valgrind_leaks ok
As you can see, some test pass, some fails.
The last 3 lines are for Valgrind. If they fail, then there is a Valgrind error in the code: a memory leak.
How to write test
Writing UnitTest is not really hard, if you understand the code. Sometime it is tricky because some classes are not meant to be used alone.
Directory structure
Every directory that contains code has atdirectory the contains its test.
The support code for the UnitTest is in =abi/src/af/tf/xp/=.
The files are organized to match the source code files. They have the same name but with the.t.cppsuffix. (
.t.mmfor Objective-C++ file where applicable).
One test
Here is a sample test for =UT_Vector=:
TFTEST_MAIN("UT_GenericVector basics") { UT_GenericVector<char *> v; TFPASS(v.getItemCount() == 0); v.addItem("foo"); TFFAIL(v.getItemCount() == 0); }
TFTEST_MAINis a macro to declare the test function.
TFPASS()is a macro to check some condition that should be true
TFFAIL()is a macro to check a condition that should be false
Makefile
TBD