Newbie - passing command line arguments in

  • Martin Waller
    Martin Waller

    I'm really struggling to work out how to pass command line arguments from main into my TestFixture - surely this must be easy to do?

    Here's my main - I want to pass my one or more argvs into my
    TestFixture (follows):

    int main( int argc, char **argv)
    //all I want is to get these argvs to my TextFixture!
        // derive results filename from executable filename

        std::string    resultsFname = argv[0];
        resultsFname += ".cppunitresults.txt";

        std::ofstream resultsFile(resultsFname.c_str());

        // Create the event manager and test controller
        CppUnit::TestResult controller;

        // Add a listener that colllects test result
        CppUnit::TestResultCollector result;
        controller.addListener( &result );       

        // make a test runner, and add all registered tests

        CppUnit::TestRunner runner;
        CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
        runner.addTest( registry.makeTest() ); controller, "" );

        CppUnit::TextOutputter outputter(&result, resultsFile);

        return result.wasSuccessful() ? 0 : 1;

    Here's my TestFixture declaration:

    #include <cppunit/TestFixture.h>

    #include <cppunit/extensions/HelperMacros.h>

    class MyFixture:public CppUnit::TestFixture

        void setUp();
        void tearDown();

        void runTests();


    I just don't see how to get the argvs into there - ideally for use in setUp().  Can someone help me out...I'm very confused?


    • Felix Deutsch
      Felix Deutsch

      The question remains just WHY you would want to have access to the command line args in setUp().

      If you want to output something containing (parts of) the command line arguments as each test runs, _don't_ do it in setUp().

      SetUp is strictly for setting up the test environment for each testcase; for logging and related stuff use a TestListener.

      You create it as a Singleton, then call a method on it in main(), i.e. myTestListener::Instance().setCmdLineArgs(argc, argv),
      which saves them to private members.

      Then you implement the startTest() method of the TestListener (which gets called before each testcase, after you register the TestListener),
      where you have access to the previously saved command line arguments and are able to do whatever it is you want to do.