I would like to tell our logger which test method is being run before it starts. Is something like this possible?
class MyTest : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(MyTest);
CPPUNIT_TEST(MyMethod);
CPPUNIT_TEST(MyOtherMethod);
CPPUNIT_TEST_SUITE_END();
I would like to tell our logger which test method is being run before it starts. Is something like this possible?
class MyTest : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(MyTest);
CPPUNIT_TEST(MyMethod);
CPPUNIT_TEST(MyOtherMethod);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {
SetMyLoggerPrefix(/* ?? getCurrentTestName() ?? */);
}
void MyMethod() {...}
void MyOtherMethod() {...}
...
};
I would like for SetMyLoggerPrefix to be called with arguments "MyMethod" and "MyOtherMethod" in turn. Is there a way to do this?
Thanks,
-Gabe
Use a custom TestListener and implement its startTest( CPPUNIT_NS::Test *test ) method, like so:
class CustomTestListener : public CPPUNIT_NS::TestListener
{
[...]
}
void
CustomTestListener::startTest( CPPUNIT_NS::Test *test )
{
SetMyLoggerPrefix( test->getName() );
}
Register your custom TestListener before you run the tests:
CPPUNIT_NS::TextUi::TestRunner runner;
CPPUNIT_NS::TestResult controller;
// add my custom TestListener
CustomTestListener myCustomTestListener;
controller.addListener( (CPPUNIT_NS::TestListener*) &myCustomTestListener );
// add a listener that collects test results
CPPUNIT_NS::TestResultCollector result;
controller.addListener( &result );
// Add a listener that print dots as tests run
CPPUNIT_NS::TextTestProgressListener progress;
controller.addListener( &progress );
// add root test suite to the test runner
CPPUNIT_NS::TestFactoryRegistry ®istry = CPPUNIT_NS::TestFactoryRegistry::getRegistry( Mytest );
CPPUNIT_NS::Test *test = registry.makeTest();
runner.addTest( test );
// run tests
runner.run( controller );
You could actually implement the logger as a TestListener in the first place.