目录
准备工作:构建好一个测试用例(我这里构建了个DllForTestTestCase类)
单元测试第一种:就是我们常规看到的在代码里进行插入测试(这种网上示例很多,我就不赘述了)
单元测试第二种:用DllPlugInTester.exe来调用测试方法
准备工作:构建好一个测试用例(我这里构建了个DllForTestTestCase类)
首先是.h文件:
#pragma once
#ifndef _CPP_UNIT_DLLFORTESTTESTCASE_H_
#define _CPP_UNIT_DLLFORTESTTESTCASE_H_
#include <cppunit/extensions/HelperMacros.h>
class DllForTestTestCase:public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE( DllForTestTestCase );
CPPUNIT_TEST( testTSCFoo );
CPPUNIT_TEST_SUITE_END();
public:
DllForTestTestCase();
~DllForTestTestCase();
protected:
void testTSCFoo();
};
#endif // !_CPP_UNIT_DLLFORTESTTESTCASE_H_
接着是.cpp文件:
#include "DllForTestTestCase.h"
#include <string>
//#include "TSClass.h"
CPPUNIT_TEST_SUITE_REGISTRATION( DllForTestTestCase );
DllForTestTestCase::DllForTestTestCase()
{
}
DllForTestTestCase::~DllForTestTestCase()
{
}
void DllForTestTestCase::testTSCFoo()
{
std::cout << "call func testTSCFoo!" << std::endl;
/*TSClass tc;
tc.MyFoo();*/
}
至此,我们的测试用例已经写好了(DllForTestTestCase::testTSCFoo())
现在开始进行单元测试:
单元测试第一种:就是我们常规看到的在代码里进行插入测试(这种网上示例很多,我就不赘述了)
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TextOutputter.h>
#include <cppunit/TestRunner.h>
int main()
{
CppUnit::TestResult r;
CppUnit::TestResultCollector rc;
r.addListener(&rc); // 准备好结果收集器
CppUnit::TestRunner runner; // 定义执行实体
runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
runner.run(r); // 运行测试
CppUnit::TextOutputter o(&rc, std::cout);
o.write(); // 将结果输出
int rInt = rc.wasSuccessful() ? 0 : -1;
return 0;
}
单元测试第二种:用DllPlugInTester.exe来调用测试方法
优势:C++编程一般是“黑框框编程”(原谅我这么称呼它),当我们想对一个dll进行测试时,在工程的调试中简单设置,就可以直接应用DllPlugInTester.exe来测试,这样就大大的增加了程序工程师的开发效率
1、项目调试一栏的参数设置如下:
2、还得增加一句话:(原则上可以写在任一一个.cpp文件中,我们一般写在dllmain.cpp里边,以下为dllmain.cpp)
#include <cppunit/plugin/TestPlugIn.h>
CPPUNIT_PLUGIN_IMPLEMENT();
到此,设置结束,设置该工程为启动项,直接进入到 DllForTestTestCase::testTSCFoo()方法
附:我自己编译好的cppunit1.13.2版本,点此链接下载