Create Fixture just before setUp and delete it just after tearDown.

This commit is contained in:
Eddy Pronk 2011-10-30 10:24:24 +10:00
parent 33c9aaa2f9
commit 5946699c39
4 changed files with 26 additions and 53 deletions

View File

@ -3,6 +3,7 @@
#include <cppunit/Exception.h> #include <cppunit/Exception.h>
#include <cppunit/TestCase.h> #include <cppunit/TestCase.h>
#include <cppunit/extensions/TestFixtureFactory.h>
#if CPPUNIT_USE_TYPEINFO_NAME #if CPPUNIT_USE_TYPEINFO_NAME
@ -106,80 +107,41 @@ class TestCaller : public TestCase
typedef void (Fixture::*TestMethod)(); typedef void (Fixture::*TestMethod)();
public: public:
/*!
* Constructor for TestCaller. This constructor builds a new Fixture
* instance owned by the TestCaller.
* \param name name of this TestCaller
* \param test the method this TestCaller calls in runTest()
*/
TestCaller( std::string name, TestMethod test ) :
TestCase( name ),
m_ownFixture( true ),
m_fixture( new Fixture() ),
m_test( test )
{
}
/*!
* Constructor for TestCaller.
* This constructor does not create a new Fixture instance but accepts
* an existing one as parameter. The TestCaller will not own the
* Fixture object.
* \param name name of this TestCaller
* \param test the method this TestCaller calls in runTest()
* \param fixture the Fixture to invoke the test method on.
*/
TestCaller(std::string name, TestMethod test, Fixture& fixture) :
TestCase( name ),
m_ownFixture( false ),
m_fixture( &fixture ),
m_test( test )
{
}
/*! /*!
* Constructor for TestCaller. * Constructor for TestCaller.
* This constructor does not create a new Fixture instance but accepts
* an existing one as parameter. The TestCaller will own the
* Fixture object and delete it in its destructor.
* \param name name of this TestCaller * \param name name of this TestCaller
* \param test the method this TestCaller calls in runTest() * \param test the method this TestCaller calls in runTest()
* \param fixture the Fixture to invoke the test method on. * \param factory the TestFixtureFactory.
*/ */
TestCaller(std::string name, TestMethod test, Fixture* fixture) : TestCaller(std::string name, TestMethod test, TestFixtureFactory* factory) :
TestCase( name ), TestCase( name ),
m_ownFixture( true ), m_fixture( 0 ),
m_fixture( fixture ), m_test( test ),
m_test( test ) m_factory( factory )
{ {
} }
~TestCaller()
{
if (m_ownFixture)
delete m_fixture;
}
~TestCaller()
{
delete m_factory;
}
void runTest() void runTest()
{ {
// try {
(m_fixture->*m_test)(); (m_fixture->*m_test)();
// }
// catch ( ExpectedException & ) {
// return;
// }
// ExpectedExceptionTraits<ExpectedException>::expectedException();
} }
void setUp() void setUp()
{ {
m_fixture = static_cast<Fixture*>(m_factory->makeFixture());
m_fixture->setUp (); m_fixture->setUp ();
} }
void tearDown() void tearDown()
{ {
m_fixture->tearDown (); m_fixture->tearDown ();
delete m_fixture;
} }
std::string toString() const std::string toString() const
@ -192,9 +154,9 @@ private:
TestCaller &operator =( const TestCaller &other ); TestCaller &operator =( const TestCaller &other );
private: private:
bool m_ownFixture;
Fixture *m_fixture; Fixture *m_fixture;
TestMethod m_test; TestMethod m_test;
TestFixtureFactory *m_factory;
}; };

View File

@ -298,7 +298,7 @@
( new CPPUNIT_NS::TestCaller<TestFixtureType>( \ ( new CPPUNIT_NS::TestCaller<TestFixtureType>( \
context.getTestNameFor( #testMethod), \ context.getTestNameFor( #testMethod), \
&TestFixtureType::testMethod, \ &TestFixtureType::testMethod, \
context.makeFixture() ) ) ) context.cloneFactory() ) ) )
/*! \brief Add a test which fail if the specified exception is not caught. /*! \brief Add a test which fail if the specified exception is not caught.
* *

View File

@ -18,6 +18,7 @@ class TestFixtureFactory
public: public:
//! Creates a new TestFixture instance. //! Creates a new TestFixture instance.
virtual TestFixture *makeFixture() =0; virtual TestFixture *makeFixture() =0;
virtual TestFixtureFactory *clone() =0;
virtual ~TestFixtureFactory() {} virtual ~TestFixtureFactory() {}
}; };
@ -40,6 +41,11 @@ class ConcretTestFixtureFactory : public CPPUNIT_NS::TestFixtureFactory
{ {
return new TestFixtureType(); return new TestFixtureType();
} }
TestFixtureFactory *clone()
{
return new ConcretTestFixtureFactory(*this);
}
}; };

View File

@ -74,6 +74,11 @@ public:
*/ */
const std::string getStringProperty( const std::string &key ) const; const std::string getStringProperty( const std::string &key ) const;
TestFixtureFactory *cloneFactory() const
{
return m_factory.clone();
}
protected: protected:
TestFixture *makeTestFixture() const; TestFixture *makeTestFixture() const;