Possible to get test name in setUp?

  • Gabriel Redner

    Gabriel Redner - 2006-11-13

    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

    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?


    • Felix Deutsch

      Felix Deutsch - 2006-11-14

      Use a custom TestListener and implement its startTest( CPPUNIT_NS::Test *test ) method, like so:

      class CustomTestListener : public CPPUNIT_NS::TestListener

      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 &registry = 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.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks