问boost.test在DLL工程的单元测试如何启动
初用boost的test,照网上的例子,在VC的console里面添加如下代码:
#define BOOST_TEST_MODULE test_main
#include <boost/test/unit_test.hpp>
int add( int a, int b ){return (a+b);}
BOOST_AUTO_TEST_SUITE(minimal_test)
BOOST_AUTO_TEST_CASE( test_func_add )
{
BOOST_CHECK_EQUAL( 0, 0 );
BOOST_CHECK_EQUAL( add(1,-1), 1 );
getchar();
}
BOOST_AUTO_TEST_SUITE_END()
然后屏蔽掉console原本的main函数,再把$(OutDir)\$(ProjectName).exe添加到工程的Post-Build Events,链接后能看到测试在跑:
3>Performing Post-Build Event...
3>Running 2 test cases...
3>g:/test/boosttest/maintest/maintest.cpp(45): error in "test_func_add": check add(1,-1) == 1 failed [0 != 1]
3>*** 3 failures detected in test suite "test_main"
3>Project : error PRJ0019: A tool returned an error code from "Performing Post-Build Event..."
但相同的做法,在DLL功能是怎么启动测试的呢?因为DLL工程又不能添加执行exe的Post-Build Events,尽管boost.test有main函数,但怎么调用?
[解决办法]
有没有直接的方法对DLL做单元测试?还是要挂在一个比如console上测试呢?
[解决办法]
这两天正好遇到这个问题,有两个方法可以解决:
1。 自己写一个测试应用程序,在dll中导出测试方法,再添加到测试框架中
//dll中的导出函数
__declspec(dllexport) void myTest()
{
CMainTest *pTest = new CMainTest();
if (*pTest>runTest())
cout<<"测试成功"<<endl;
delete *pTest;
}
...
//测试程序
test_suite* init_unit_test_suite( int, char* [] ) {
framework::master_test_suite().p_name.value = "Unit test example 01";
// register the test case in test tree and specify number of expected failures so
// this example will pass at runtime. We expect 2 errors: one from failed check and
// one from memory acces violation
framework::master_test_suite().add( BOOST_TEST_CASE( &myTest ), 1 );
return 0;
}
// dll中
BOOST_AUTO_TEST_CASE( test1 )
{
int i = 0;
BOOST_CHECK_EQUAL( i, 2 );
}
BOOST_AUTO_TEST_CASE( test2 )
{
BOOST_CHECKPOINT("About to force division by zero!");
int i = 1, j = 0;
i = i / j;
}
extern "C" {
#ifdef BOOST_WINDOWS
__declspec(dllexport)
#endif
bool init_unit_test() {
framework::master_test_suite().p_name.value = "Test runner test";
return true;
}
}