http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html
-------------------------------------UI格式 开始--------------------------------------------------------------------------------
测试代码框架:
.h
#include <cppunit/TestCase.h>
#include <CppUnit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
class CPlusTestCase: public CppUnit::TestCase
{
CPPUNIT_TEST_SUITE(CPlusTestCase);
CPPUNIT_TEST(testAdd);
CPPUNIT_TEST_SUITE_END();
public:
CPlusTestCase();
virtual ~CPlusTestCase();
void testAdd();
static CppUnit::Test* GetSuite();
};
.cpp:
#include "stdafx.h"
#include "UnitTest.h"
#include "PlusTestCase.h"
//#include <cppunit/TestAssert.h>
#include "CPlusTestSuit/Plus.h"
#include <cppunit/extensions/TestFactoryRegistry.h>
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CPlusTestCase, "CPlusTestSuit");
CppUnit::Test* CPlusTestCase::GetSuite()
{
CppUnit::TestFactoryRegistry& reg = CppUnit::TestFactoryRegistry::getRegistry("CPlusTestSuit");
return reg.makeTest();
}
void CPlusTestCase::testAdd()
{
CPlus plus;
int nResult = plus.Add(10, 20); //执行Add操作
CPPUNIT_ASSERT_EQUAL(30, nResult); //检查结果是否等于30
}
工程dll接口代码
// Plus.h
class AFX_EXT_CLASS CPlus
{
public:
CPlus();
virtual ~CPlus();
public:
int Add(int nNum1, int nNum2);
};
运行测试:
CppUnit::MfcUi::TestRunner runner;
runner.addTest(CPlusTestCase::GetSuite());//添加测试
runner.run();
-------------------------------------UI格式 结束--------------------------------------------------------------------------------
-------------------------------------Console格式 开始--------------------------------------------------------------------------------
test case class:
#ifndef _CEXAMPLE_TEST_H
#define _CEXAMPLE_TEST_H
#include <cppunit/extensions/HelperMacros.h>
class CExampleTest : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE( CExampleTest );
CPPUNIT_TEST( TestCase01 );
CPPUNIT_TEST( TestCase02 );
// CPPUNIT_TEST( TestCase03 );
// 在这里添加测试用例声明, 将测试用例加入测试套
// CPPUNIT_TEST( TestCasexx );
CPPUNIT_TEST_SUITE_END();
public:
void setUp();
void tearDown();
void TestCase00();
// 在这里添加测试用例声明, 声明测试类的成员函数
// void TestCasexx();
void TestCase01();
void TestCase02();
void TestCase03();
void TestCase04();
void TestCase05();
};
#include <cppunit/config/SourcePrefix.h>
#include "CExampleTest.h"
#include <stdlib.h>
#include <string.h>
#include "stdio.h"
#include "malloc.h"
/* 外部说明 */
extern int Calc_complex_level(const char *, const unsigned int, unsigned int *);
// 注册测试套到CppUnit
CPPUNIT_TEST_SUITE_REGISTRATION( CExampleTest );
// 把测试文件内容读取到缓冲区中
void InitFunc(char *pcFile, char **ppcFuncCont, unsigned int *puiLength)
{}
void CExampleTest::TestCase01()
{
char *pcFuncCont;
unsigned int uiFuncLength;
unsigned int uiFuncComplexLevel;
int iRet = 0;
/* 初始化,把函数读入到缓冲区中 */
InitFunc("..\\testcase\\input.txt", &pcFuncCont, &uiFuncLength);
/* 要测试的函数 */
iRet = Calc_complex_level(pcFuncCont, uiFuncLength, &uiFuncComplexLevel);
free(pcFuncCont);
CPPUNIT_ASSERT(1 == iRet);
CPPUNIT_ASSERT(7 == uiFuncComplexLevel);
}
void CExampleTest::TestCase02(){ }
...
source:
int Calc_complex_level(const char *pFuncCont, const unsigned int uiFuncLength, unsigned int *puiComplexLevel);
main:
#include <cppunit/TextOutputter.h>
#include <cppunit/XmlOutputter.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
int main(int argc, char* argv[])
{
// Create the event manager and test controller
CPPUNIT_NS::TestResult controller;
// Add a listener that colllects test result
CPPUNIT_NS::TestResultCollector result;
controller.addListener( &result );
// Add a listener that print dots as test run.
CPPUNIT_NS::BriefTestProgressListener progress;
controller.addListener( &progress );
// Add the top suite to the test runner
CPPUNIT_NS::TestRunner runner;
runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry("All Tests").makeTest() );
runner.run( controller );
// Print test in a text format.
CPPUNIT_NS::TextOutputter outputter( &result, CPPUNIT_NS::stdCOut() );
outputter.write();
// This for XML output
std::ofstream file( "TestResult.xml" );
CPPUNIT_NS::XmlOutputter xml( &result, file );
xml.setStyleSheet( "report.xsl" );
xml.write();
file.close();
char ccc;
scanf( &ccc );
return result.wasSuccessful() ? 0 : 1;
}
-------------------------------------Console格式 开结束-------------------------------------------------------------------------------