How to run subset of unit tests in CPPUnit by selecting them at run-time?
Asked Answered
C

3

7

I am using CppUnit as a unit test framework. Is it possible to select a subset of testcases to execute at runtime?

Is there a filtering option provided within CppUnit to accomodate this?

Countryside answered 6/5, 2010 at 19:2 Comment(0)
C
6

The TestRunner::run() method you are likely calling in your main() actually has optional parameters: run(std::string testName = "", bool doWait = false, bool doPrintResult = true, bool doPrintProgress = true). testName must be the specific name of a test. You could request a specific test by name if you wanted. You can also call runTest(Test*) on a specific test, or runTestByName(testName).

But it sounds like you want to get more sophisticated. Assuming you registered all your tests with the CPPUNIT_TEST_SUITE_REGISTRATION() macros, the static TestFactoryRegistry::makeTest() method will return a TestSuite of all registered tests.

The TestSuite object yields a vector via the getTests() method. You could iterate through those, matching their names against a regexp (or by index number or however you want) and instead of calling TestRunner::addTest(registry.makeTest()) on the whole suite like most people do, you just add the specific tests you're requesting.

You'll have to write something to iterate through the tests and do the matching, but other than that it should be dead simple. Probably a dozen lines of code, plus parsing the command line arguments. Use regex to keep it simpler for yourself.

Chadwick answered 7/5, 2010 at 4:41 Comment(0)
C
1

If you are using the GUI test runner for cppunit you can just check the tests you want to run.

If you cannot use the GUI test runner, check out this post - it describes an "configurable" way of defining which tests to run based on a xml document (the last post describes more or less the solution I had in the end).

Connecticut answered 6/5, 2010 at 19:6 Comment(4)
No, I am not using GUI test runner (it seems GUI test runner is supported on Win - I am using Linux). I am looking for command line options, preferably using regex.Countryside
I edited the post with a link to a possible solution - let me know if you found it useful.Connecticut
Thanks for the link. I checked it - looks interesting, I will try to build and run it. However, not sure from the discussion - have you tried to use it in inter-active mode?Countryside
link provided in answer is brokenPy
H
0

An alternative approach:

// find the unit test as specified by the one argument to this program
CPPUNIT_NS::Test *suite = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
int iTestIndex = 0;
for (; iTestIndex < suite->getChildTestCount(); ++iTestIndex)
{
    fprintf(stderr, "INFO: Looking for a match between '%s' and '%s'\n",
            suite->getChildTestAt(iTestIndex)->getName().c_str(),
            argv[1]);
    if (suite->getChildTestAt(iTestIndex)->getName() == std::string(argv[1]))
    {
        fprintf(stderr, "INFO: Found a match for '%s' and '%s'\n",
                suite->getChildTestAt(iTestIndex)->getName().c_str(),
                argv[1]);
        break;
    }
}
if (iTestIndex >= suite->getChildTestCount())
{
    fprintf(stderr, "ERROR: Did NOT find test '%s'!\n", argv[1]);
    return -1;
}
Heeltap answered 3/5, 2018 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.