Cppunit

From Yefu's notes
Jump to: navigation, search
#include "stdafx.h"
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/TestResult.h>

/*************************************************/
#include "MyUT.h"
CPPUNIT_TEST_SUITE_REGISTRATION(MyUT);
/*************************************************/

int main(int argc, _TCHAR* argv[])
{
	  // Get the top level suite from the registry
  CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
  CppUnit::TestResult controller;
  // Adds the test to the list of test to run
  CppUnit::TextUi::TestRunner runner;
  runner.addTest( suite );

  // Change the default outputter to a compiler error format outputter
  runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
                                                       std::cerr ) );
  // Run the tests.
  bool wasSucessful = runner.run("",true);

  // Return error code 1 if the one of test failed.
  return wasSucessful ? 0 : 1;
}

Codes in MyUT.h:

#pragma once
#include <cppunit/extensions/HelperMacros.h>
#include <iostream>

class MyUT: public CppUnit::TestFixture
{
  CPPUNIT_TEST_SUITE(TListMode);
  CPPUNIT_TEST(testNothing);
  CPPUNIT_TEST_SUITE_END();
public:
	void setUp(){};
	void tearDown(){};

	void testNothing()
	{
		CPPUNIT_ASSERT(false);
	};
};