› unit testing

» Shell helper: running KDE unit tests (ctests) the easy way
Thu, 03/26/2009 - 03:09
Unit tests are in my eyes a very important part of programming. KDE uses them, KDevelop does - the PHP plugin I help writing does as well. cmake comes with a ctest program which does quite well to give you a quick glance on which test suite you just broke with your new fance feature :)
But I am very dissatisfied with it. Right now I usually do the following
# lets assume I'm in the source directory cb && ctest # look for failed test suites cd $failed_test_suite_path ./$failed_test_suite.shell | less # search for FAIL cs cd $to_whereever_I_was_before
That’s pretty much for just running a test. Especially all that cding and lessing became very tedious. Tedious is good, because I eventually fix it:
introducing kdetest
I wrote a bash function (with autocompletion!!!) called kdetest. Calling it without any parameter will run all test suites and gives a nice report of failed functions at the end. Here’s an example (run via cs php && kdetest).
kdetest # ... lots of test output --- ALL PASSED TESTS --- ... PASS : Php::TestCompletion::implementMethods() PASS : Php::TestCompletion::inArray() PASS : Php::TestCompletion::cleanupTestCase() 143 passed tests in total --- ALL FAILED TESTS --- FAIL! : Php::TestCompletion::newExtends() Compared values are not the same FAIL! : Php::TestCompletion::updateExtends() '! forbiddenIdentifiers.contains(item->declaration()->identifier().toString())' returned FALSE. () FAIL! : Php::TestCompletion::updateExtends() '! forbiddenIdentifiers.contains(item->declaration()->identifier().toString())' returned FALSE. () FAIL! : Php::TestCompletion::updateExtends() Compared values are not the same FAIL! : Php::TestCompletion::newImplements() Compared values are not the same FAIL! : Php::TestCompletion::updateImplements() Compared values are not the same 6 failed tests in total
usage
kdetest, i.e. without any arguments runs all tests in this directory and belowkdetest path/to/test.shell ...runs that test suite only,...can by any argument the test suite accepts.
autocompletion
kdetest comes with full support for autocompletion of tests and functions, for example:
milian@odin:~/projects/kde4/php$ kdetest TABTAB completion/tests/completiontest.shell duchain/tests/expressionparsertest.shell parser/test/lexertest.shell duchain/tests/duchaintest.shell duchain/tests/usestest.shell milian@odin:~/projects/kde4/php$ kdetest duchain/tests/usestest.shell TABTAB classAndConstWithSameName classSelf interfaceExtendsMultiple staticMemberFunctionCall classAndFunctionWithSameName constAndVariableWithSameName memberFunctionCall staticMemberVariable classConstant constant memberFunctionInString variable classExtends constantInClassMember memberVariable variableTwoDeclarations classImplements functionAndClassWithSameName memberVarInString variableTwoDeclarationsInFunction classImplementsMultiple functionCall newObject varInString classParent interfaceExtends objectWithClassName
the code
You can find the code below, or you can obtain the most up-to-date version on github. Just head over to my shell-helpers repo and peek into the bash_setup_kde4_programming file.