boost::unit_test
|
test case |
#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, "add(..) result: " << add( 2,2 ) ); // #6 continues on error BOOST_CHECK_EQUAL( add( 2,2 ), 4 ); // #7 continues on error } 注解: 1. “BOOST_TEST_MODULE”,定义测试模块。之后再include “unit_test.hpp”。 2. “BOOST_AUTO_TEST_CASE”,自动注册测试用例。 3. 测试工具分为"WARN”,“CHECK"和"REQUIRE"三个等级。“CHECK"与"REQUIRE"差别为:前者失败,也仍然继续;后者则认为是必须的,为严重错误,直接退出当前测试。 |
|
test suite |
#define BOOST_TEST_MODULE MySuiteTest #include <boost/test/included/unit_test.hpp>
/* test_suite1 start */ BOOST_AUTO_TEST_SUITE( test_suite1 ) // test_case1 in test_suite1 BOOST_AUTO_TEST_CASE( test_case1 ) { BOOST_WARN( sizeof(int) < 4 ); } // test_case2 in test_suite1 BOOST_AUTO_TEST_CASE( test_case2 ) { BOOST_REQUIRE_EQUAL( 1, 2 ); // note: REQUIRE BOOST_FAIL( "Should never reach this line" ); } BOOST_AUTO_TEST_SUITE_END() /* test_suite1 end */
//---test_suite2 start BOOST_AUTO_TEST_SUITE( test_suite2 ) // test_case3 in test_suite2 BOOST_AUTO_TEST_CASE( test_case3 ) { BOOST_CHECK( true ); } BOOST_AUTO_TEST_SUITE_END() /* test_suite2 end */
// test_case_on_file_scope BOOST_AUTO_TEST_CASE( test_case_on_file_scope ) { BOOST_CHECK( true ); }
/* test_suite2 start */ BOOST_AUTO_TEST_SUITE( test_suite2 ) // test_case4 in test_suite2 BOOST_AUTO_TEST_CASE( test_case4 ) { BOOST_CHECK( false ); } BOOST_AUTO_TEST_SUITE_END() //---test_suite2 end 注解:test_suite之间独立,失败互补影响. |
|
test fixture |
todo |
|
自定义入口 |
//#define BOOST_TEST_MODULE MyTest //注释掉,否则会自动引入main,造成冲突 #include <boost/test/unit_test.hpp> #include <iostream> int add(int i, int j ) {return i+j;} bool init_unit_test(){ std::cout << "custom init " << std::endl; return true; // if false is returned , the incoming test process will be terminated. } 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, "add(..) result: " << add( 2,2 ) ); // #6 continues on error BOOST_CHECK_EQUAL( add( 2,2 ), 4 ); // #7 continues on error } int main(int argc, char **argv){ return boost::unit_test::unit_test_main( init_unit_test, argc, argv ); } |
|
链接与宏定义 |
g++ --std=c++11 -DBOOST_ALL_DYN_LINK -DBOOST_LOG_DYN_LINK -I/usr/include $^ -o $@ -lboost_unit_test_framework |

浙公网安备 33010602011771号