初用boost的test,照网上的例子,在VC的console里面添加如下代码:
#define BOOST_TEST_MODULE test_main |
然后屏蔽掉console原本的main函数,再把$(OutDir)\$(ProjectName).exe添加到工程的Post-Build Events,链接后能看到测试在跑:
3>Performing Post-Build Event... |
但相同的做法,在DLL功能是怎么启动测试的呢?因为DLL工程又不能添加执行exe的Post-Build Events,尽管boost.test有main函数,但怎么调用?这两天正好遇到这个问题,有两个方法可以解决:
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; } |