Re: examples on webpage.

Bart Whiteley <[email protected]> Mon, 24 Feb 2003 13:12:31 -0800
Newsgroups gmane.comp.lib.cppunit.devel
Message-ID <[email protected]>
On Mon, Feb 24, 2003 at 09:58:41PM +0100, Thomas Zander wrote:
> 
> Thanx! This one actually compiled :)
> It took some time to get a correct install; my debian version did not link.
> I had to compile the sources.
> So, I'm of in creating more testunits!
> 

Glad I could help. 

> 
> btw; your testrunner:
> 
> > 	bool wasSucessful = runner.run( "", false );
> > 	return wasSucessful;
> 
> seems wrong; wasSucessful is 1 when everything went right, but returning
> 0 is the 'success' return state of main.
> So I believe it should be
>     if(wasSucessful) return 0;
>     return -1;
> 
> And if you want to assume the implied values of the bool (sounds dirty ;).
>     return !wasSucessful;
> 

Correct.  I was trying to stay as close to the cookbook as possible. 
See attachment for my real testrunner.cc.  It also adds the feature
of passing a command line param to run a single test suite or 
single unit. 

./testrunner TestSuiteName
  or 
./testrunner TestSuiteName::UnitTestName

-- 
Bart Whiteley             Computer Scientist   voice: (925) 423-2249
National Atmospheric Release Advisory Center   FAX: (925) 423-8274
Lawrence Livermore National Laboratory         email: [email protected]
P.O. Box 808, Livermore, CA 94551-0808         MS: L-103
testrunner.cc (text/plain, 1 KB)
/*
 * @author Bart Whiteley
 *
 * This is pretty much straight from the cookbook. 
 * http://cppunit.sourceforge.net/cppunit_cookbook.html
 * You shouldn't need to change this file.  New tests are added via
 * the CPPUNIT_TEST_SUITE_REGISTRATION macro within a .cc file for
 * the new testsuite. 
 */


#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <iostream>

using std::cerr; 
using std::endl;
using std::string; 

int main( int argc, char **argv)
{
	if (argc > 2)
	{
		cerr << "Usage: " << argv[0] << " [TestSuiteName]" << endl;
		return 1; 
	}
	string testName = ""; 
	if (argc == 2)
	{
		testName = argv[1]; 
	}
	CppUnit::TextUi::TestRunner runner;
	CppUnit::TestFactoryRegistry &registry 
			= CppUnit::TestFactoryRegistry::getRegistry();
	runner.addTest( registry.makeTest() );
	bool wasSucessful = false; 
	try
	{
		wasSucessful = runner.run( testName, false );
	}
	catch(std::invalid_argument& e)
	{
		cerr << e.what() << endl;
		return 1;
	}
	return (wasSucessful? 0: 1);
}