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