发表于: 2012-03-24 10:18:17
初用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函数,但怎么调用?
#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函数,但怎么调用?
这两天正好遇到这个问题,有两个方法可以解决:
1。 自己写一个测试应用程序,在dll中导出测试方法,再添加到测试框架中
很显然这种方式不好,须手动添加测试方法,而且得自己写一个测试程序
2. 使用 console_test_runner.exe
console_test_runner.exe是boost自带的工具,使用这个工具须先编译
1). 如果有编译好的 bjam
2). 在命令模式下编译 console_test_runner,命令为:bjam libs\test\tools\console_test_runner
3). 将生成的 console_test_runner.exe和boost_unit_test_framework-vc80-mt-gd-1_51.dll 拷贝到你的工作目录中
4). 因为要处理应用程序与 dll 中的共享问题,在你的dll中须要添加宏 BOOST_TEST_DYN_LINK,并在 dll 工程中 引入 boost_unit_test_framework-vc80-mt-gd-1_51.lib
5). 通过命令: console_test_runner.exe --test xxx.dll 测试你的工程
这样就可以在 dll 中写自动加载的测试用列了
1。 自己写一个测试应用程序,在dll中导出测试方法,再添加到测试框架中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//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;
}
|
很显然这种方式不好,须手动添加测试方法,而且得自己写一个测试程序
2. 使用 console_test_runner.exe
console_test_runner.exe是boost自带的工具,使用这个工具须先编译
1). 如果有编译好的 bjam
2). 在命令模式下编译 console_test_runner,命令为:bjam libs\test\tools\console_test_runner
3). 将生成的 console_test_runner.exe和boost_unit_test_framework-vc80-mt-gd-1_51.dll 拷贝到你的工作目录中
4). 因为要处理应用程序与 dll 中的共享问题,在你的dll中须要添加宏 BOOST_TEST_DYN_LINK,并在 dll 工程中 引入 boost_unit_test_framework-vc80-mt-gd-1_51.lib
5). 通过命令: console_test_runner.exe --test xxx.dll 测试你的工程
这样就可以在 dll 中写自动加载的测试用列了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
// 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
;
}
}
|