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.
#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.