I want to give TDD a try and I've chosen the UnitTest++ framework, but the documentations is almost non-existent (to my knowledge).
My concern is this: in all the tutorials I've seen, they put UnitTest::RunAllTests()
in the开发者_如何学JAVA main()
function. I'm guessing they do it only to simplify the explanation, but I wouldn't want that with my software. Where should I put UnitTest::RunAllTests()
so that I can have it executed every time I build the software but not when I run it?
UnitTest::RunAllTests()
should be put into the main
function of a separate program, which you compile and run as part of your build process.
One thing we've done in the past is to add a command line argument which makes the main executable run all the tests and then exit. It's fairly easy to arrange some #ifdefs such that this code gets compiled out on release builds. Something like this (it's not very C++ but if you weren't parsing command line arguments already this is the simplest way to do it):
int main (int argc, char *argv[])
{
#ifdef DEBUG
if (argc > 1 && !strcmp(argv[2], "-t"))
{
return UnitTest::RunAllTests();
}
#endif
[rest of program]
}
精彩评论