I'm just starting to learn about CppUnit and think it's going to be very
useful for our project. I'm obviously a newbie here, so please set me
straight as needed.
As I started to use CppUnit, I struggled a bit because the current CppUnit
Cookbook didn't quite explain enough for me. I decided to take a stab at
rewriting the cookbook, based on a couple of the documents I found on JUnit
(which helped me understand CppUnit better). My rewrite is very
Linux-centric -- it would be great if someone could update it with some
Windows instructions, since it seems like there's lots of CppUnit developers
using Windows. I also put together some sample code with explanations and
build instructions for Linux.
One of the best features of a test framework like CppUnit is the ability to
integrate the tests into the standard build process. As I looked through the
example code, and the original cookbook, I was confused why
TextTestRunner::run returned a void instead of int. By returning an int, my
test program can give an indication to the 'make' program whether any of the
tests failed.
In other words, I wanted to be able to write a test program like this.
int main(int argc, char **argv) {
CppUnit::TextTestRunner runner;
runner.addTest (MoneyTest::suite());
return runner.run(); // returns non-zero if any test failed
}
That way, when I run 'make check', the make actually fails if the test
program returns a non-zero value. The test failure is reported in the output
either way, but the non-zero return value brings make to a halt just like a
compile error.
Perhaps there was another way to do this, but I found it pretty easy to just
change TextTestRunner to do what I want. Please let me know if I could have
had similar results without changing the CppUnit source.
I'm attaching a tarball (39k) that includes my rewrite of the cookbook (with
sample code), and the patch to CppUnit 1.6.2 to have TextTestRunner::run
return an int.
- Phil
|