I am having trouble working around a problem with inheriting unit test cases. Suppose I have a base class Foo and several derived classes. Suppose I have a corresponding base test case and I would like to reuse that base test case with test cases for the derived classes. What I would like to do is this, in highly stylized form: class BaseFooTestCase { public: virtual Foo* create() = 0; void test_base() { Foo* foo = create(); // test foo ... } void add(test_suite* ts) { ts->add(BOOST_CLASS_TEST_CASE(&BaseFooTestCase::test_base, this); } }; class DerivedFooTestCase { public: virtual Foo* create() { return new Derived(); } void test_derived() { Foo* foo = create(); // test foo } void add(test_suite* ts) { BaseFooTestCase::add(ts); ts->add(BOOST_CLASS_TEST_CASE(&DerivedFooTestCase::test_derived, this); } }; Then, something like this: test_suite* init_unit_test_suite(int, char* []) { auto_ptr<test_suite> ts(new test_suite("test")); DerivedFooTestCase* tc = new DeriveFooTestCase(); tc->add(ts); return ts; } However, I can't get the add() methods in the test case classes worked out. The BOOST_CLASS_TEST_CASE macro seems to want a shared_ptr, and passing "this" to it does not seem to work. Could anyone please provide an example of how to do this? Every time I try to work this out, I end up with a mess --- static allocator methods, weak_ptr wrapped by shared_ptr, and other workarounds --- all of which works, but is really quite a lot of effort for what I want and is overly complex for me to recommend to my team as a solution. Any help appreciated. Thanks. Bill
On Friday, January 28, 2005 at 16:23:57 (-0600) Bill Lear writes:
I am having trouble working around a problem with inheriting unit test cases. ...
I have found an answer: #include <iostream> #include <boost/test/unit_test.hpp> using namespace boost::unit_test; using namespace std; struct Foo { virtual void foo(string) = 0; }; struct Bar : public Foo { void foo(string n) { cout << "in " << n << "\n"; }}; class CommonTest { public: void test_base() { auto_ptr<Foo> foo(create()); foo->foo("CommonTest"); } virtual Foo* create() = 0; }; class ConcreteTest : public CommonTest { public: void test_concrete() { auto_ptr<Foo> foo(create()); foo->foo("ConcreteTest"); } virtual Foo* create() { return new Bar(); } }; test_suite* init_unit_test_suite(int, char* []) { auto_ptr<test_suite> suite(new test_suite()); boost::shared_ptr<CommonTest> common_test(new ConcreteTest()); suite->add(BOOST_CLASS_TEST_CASE(&CommonTest::test_base, common_test)); boost::shared_ptr<ConcreteTest> concrete_test(new ConcreteTest()); suite->add(BOOST_CLASS_TEST_CASE(&ConcreteTest::test_concrete, concrete_test)); return suite.release(); } Bill
participants (1)
-
Bill Lear