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