实验: cppunit

cppunit 下载点: http://sourceforge.net/projects/cppunit/files/cppunit/1.12.1/cppunit-1.12.1.tar.gz/download

测试环境: vs2005 + cppunit.

主要参考资料: http://blog.csdn.net/jinjunmax_78/article/details/2033755


释放到 D:\cppunit-1.12.1\


运行D:\cppunit-1.12.1\src\CppUnitLibraries.dsw,  由vs2005转换为CppUnitLibraries.sln

选择编译配置 "Release Unicode"


修改 d:\cppunit-1.12.1\src\msvc6\testrunner\MsDevCallerListCtrl.cpp 中的

#import "libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2" version("7.0") lcid("0") raw_interfaces_only named_guids


#import "libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2" version("8.0") lcid("0") raw_interfaces_only named_guids


Rebuild Solution,  ========== Rebuild All: 6 succeeded, 0 failed, 0 skipped ==========


新建d:\cppunit, 分别建立<bin>, <inc>, <lib>, 将生成的文件拷贝出来,供测试用.

 D:\cppunit\bin 的目录

2011-08-29  23:47    <DIR>          .
2011-08-29  23:47    <DIR>          ..
2011-08-29  23:37           217,088 cppunit_dll.dll
2011-08-29  23:37           131,072 DllPlugInTesteru.exe
2011-08-20  10:25           708,608 stlport.5.2.dll
2011-08-29  23:37           102,400 TestPlugInRunner.exe
2011-08-29  23:37           143,360 testrunneru.dll
               5 个文件      1,302,528 字节
               2 个目录 92,759,465,984 可用字节


 D:\cppunit\lib 的目录

2011-08-29  23:46    <DIR>          .
2011-08-29  23:46    <DIR>          ..
2011-08-29  23:37        14,948,684 cppunit.lib
2011-08-29  23:37           206,418 cppunit_dll.lib
2011-08-29  23:37             4,818 testrunneru.lib
               3 个文件     15,159,920 字节
               2 个目录 92,759,465,984 可用字节


 D:\cppunit\inc 的目录

2011-08-29  23:45    <DIR>          .
2011-08-29  23:45    <DIR>          ..
2011-08-29  23:45    <DIR>          cppunit
2001-04-29  07:38                83 Makefile.am
2008-02-20  13:59            15,400 Makefile.in
2011-08-29  23:45    <DIR>          msvc6
               2 个文件         15,483 字节
               4 个目录 92,759,465,984 可用字节

cppunit测试工程编译环境设置如下:

测试工程编译环境设置为 release unicode.

头文件路径: D:\cppunit\inc

库路径: D:\cppunit\lib

工作目录: D:\cppunit\bin


当做测试工程的样例来自于: D:\cppunit-1.12.1\examples\money, 将这个工程拷贝到D:\devTemp\money, 用来证明和cppunit本身的编译环境完全隔离.


在测试程序头部加入

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

在main()末尾加入 以下语句, 使测试程序在退出前能停住, 用于观看测试结果.

  _tprintf(_T("END, press any key to quit\n"));
  getchar();


编译完成后, 运行此程序, 可以看到cppunit的测试结果.

#include "StdAfx.h"

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

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


int main(int argc, char* argv[])
{
  // Get the top level suite from the registry
  CPPUNIT_NS::Test *suite = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();

  // Adds the test to the list of test to run
  CPPUNIT_NS::TextUi::TestRunner runner;
  runner.addTest( suite );

  // Change the default outputter to a compiler error format outputter
  runner.setOutputter( new CPPUNIT_NS::CompilerOutputter( &runner.result(),
                                                          CPPUNIT_NS::stdCOut() ) );
  // Run the test.
  bool wasSucessful = runner.run();

  // Return error code 1 if the one of test failed.


  _tprintf(_T("END, press any key to quit\n"));
  getchar();
  
  return wasSucessful ? 0 : 1;
}


运行结果为:

....

OK (4)
END, press any key to quit

实际工程是win32 + unicode + release,  dll居多,  用cppunit控制台程序测试比较方便.


测试包的编写用cppunit宏组成测试包, 由cppunit框架自动调用.

// MoneyTest.h
#ifndef MONEYTEST_H
#define MONEYTEST_H

#include <cppunit/extensions/HelperMacros.h>

class MoneyTest : public CPPUNIT_NS::TestFixture
{
  CPPUNIT_TEST_SUITE( MoneyTest );
  CPPUNIT_TEST( testConstructor );
  CPPUNIT_TEST( testEqual );
  CPPUNIT_TEST( testAdd );
  CPPUNIT_TEST_EXCEPTION( testAddThrow, IncompatibleMoneyError );
  CPPUNIT_TEST_SUITE_END();

public:
  void setUp();
  void tearDown();

  void testConstructor();
  void testEqual();
  void testAdd();
  void testAddThrow();
};

#endif  // MONEYTEST_H

D:\cppunit-1.12.1\examples下还有些cppunit测试样例, 可以参考.


下面从头写一个程序, 应用cppunit进行测试. 来验证cppunit测试的完成流程.

主程序

/** @file Math.h */
#pragma once

/**
* @class CMath
* @brief 通过不断的调用add方法, 累加结果到m_iAddSum
*/
#include <windows.h>

class CMath
{
public:
	CMath(void) : m_llAddSum(0)
	{

	}

	virtual ~CMath(void)
	{

	}

public:
	VOID add(INT iX) {m_llAddSum += iX;}
	LONGLONG GetAddSum() {return m_llAddSum;}

private:
	LONGLONG m_llAddSum;/**< 累加和 */
};

// mainprog.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Math.h"

int _tmain(int argc, _TCHAR* argv[])
{
	INT i = 0;
	CMath math;

	for(i = 0; i < 100; i++)
	{
		math.add(i);
	}

	_tprintf(_T("<%s>=<%d>\n"),
		_T("math.GetAddSum()"), 
		math.GetAddSum());

	_tprintf(_T("END, press any key to quit\n"));
	getchar();

	/** run results
	* <math.GetAddSum()>=<4950>
	* END, press any key to quit
	*/
	return 0;
}


再写测试程序, 测试程序搬到一个独立目录, 和主程序完全分开, 将需要测试的类文件拷贝到测试程序中.

// testcase_Math.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>

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

#pragma comment(lib, "cppunit.lib")

int _tmain(int argc, _TCHAR* argv[])
{
	BOOL bWasSucessful = TRUE;

	CPPUNIT_NS::Test *suite = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
	CPPUNIT_NS::TextUi::TestRunner runner;
	runner.addTest( suite );
	runner.setOutputter( new CPPUNIT_NS::CompilerOutputter( &runner.result(), CPPUNIT_NS::stdCOut()));
	
	bWasSucessful = runner.run();

	_tprintf(_T("bWasSucessful = [%s], END, press any key to quit\n"), 
		bWasSucessful ? _T("TRUE") : _T("FALSE"));

	getchar();
	return bWasSucessful ? 0 : 1;

	/** run results
	 .
	OK (1)
	bWasSucessful = [TRUE], END, press any key to quit
	*/
}



#pragma once

#include <cppunit/extensions/HelperMacros.h>
#include "Math.h" /**< 将要测试的类文件拷贝到测试工程中 */

class case_Math  : public CPPUNIT_NS::TestFixture
{
	CPPUNIT_TEST_SUITE( case_Math );
	CPPUNIT_TEST( testcase_addsum );
	CPPUNIT_TEST_SUITE_END();

public:
	void setUp();
	void tearDown();

	void testcase_addsum();

private:
	CMath m_math;
};

#include "StdAfx.h"
#include "case_Math.h"

CPPUNIT_TEST_SUITE_REGISTRATION( case_Math ); /**< very important~ */

void case_Math::setUp()
{

}

void case_Math::tearDown()
{

}

void case_Math::testcase_addsum()
{
	INT i = 0;
	LONGLONG llAddSum = 0;

	for(i = 0; i < 100; i++)
	{
		llAddSum += i;
		m_math.add(i);

		CPPUNIT_ASSERT(llAddSum == m_math.GetAddSum());
	}
}

这个测试用例上传位置: http://download.csdn.net/source/3559395

cppunit基本用法如此, 更多测试函数的使用, 可以参见\cppunit-1.12.1\examples\中的其他工程.







你可能感兴趣的:(实验: cppunit)