Unit Testing, Part Deux

Once that's done, head over to the current/tests/ directory. There you should find a shiny new unit testing app appropriately named UnitTester. Running UnitTester with no arguments will run all of the available tests against our Haiku implementations. Running 'UnitTester -r5' will run all the unit tests against Be's R5 implementations (for the sake of reference). Running 'UnitTester --list' will list off all the available suites and tests. You can then run specific tests or suites of tests by passing their names as arguments to UnitTester. And finally, 'UnitTester --help' will list off all the available options, since I'm not going to cover any more of them here.

Currently, the Storage, Support, and App Kit tests have been integrated into the testing framework (note that many other kits have separate tests that you may want to investigate as well; dig around in the /current/test/ directory to see what's there).

If you have any problems or questions, let me know.

Part II - Writing Tests

Now, for those of you developers out there that would like to add new tests to or integrate old tests into the framework, this section is for you.

First off, there's an example suite in current/src/tests/ that's meant to be a simple reference point for figuring out what you need to do. Single- and multi-threaded tests are both demonstrated. You might also take a look at the Storage, Support, or App Kit tests for help.

Second off, all the headers for the CppUnit classes you'll need are in current/headers/tools/cppunit (BeOS specific classes) and current/headers/tools/cppunit/cppunit (stock CppUnit classes). Remember to always include stock CppUnit headers with the cppunit/ directory prefix, i.e, if for some reason you want access to CppUnit::TestCase but not BTestCase, use:

 
#include

Now, adding your tests to the framework involves two easy steps :-)

1. Add a suite for your kit (or other major chunk of code, if you aren't technically a kit) if you don't already have one. This is done by creating an add-on that returns a BTestSuite containing all of your tests. You'll want to use the CommonTestLib rule in your Jamfile (see the current/Jamrules for lots of helpful instructions on using various rules) to compile and link everything together.

Headers of interest: TestSuiteAddon.h, TestSuite.h

2. Create a BTestCase subclass (single-threaded) or one or more BThreadedTestCase subclasses (multi-threaded) that implement your tests, and add your tests to your suite with a

CppUnit::TestCaller
object or a

BThreadedTestCaller

object. The example tests show both methods.

 
Headers of interest: TestCase.h, ThreadedTestCase.h,
cppunit/TestCaller.h, ThreadedTestCaller.h

That's it. :-) Certainly, this is a fairly high-level description of things, so if you get stuck somewhere and the available examples aren't helping, feel free to contact me with your questions (or you can send an email to the Haiku mailing list). Also, if you haven't already, I would highly encourage you to check out Jeremy Rand's previous unit testing article. Most of the additions he made to the App/Interface Kit's testing framework were incorporated into the current framework, and he goes into much greater detail about what you need to do to get started (though be warned that some of the class names are slightly different now, and that you are now required to subclass BThreadedTestCase if you want to run tests with multiple threads).