Sending arguments to Test Fixtures

2012-05-16
2013-04-22
  • Hi

    I would like to send an argument to a test fixture/suite. At the moment, the test suites that I am running look like:

    CppUnit::TextTestRunner runner;
    CPPUNIT_TEST_SUITE_REGISTRATION(TestIntersectionOfVizConeAndPolygon);
    CPPUNIT_TEST_SUITE_REGISTRATION(TestMessagingSystem);
    CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
    runner.addTest(registry.makeTest());
    runner.run("");
    return;
    

    TestMessagingSystem is a suite that I have defined inside a fixture of the same name. Could I pass an argument to either the suite or the fixture? Preferably I would like to pass it to the fixture. The reason I need to do this is that I am running these tests using a middleware that requires the context from the main class to use some of it's functions. So I need to pass the context "down" to the individual tests. Is that possible?

    Cheers
    Shanker

     
  • Bitwise
    Bitwise
    2012-09-03

    That is not part of the API.  Perhaps derive your own runner class from TextTestRunner and pass the parameter to the constructor or to a method that you add before making the runner.run() call.

     
  • If this is interesting to someone who has a similar question, here is what I did.

    1. I defined the TestMessagingSystem suite without using the macros and instead something like:

    CppUnit::Test* suite(cast::ManagedComponent* mcptr)
              {
                TestMessagingSystem::castMCPtr = mcptr;
                CppUnit::TestSuite *suiteOfTests = new CppUnit::TestSuite( "TestMessagingSystem" );
                        suiteOfTests->addTest( new CppUnit::TestCaller<TestMessagingSystem>(
                                                                           "testPublishEOIMsgs",
                                                                           &TestMessagingSystem::testPublishEOIMsgs ) );
                       ......
    

    Which means I overloaded the suite function. If someone is interested in the same, I used a static variable of the fixture to store the variable that I passed through.

    2. Then in the main program, I didn't use the CPPUNIT_TEST_SUITE_REGISTRATION macro either. Instead, I did:

    TestMessagingSystem msgtest;
      runner.addTest(msgtest.suite(this));
    

    which means that I defined the fixture explicitly, also of name TestMessagingSystem. Then I called the suite inside and a passed an argument (which is the this pointer here).