diff options
Diffstat (limited to 'examples/cppunittest/CppUnitTestMain.cpp')
-rw-r--r-- | examples/cppunittest/CppUnitTestMain.cpp | 61 |
1 files changed, 40 insertions, 21 deletions
diff --git a/examples/cppunittest/CppUnitTestMain.cpp b/examples/cppunittest/CppUnitTestMain.cpp index db7b5bd..76f255d 100644 --- a/examples/cppunittest/CppUnitTestMain.cpp +++ b/examples/cppunittest/CppUnitTestMain.cpp @@ -1,32 +1,51 @@ -#include <cppunit/extensions/TestFactoryRegistry.h> #include <cppunit/CompilerOutputter.h> -#include <cppunit/ui/text/TestRunner.h> +#include <cppunit/TestResult.h> +#include <cppunit/TestResultCollector.h> +#include <cppunit/TestRunner.h> +#include <cppunit/TextTestProgressListener.h> #include "CppUnitTestSuite.h" int main( int argc, char* argv[] ) { - // if command line contains "-selftest" then this is the post build check - // => the output must be in the compiler error format. - bool selfTest = (argc > 1) && - (std::string("-selftest") == argv[1]); - - CppUnit::TextUi::TestRunner runner; - runner.addTest( CppUnitTest::suite() ); // Add the top suite to the test runner - - if ( selfTest ) - { // Change the default outputter to a compiler error format outputter - // The test runner owns the new outputter. - runner.setOutputter( CppUnit::CompilerOutputter::defaultOutputter( - &runner.result(), - std::cerr ) ); - } + // Retreive test path from command line first argument. Default to "" which resolve + // to the top level suite. + std::string testPath = (argc > 1) ? std::string(argv[1]) : ""; + + // Create the event manager and test controller + CppUnit::TestResult controller; + + // Add a listener that colllects test result + CppUnit::TestResultCollector result; + controller.addListener( &result ); + + // Add a listener that print dots as test run. + CppUnit::TextTestProgressListener progress; + controller.addListener( &progress ); - // Run the test. - bool wasSucessful = runner.run( "" ); + // Add the top suite to the test runner + CppUnit::TestRunner runner; + runner.addTest( CppUnitTest::suite() ); + try + { + std::cout << "Running " << testPath; + runner.run( controller, testPath ); + + std::cerr << std::endl; + + // Print test in a compiler compatible format. + CppUnit::CompilerOutputter outputter( &result, std::cerr ); + outputter.write(); + } + catch ( std::invalid_argument &e ) // Test path not resolved + { + std::cerr << std::endl + << "ERROR: " << e.what() + << std::endl; + return 0; + } - // Return error code 1 if the one of test failed. - return wasSucessful ? 0 : 1; + return result.wasSuccessful() ? 0 : 1; } |