您的位置:首页 > 编程语言 > C语言/C++

cppUnit快速使用指南

2007-07-06 08:44 351 查看
【原文写于22 January 2007 ,

VC下cppUnit快速使用指南

编译
打开cppUnit/src/CppUnitLibraries.dsw,build->batch build,全部build
编译后得到所需库文件,在cppUnit/lib

安装
将cppUnit/include加入到VC include目录
将cppUnit/lib加入到VC library目录
将cppUnit/lib加入到VC executable目录,以使能够定位到testrunner(d).dll
如果是VC6的话,Tools/Customize…/Add-ins and macro files/Browse…,选择cppUnit/lib/TestRunnerDSPlugIn.dll,这样使用cppUnit的编译器输出可以直接到达源文件行

安装测试
打开cppunit/examples/examples.dsw
找到CppUnitTestApp,这是cppunit自测试的MFC GUI版本,set as Active Project,运行
(在实验室机器上编译有错,打开XmlUniformiser.cpp, line 38, message.str()修改为message.str().c_str(),编译通过。不知道是不是stlport的问题)
browse->选择All Tests->run,如果green,表示安装成功,运行正常

找到CppUnitTestMain,这是cppunit自测试的编译器输出版本,set as Active Project,build(不必run)
如果打印出OK (219),表示成功

使用

MFC(testrunner)
新建MFC对话框工程
修改工程设置:
Code generation,设为Multithreaded DLL
C++ langage,勾选RTTI
添加库cppunitd.lib(debug版)或cppunit.lib(release版)

在CXXXApp::InitInstance()中,注释掉原来的生成对话框代码,在该处加入以下代码

CppUnit::Test * suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
CppUnit::MfcUi::TestRunner runner;
runner.addTest(suite);
runner.run();

并在前边加入头文件
#include <cppunit/ui/mfc/TestRunner.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

然后编写测试用例
如果要使用cppunit的dll版本,在工程中添加CPPUNIT_DLL定义(Preprocessor definitions)

#include <cppunit/extensions/HelperMacros.h>

class CXXXTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(CXXXTest);
CPPUNIT_TEST(test_a);
CPPUNIT_TEST(test_b);
CPPUNIT_TEST(test_c);
CPPUNIT_TEST_SUITE_END();
public:
void test_a();
void test_b();
void test_c();
};
CPPUNIT_TEST_SUITE_REGISTRATION(CXXXTest);

void CXXXTest::test_a()
{

CPPUNIT_ASSERT(…);
CPPUNIT_ASSERT_EQUAL(…, …);

}

各种ASSERT参见cppUnit文档中Making assertions一章

编译器输出(build即测试)
新建console工程
修改工程设置与MFC GUI方式相同
在工程’post-build step’页中,’Post-build description’, 输入'’Unit testing…’;在’post-build command(s)’中, 添加一行: $(TargetPath)

编写main.cpp

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

int main()
{
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()) );
bool ret = runner.run();

return ret ? 0 : 1;
}

编写测试用例与MFC GUI方式相同
build,即同时完成建造和单元测试
测试失败会在建造信息中显示,双击该行信息会自动跳到引起测试失败的CPPUNIT_ASSERT语句
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: