Technology

C++ unit test

Here’s the usual way I setup my main() for unit tests in unit_t.cpp. You can run either the entire test suite or specify an individual test on the command line:

$ ./unit_t TestWithin::testWithinDouble

Here’s the code:

/**
 * @file pathtest/main.cpp
 * @defgroup PathTest Unit tests for path library
 */
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int
main( int argc, char* argv[] )
{
    std::string                 testname;
    CppUnit::TestResult         controller;
    CppUnit::TestResultCollector result;                // For summary
    CppUnit::BriefTestProgressListener progress;        // Prints '.' for each test
    CppUnit::TestRunner         runner;
    if (argc <1)
        testname = argv[1];
    controller.addListener (&result);
    controller.addListener (&progress);
    runner.addTest (CppUnit::TestFactoryRegistry::getRegistry().makeTest());
    runner.run (controller, testname);
    // Print test in a compiler compatible format.
    CppUnit::CompilerOutputter outputter (&result, std::cout);
    outputter.write();
    return result.wasSuccessful() ? 0 : 1;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.