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...) |
|||
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 | + | ---+++ How to use |
- | + | ||
- | On Unix, simply run | + | On Unix, simply run |
- | + | ||
- | =make test= | + | =make test= |
- | + | ||
- | It will run under Valgrind if configure did detect it. | + | It will run under Valgrind if configure did detect it. |
- | + | ||
- | Here is the sample output of a test from a real bug: | + | Here is the sample output of a test from a real bug: |
- | + | ||
- | <verbatim> | + | <verbatim> |
- | 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 |
- | </verbatim> | + | </verbatim> |
- | + | ||
- | 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 some | + | ---+++ How to write some |
- | + | ||
- | 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 | + | ---++++ Directory structure |
- | + | ||
- | Every directory that contains code has a =t= directory the contains its test. | + | Every directory that contains code has a =t= 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 =.t.cpp= suffix. (=.t.mm= for Objective-C++ file where applicable). | + | 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). |
- | + | ||
- | ---++++ One test | + | ---++++ One test |
- | + | ||
- | Here is a sample test for =UT_Vector=: | + | Here is a sample test for =UT_Vector=: |
- | + | ||
- | <verbatim> | + | <verbatim> |
- | TFTEST_MAIN("UT_GenericVector basics") | + | TFTEST_MAIN("UT_GenericVector basics") |
- | { | + | { |
- | UT_GenericVector<char *> v; | + | UT_GenericVector<char *> v; |
- | + | ||
- | TFPASS(v.getItemCount() == 0); | + | TFPASS(v.getItemCount() == 0); |
- | v.addItem("foo"); | + | v.addItem("foo"); |
- | TFFAIL(v.getItemCount() == 0); | + | TFFAIL(v.getItemCount() == 0); |
- | } | + | } |
- | </verbatim> | + | </verbatim> |
- | + | ||
- | =TFTEST_MAIN= is a macro to declare the test function. | + | =TFTEST_MAIN= is a macro to declare the test function. |
- | + | ||
- | =TFPASS()= is a macro to check some condition that should be true | + | =TFPASS()= is a macro to check some condition that should be true |
- | + | ||
- | =TFFAIL()= is a macro to check a condition that should be false | + | =TFFAIL()= is a macro to check a condition that should be false |
- | + | ||
- | ---++++ Makefile | + | ---++++ Makefile |
- | + | ||
- | TBD | + | TBD |
[[Category:To Convert]] | [[Category:To Convert]] |
Revision as of 19:42, 12 October 2007
UnitTest is part of the TestingFramework. It performs basic testing of individual code units.
---+++ 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:
<verbatim> 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 </verbatim>
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 some
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 =t= directory 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.cpp= suffix. (=.t.mm= for Objective-C++ file where applicable).
---++++ One test
Here is a sample test for =UT_Vector=:
<verbatim> TFTEST_MAIN("UT_GenericVector basics") {
UT_GenericVector<char *> v;
TFPASS(v.getItemCount() == 0); v.addItem("foo"); TFFAIL(v.getItemCount() == 0);
} </verbatim>
=TFTEST_MAIN= is 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