Re: [Cppunit-devel] hierarchy sample bug...
Brought to you by:
blep
From: Baptiste L. <bl...@cl...> - 2001-05-07 19:16:37
|
> Humm, just one thing that popup in my mind: who owns the fixture given > to the test caller ? That's may be the reason why it is done that way... > > I guess it had something to do with the automatic test registration thing, > which has been removed anyway. No, it was done that way in Michael Feather version. And if I understood thing well, it's also done that way in junit. > Because I did not have a clear picture of all use cases of TestCaller, the > current implementation allows the TestCaller to own the Fixture or not: > If the Fixture is passed through a reference the TestCaller won't own it. > If the Fixture is passed through a pointer the TestCaller will assume > ownership and destroy it in its destructor. Things I want to point out: - in about 95% of the case, you use the default constructor for the fixture. - Even when subclassing a test case, you override the setUp() and tearOff() method, and would still have a default constructor. - non default constructor would most likely be used for a test case with are parameterized (a generic test case that load tests from a file for example). I don't see that happening often. And in all likelyhood, you would more likely overide runTests() than create a suite using some test callers... Well, here I presents the test cases of use of the TestCaller. The common code to create a suite is as follow: TestSuite *suite = new TestSuite( "MyTestCase" ); registerTest( suite ); void registerTest( TestSuite *suite ) { suite->addTest( new TestCaller<MyTestCase>( "test1", test1 ) ); suite->addTest( new TestCaller<MyTestCase>( "test1", test2 ) ); ... } Using the constructor with a pointer on the fixture works: void registerTest( TestSuite *suite ) { te->addTest( new TestCaller<MyTestCase>( "test1", test1, new MyTestCase( "file-test1.test") ) ); suite->addTest( new TestCaller<MyTestCase>( "test1", test2, new MyTestCase("file-test2.test") ) ); ... } The problem is if you want one fixture to be shared with many test caller (which is what you would do when subclassing and reusing test case): void registerTest( TestSuite *suite, MyTestCase &fixture ) { suite->addTest( new TestCaller<MyTestCase>( "test1", test1, fixture ) ); suite->addTest( new TestCaller<MyTestCase>( "test1", test2, fixture ) ); ... } The problem is who owns the referenced fixture: MySubclassedTestCase::suite() { TestSuite *suite = new TestSuite( "MySubClassedTestCase" ); // WHO owns that fixture ??? => who will delete it ? MySubclassedTestCase *fixture = new MySubclassedTestCase(); MyTestCase::registerTest( suite, *fixture ); ... } As I see it the problem is solved by introducing a makeFixture() virtual method in test case which can be subclassed. You would have something like: void registerTest( TestSuite *suite ) { suite->addTest( new TestCaller<MyTestCase>( "test1", test1, makeFixture() ) ); suite->addTest( new TestCaller<MyTestCase>( "test1", test2, makeFixture() ) ); ... } So until a clean way is found to "share" a fixture between test callers, I'll stick to the one fixture per test caller policy (not that much of a bother after all). All the previous problem (subclassing) have been solved by the introduction constructor with a pointer on the fixture. The only use I can see for the test caller with fixture by reference is if the Test Caller is stored by value in another class. Things to do: Update TestSuiteBuilder: - add a addTestCaller() method which take a pointer on the fixture. - a dd a constructor that take a pointer on a "makeFixture()" methods, to make subclassing easier. Baptiste. PS: I tried to login with CVS and SSH this afternoon and I got a error message. So I guess I'll have to go back to the doc ;-( --- Baptiste Lepilleur <gai...@fr...> http://gaiacrtn.free.fr/index.html Author of The Text Reformatter, a tool for fanfiction readers and writers. Language: English, French (Well, I'm French). |