[Cppunit-devel] RE: TestFactoryRegistry
Brought to you by:
blep
From: King D. <Ki...@tc...> - 2003-02-04 21:21:28
|
> My problem is that I have a "UnitTest" project that contains > the main() to > run the unit test from all other projects in the solution. The other > projects use CPPUNIT_TEST_SUITE_REGISTRATION() to register > their suites. > > > > Once the libraries from the other projects are created the > TestFactoryRegistry is lost, and I cannot use it in my > UnitTest main(). > > > > The post > http://sourceforge.net/mailarchive/message.php?msg_id=908942 > <http://sourceforge.net/mailarchive/message.php?msg_id=908942> > says the > below is an option: > > > > >Have your librairies publishing an > > >interface that provide an access to their TestFactoryRegistry. > > >For example, in suite1.lib project, you add: > > >TestFactoryRegistry &getSuite1Registry() { > > > return TestFactoryRegistry::getRegistry(); > > >} > > This would work, but would involve me having to include a ton > of header > files in my main. One of the stated purposes of having the > TestFactoryRegistry is to remove the "compilation bottleneck > caused by the > inclusion of all test case headers". My solution to that is to have each test project publish its suite name through the same way and use a different namespace for each project. So a foobar test project would have this in an include file: namespace FooBar { inline std::string suiteName() { return "FooBar"; } } That include file is included by the tests in the project, but is not included by the main routine, so the main routine has no compilation dependencies on the include file and so I did not have to add all the individual projects to the main routine's include search path. My main routine then looks something like this: namespace FooBar { extern std::string suiteName(); } namespace Quux { extern std::string suiteName(); } int main( int argc, char* argv[] ) { bool selfTest = (argc > 1) && (std::string("-selftest") == argv[1] ); using namespace CppUnit; TextTestRunner runner; runner.addTest( TestFactoryRegistry::getRegistry( FooBar::suiteName() ).makeTest() ); runner.addTest( TestFactoryRegistry::getRegistry( Quux::suiteName() ).makeTest() ); bool wasSucessful = runner.run( "", false, !selfTest ); if( selfTest ) { CppUnit::CompilerOutputter outputter( runner.result(), std::cerr ); outputter.write(); } return wasSucessful ? 0 : 1; } Because we have references from the main routine to an element in each static library, that library does get included into the link, and we have done it without actually requiring the main routine to do a compile time include which reduces rebuilds and simplifies the set up of the main routine project. This is not ideal, but I have not found any better solution that allows you to group tests into different projects that build static libraries. If Micro$oft had a flag to tell the linker not to optimize away code that is not referenced from the main project this would be unnecessary. I have found no way to accomplish this after much study. Ideally what I would like is to be enable/disable groups of tests simply by saying that the main project depends on them in VC++. -- Dale King |