In NUnit, the TestFixtureSetup attribute can be used to mark a method which should be executed once before any tests in the fixture.
Is there an analogous concept in CppUnit?
If not, is there a C++ unit testing framework that supports this concept?
Based on the answer below, here is an example which accomplishes this (following the advice of the answers to this question):
// Only one instance of this class is created.
class ExpensiveObjectCreator
{
public:
const ExpensiveObject& get_expensive_object() const
{
return expensive;
}
private:
ExpensiveObject expensive;
};
class TestFoo: public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(TestFoo);
CPPUNIT_TEST(FooShouldDoBar);
CPPUNIT_TEST(FooShouldDoFoo);
CPPUNIT_TEST_SUITE_END();
public:
TestFoo()
{
// This is called once for each test
}
void setUp()
{
// This is called once for each test
}
void FooShouldDoBar()
{
ExpensiveObject expensive = get_expensive_object();
}
void FooShouldDoFoo()
{
ExpensiveObject expensive = get_expensive_object();
}
private:
const ExpensiveObject& get_expensive_object()
{
static ExpensiveObjectCreator expensive_object_creator;
return expensive_object_creator.get_expensive_object();
}
};