boost test study note学习笔记【转】

本文介绍Boost.Test单元测试框架的基本使用方法,通过多个示例展示了如何进行基本的测试用例编写,包括不同类型的断言及错误处理方式。同时提供了编译指令及常见配置说明。
source code:
#define BOOST_TEST_MODULE MyTest
#include <boost/test/unit_test.hpp>

int add( int i, int j ) { return i+j; }

BOOST_AUTO_TEST_CASE( my_test )
{
    // seven ways to detect and report the same error:
    BOOST_CHECK( add( 2,2 ) == 4 );        // #1 continues on error

    BOOST_REQUIRE( add( 2,2 ) == 4 );      // #2 throws on error

    if( add( 2,2 ) != 4 )
      BOOST_ERROR( "Ouch..." );            // #3 continues on error

    if( add( 2,2 ) != 4 )
      BOOST_FAIL( "Ouch..." );             // #4 throws on error

    if( add( 2,2 ) != 4 ) throw "Ouch..."; // #5 throws on error

    BOOST_CHECK_MESSAGE( add( 2,2 ) == 4, // #6 continues on error
                         "add(..) result: " << add( 2,2 ) );

    BOOST_CHECK_EQUAL( add( 2,2 ), 4 );    // #7 continues on error
}
//EOF

on linux system use this command to compile,
g++ boostUnitTest.cpp -Lpath_to_boost_libraries -lboost_test_exec_monitor-gcc34-mt
path_to_boost_libraries is where contains libboost_test_exec_monitor-gcc34-mt.a, in my computer path_to_boost_libraries is ~/boost_1_34_0/lib/,
and you have to compile boost::test first.

$ ./a.out
Running 1 test case...

*** No errors detected


there is also a example which libboost_test_exec_monitor-gcc34-mt.a is not neede.
#include <boost/test/included/test_exec_monitor.hpp>

int test_main(int   ,   char   *[])
{
      BOOST_CHECK_EQUAL(1   ,   2);
      return   1;
      throw   "Ooops..";
}
//EOF

use this command to compile:
g++ test.cpp

$ ./a.out
Running 1 test case...
boosttest.cpp(5): error in "test_main_caller( argc, argv )": check 1 == 2 failed [1 != 2]
/home/weixk/boost_1_34_0/include/boost-1_34/boost/test/impl/test_main.ipp(40): error in "test_main_caller( argc, argv )": check test
_main_result == 0 || test_main_result == boost::exit_success failed

*** 2 failures detected in test suite "Test Program"


there are some example in boost document , you can modify them.

用test_main的那种是以前版本boost test用的,功能很简单
The Minimal Testing Facility - simple facility designed to provide the functionality presented by the original version of Boost.Test. It causes the test program to run in a monitored environment. In addition it defines several simple test tools that behave similarly to ones defined in the Unit Test Framework test tools. Minimal testing facility does not require linking with external components, so could be a component of choice for simple and quick testing needs.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值