Home > The Unit Test Framework > Testing tools > Reference
PrevNext

The UTF testing tools reference

BOOST_WARN(predicate)
BOOST_CHECK(predicate)
BOOST_REQUIRE(predicate)

These tools are used to validate the predicate value. The only parameter for these tools is a boolean predicate value that gets validated. It could be any expression that could be evaluated and converted to boolean value. The expression gets evaluated only once, so it's safe to pass complex expression for validation.
這些工具用於驗證斷言的值。唯一的參數是被驗證的布爾斷言值。是可以被計算並轉換為布爾值的任意表達式。 表達式只被計算一次,所以傳入複雜的驗證表達式也是安全的。

Example 38. BOOST_<level> usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    int i=2;
    BOOST_WARN( sizeof(int) == sizeof(short) );
    BOOST_CHECK( i == 1 );
    BOOST_REQUIRE( i > 5 );
    BOOST_CHECK( i == 6 ); // will never reach this check
}

//____________________________________________________________________________//
Source code | Show output
> example --log_level=warning
Running 1 test case...
test.cpp(9): warning in "test": condition sizeof(int) == sizeof(short) is not satisfied
test.cpp(10): error in "test": check i == 1 failed
test.cpp(11): fatal error in "test": critical check i > 5 failed

*** 2 failures detected in test suite "example"

See also: BOOST_<level>_MESSAGE
BOOST_WARN_BITWISE_EQUAL(left, right)
BOOST_CHECK_BITWISE_EQUAL(left, right)
BOOST_REQUIRE_BITWISE_EQUAL(left, right)

These tools are used to perform bitwise comparison of two values. The check shows all positions where left and right value's bits mismatch.
這些工具用於兩個值的按位比較。檢查顯示左右兩個值不匹配的所有位。

The first parameter is the left compared value. The second parameter is the right compared value. Parameters are not required to be of the same type, but warning is issued if their type's size does not coincide.
第一個參數是被比較的左邊的值。第二個參數是被比較的右邊的值。 參數不要求是相同的類型,但如果它們的類型大小不同,將有警告出現。

Example 39. BOOST_<level>_BITWISE_EQUAL usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    BOOST_CHECK_BITWISE_EQUAL( (char)0x26, 0x04 );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...                                                          
test.cpp(8): error in "test": check (char)0x26 =.= 0x04 failed.                 
Mismatch in a position 1                                                        
Mismatch in a position 5                                                        
Operands bit sizes mismatch: 8 != 32                                            
                                                                                
*** 1 failure detected in test suite "example" 

See also: BOOST_<level>_EQUAL
BOOST_WARN_CLOSE(left, right, tolerance)
BOOST_CHECK_CLOSE(left, right, tolerance)
BOOST_REQUIRE_CLOSE(left, right, tolerance)

These tools are used to check on closeness using strong relationship defined by the predicate check_is_close( left, right, tolerance ). To check for the weak relationship use BOOST_<level>_PREDICATE family of tools with explicit check_is_close invocation.
這些工具使用謂詞 check_is_close(left, right, tolerance) 定義的強關係檢查接近。 要檢查弱關係,使用 BOOST_<level>_PREDICATE 族的工具和顯式的 check_is_close 調用。

The first parameter is the left compared value. The second parameter is the right compared value. Last third parameter defines the tolerance for the comparison in percentage units.
第一個參數是比較的left。第二個參數是比較的right。 最後的第三個參數以百分比單元定義了比較的限度。

[Note] Note

It is required for left and right parameters to be of the same floating point type. You will need to explicitly resolve any type mismatch to select which type to use for comparison.
left 和 right 參數需要是相同的浮點數類型。你需要顯式選擇比較使用的類型,解決類型不匹配。

[Note] Note

Note that to use these tools you need to include additional header floating_point_comparison.hpp.
要使用這些工具,需要包含額外的頭文件 floating_point_comparison.hpp。

Example 40. BOOST_<level>_CLOSE usage with very small values

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    double v1 = 1.23456e-10;
    double v2 = 1.23457e-10;

    BOOST_CHECK_CLOSE( v1, v2, 0.0001 );
    // Absolute value of difference between these two values is 1e-15. They seems 
    // to be very close. But we want to checks that these values differ no more then 0.0001%
    // of their value. And this test will fail at tolerance supplied.
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(12): error in "test": difference between v1{1.23456e-010} and v2{1.23457e-010} exceeds 0.0001%

*** 1 failure detected in test suite "example"

Example 41. BOOST_<level>_CLOSE usage with very big values

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    double v1 = 1.23456e28;
    double v2 = 1.23457e28;

    BOOST_REQUIRE_CLOSE( v1, v2, 0.001 );
    // Absolute value of difference between these two values is 1e+23.
    // But we are interested only that it does not exeed 0.001% of a values compared
    // And this test will pass.
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...

*** No errors detected

See also: BOOST_<level>_CLOSE_FRACTION, BOOST_<level>_SMALL, BOOST_<level>_EQUAL, Floating point comparison algorithms
BOOST_WARN_CLOSE_FRACTION(left, right, tolerance)
BOOST_CHECK_CLOSE_FRACTION(left, right, tolerance)
BOOST_REQUIRE_CLOSE_FRACTION(left, right, tolerance)

These tools are used to check on closeness using strong relationship defined by the predicate check_is_close( left, right, tolerance ). To check for the weak relationship use BOOST_<level>_PREDICATE family of tools with explicit check_is_close invocation.
這些工具使用謂詞 check_is_close(left, right, tolerance) 定義的強關係檢查接近。 要檢查弱關係,使用 BOOST_<level>_PREDICATE 族的工具,和顯式的 check_is_close 調用。

The first parameter is the left compared value. The second parameter is the right compared value. Last third parameter defines the tolerance for the comparison in percentage units.
第一個參數是被比較數 left。第二個參數是被比較數 right。 最後第三個參數以百分比單元定義了比較的限度。

[Note] Note

It is required for left and right parameters to be of the same floating point type. You will need to explicitly resolve any type mismatch to select which type to use for comparison.
left 和 right 參數需要是相同的浮點數類型。你需要顯式選擇比較使用的類型,解決類型不匹配。

[Note] Note

Note that to use these tools you need to include additional header floating_point_comparison.hpp.
要使用這些工具,需要包含額外的頭文件 floating_point_comparison.hpp。

Example 42. BOOST_<level>_CLOSE_FRACTION usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    double v1 = 1.111e-10;
    double v2 = 1.112e-10;

    BOOST_CHECK_CLOSE_FRACTION( v1, v2, 0.0008999 );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(12): error in "test": difference between v1{1.111e-010} and v2{1.112e-010} exceeds 0.0008999

*** 1 failure detected in test suite "example"

See also: BOOST_<level>_CLOSE, BOOST_<level>_SMALL, BOOST_<level>_EQUAL, Floating point comparison algorithms
BOOST_WARN_EQUAL(left, right)
BOOST_CHECK_EQUAL(left, right)
BOOST_REQUIRE_EQUAL(left, right)

Check performed by these tools is the same as the one performed by BOOST_<level>( left == right ). The difference is that the mismatched values are reported as well.
這些工具執行的檢查等價於 BOOST_<level>( left == right )。 區別是不匹配的值也會被報告。

[Note] Note

It is bad idea to use these tools to compare floating point values. Use BOOST_<level>_CLOSE or BOOST_<level>_CLOSE_FRACTION tools instead.
使用這些工具來比較浮點數是個壞主意。 使用 BOOST_<level>_CLOSEBOOST_<level>_CLOSE_FRACTION 工具。

Example 43. BOOST_<level>_EQUAL usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    int i = 2;
    int j = 1;
    BOOST_REQUIRE_EQUAL( i, j );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(10): fatal error in "test": critical check i == j failed [2 != 1]

*** 1 failure detected in test suite "example"

See also: BOOST_<level>, BOOST_<level>_CLOSE, BOOST_<level>_NE
BOOST_WARN_EQUAL_COLLECTION(left_begin, left_end, right_begin, right_end)
BOOST_CHECK_EQUAL_COLLECTION(left_begin, left_end, right_begin, right_end)
BOOST_REQUIRE_EQUAL_COLLECTION(left_begin, left_end, right_begin, right_end)

These tools are used to perform an element by element comparison of two collections. They print all mismatched positions, collection elements at these positions and check that the collections have the same size. The first two parameters designate begin and end of the first collection. The two parameters designate begin and end of the second collection.
這些工具用於一個元素接一個元素地比較兩個集合。它們打印所有不匹配的位置,這些位置上的集合元素, 並檢查集合是否有相同的大小。前兩個參數指定了第一個集合的開始和結束。後兩個參數指定了第二個集合的開始和結束。

Example 44. BOOST_<level>_EQUAL_COLLECTION usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    int col1 [] = { 1, 2, 3, 4, 5, 6, 7 };
    int col2 [] = { 1, 2, 4, 4, 5, 7, 7 };

    BOOST_CHECK_EQUAL_COLLECTIONS( col1, col1+7, col2, col2+7 );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(11): error in "test": check { col1, col1+7 } == { col2, col2+7 } failed. 
Mismatch in a position 2: 3 != 4
Mismatch in a position 5: 6 != 7

*** 1 failure detected in test suite "example"

See also: BOOST_<level>_EQUAL
BOOST_WARN_EXCEPTION(expression, exception, predicate)
BOOST_CHECK_EXCEPTION(expression, exception, predicate)
BOOST_REQUIRE_EXCEPTION(expression, exception, predicate)

These tools are used to perform an exception detection and validation check. Tools execute the supplied expression and validate that it throws an exception of supplied class (or the one derived from it) that complies with the supplied predicate. If the expression throws any other unrelated exception, doesn't throw at all or predicate evaluates to false, check fails. In comparison with BOOST_<level>_THROW tools these allow performing more fine-grained checks. For example: make sure that an expected exception has specific error message.
這些工具用於執行異常檢測和驗證檢查。工具執行提供的表達式,並驗證其拋出給定類型 (或其子類型) 的異常,並由給定的謂詞判斷。 如果表達式拋出其它無關的異常,不拋出異常,或者謂詞計算為假,則檢查失敗。 和 BOOST_<level>_THROW 工具相比執行更細緻的檢查。 例如:確保期望的異常有特定的錯誤信息。

Example 45. BOOST_<level>_EXCEPTION usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

struct my_exception {
    explicit my_exception( int ec = 0 ) : m_error_code( ec ) {}

    int m_error_code;
};

bool is_critical( my_exception const& ex ) { return ex.m_error_code < 0; }

BOOST_AUTO_TEST_CASE( test )
{
    BOOST_CHECK_EXCEPTION( throw my_exception( 1 ), my_exception, is_critical );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(16): error in "test": incorrect exception my_exception is caught

*** 1 failure detected in test suite "example"

See also: BOOST_<level>_THROW
BOOST_WARN_GE(left, right)
BOOST_CHECK_GE(left, right)
BOOST_REQUIRE_GE(left, right)

Check performed by these tools is the same as the one performed by BOOST_<level>( left >= right ). The difference is that the argument values are reported as well.
這些工具提供的檢查等價於 BOOST_<level>( left >= right )。 差別在於不同的參數值會被報告。

Example 46. BOOST_<level>_GE usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    int i = 1;
    int j = 4;
    BOOST_CHECK_GE( i, j );
}

//____________________________________________________________________________//
Source code | Show output
>example
Running 1 test case...
test.cpp(10): error in "test": check i >= j failed [1 < 4]

*** 1 failure detected in test suite "example"

See also: BOOST_<level>_LE, BOOST_<level>_LT, BOOST_<level>_GT
BOOST_WARN_GT(left, right)
BOOST_CHECK_GT(left, right)
BOOST_REQUIRE_GT(left, right)

Check performed by these tools is the same as the one performed by BOOST_<level>( left >= right ). The difference is that the argument values are reported as well.
這些工具執行的檢查等價於 BOOST_<level>( left >= right )。 差別在於不同的參數值會被報告。

Example 47. BOOST_<level>_GT usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    int i = 2;
    int j = 14;
    BOOST_CHECK_GT( i, j );
}

//____________________________________________________________________________//
Source code | Show output
>example
Running 1 test case...
test.cpp(10): error in "test": check i > j failed [2 < 14]

*** 1 failure detected in test suite "example"

See also: BOOST_<level>_LE, BOOST_<level>_LT, BOOST_<level>_GE
BOOST_WARN_LE(left, right)
BOOST_CHECK_LE(left, right)
BOOST_REQUIRE_LE(left, right)

Check performed by these tools is the same as the one performed by BOOST_<level>( left <= right ). The difference is that the argument values are reported as well.
這些工具執行的檢查等價於 BOOST_<level>( left <= right )。 差別在於參數值會被報告。

Example 48. BOOST_<level>_LE usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    int i = 9;
    int j = 6;
    BOOST_CHECK_LE( i, j );
}

//____________________________________________________________________________//
Source code | Show output
>example
Running 1 test case...
test.cpp(10): error in "test": check i <= j failed [9 > 6]

*** 1 failure detected in test suite "example"

See also: BOOST_<level>_LT, BOOST_<level>_GE, BOOST_<level>_GT
BOOST_WARN_LT(left, right)
BOOST_CHECK_LT(left, right)
BOOST_REQUIRE_LT(left, right)

Check performed by these tools is the same as the one performed by BOOST_<level>( left < right ). The difference is that the argument values are reported as well.
這些工具執行的檢查等價於 BOOST_<level>( left < right )。 差別在於參數值將被報告。

Example 49. BOOST_<level>_LT usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    int i = 7;
    int j = 7;
    BOOST_CHECK_LT( i, j );
}

//____________________________________________________________________________//
Source code | Show output
>example
Running 1 test case...
test.cpp(10): error in "test": check i < j failed [7 >= 7]

*** 1 failure detected in test suite "example"

See also: BOOST_<level>_LE, BOOST_<level>_GE, BOOST_<level>_GT
BOOST_WARN_MESSAGE(predicate, message)
BOOST_CHECK_MESSAGE(predicate, message)
BOOST_REQUIRE_MESSAGE(predicate, message)

These tools perform exactly the same check as BOOST_<level> tools. The only difference is that instead of generating an error/confirm message these use the supplied one.
這些工具執行的檢查完成等價於 BOOST_<level> 工具。 唯一的差別在於使用提供的錯誤 / 確認信息而不是生成。

The first parameter is the boolean expression. The second parameter is the message reported in case of check failure. The message argument can be constructed of components of any type supporting the std::ostream& operator<<(std::ostream&).
第一個參數是布爾表達式。第二個參數是如果檢查失敗時報告的信息。 信息參數可以由支持 std::ostream& operator<<(std::ostream&) 的任意類型的組件構造而成。

Example 50. BOOST_<level>_MESSAGE usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

#include <cmath>

BOOST_AUTO_TEST_CASE( test )
{
    double res = std::sin( 45. );

    BOOST_WARN_MESSAGE( res > 1, "sin(45){" << res << "} is <= 1. Hmm.. Strange. " );
}

//____________________________________________________________________________//
Source code | Show output
> example --log_level=warning
Running 1 test case...
test.cpp(12): warning in "test": sin(45){0.850904} is <= 1. Hmm.. Strange. 

*** No errors detected

See also: BOOST_<level>
BOOST_WARN_NE(left, right)
BOOST_CHECK_NE(left, right)
BOOST_REQUIRE_NE(left, right)

Check performed by these tools is the same as the one performed by BOOST_<level>( left != right ). The difference is that the matched values are reported as well.
這些工具執行的檢查等價於 BOOST_<level>( left != right )。 差別在於匹配的值也會被報告。

Example 51. BOOST_<level>_NE usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    int i = 3;
    int j = 3;
    BOOST_CHECK_NE( i, j );
}

//____________________________________________________________________________//
Source code | Show output
>example
Running 1 test case...
test.cpp(10): error in "test": check i != j failed [3 == 3]

*** 1 failure detected in test suite "example"

See also: BOOST_<level>_EQUAL
BOOST_WARN_NO_THROW(expression)
BOOST_CHECK_NO_THROW(expression)
BOOST_REQUIRE_NO_THROW(expression)

These tools are used to perform a "no throw" check. Tools execute the supplied expression and validate that it does not throw any exceptions. Error would be reported by the framework even if the statement appear directly in test case body and throw any exception. But these tools allow proceeding further with test case in case of failure.
這些工具用於執行 "不拋出" 檢查。工具執行提供的表達式,並驗證其不拋出任何異常。 即使語句出現在測試用例主體中拋出任何異常,錯誤都會被框架報告。 但這些工具在檢查失敗時會允許後面的測試用例繼續執行。

If check is successful, tools may produce a confirmation message, in other case they produce an error message in a form "error in <test case name>exception was thrown by <expression>.
如果檢查成功,工具會產生確認信息,否則以 "error in <test case name>exception was thrown by <expression>" 格式產生錯誤信息。

The only parameter is an expression to execute. You can use do-while(0) block if you want to execute more than one statement.
唯一的參數是要執行的表達式。如果想要執行多於一條語句,你可以使用 do-while(0) 塊。

Example 52. BOOST_<level>_NO_THROW usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

class my_exception{};

BOOST_AUTO_TEST_CASE( test )
{
    BOOST_CHECK_NO_THROW( throw my_exception() );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(10): error in "test": exception thrown by throw my_exception()

*** 1 failure detected in test suite "example"

See also: BOOST_<level>_THROW
BOOST_WARN_PREDICATE(predicate, arguments_list)
BOOST_CHECK_PREDICATE(predicate, arguments_list)
BOOST_REQUIRE_PREDICATE(predicate, arguments_list)

These are generic tools used to validate an arbitrary supplied predicate functor (there is a compile time limit on predicate arity defined by the configurable macro BOOST_TEST_MAX_PREDICATE_ARITY). To validate zero arity predicate use BOOST_<level> tools. In other cases prefer theses tools. The advantage of these tools is that they show arguments values in case of predicate failure.
這些是通用工具,用於驗證任意提供的斷言函數對像 (對於斷言的元數有一個編譯時限制,通過宏 BOOST_TEST_MAX_PREDICATE_ARITY 設置)。 要驗證 0 元數斷言,使用 BOOST_<level> 工具。 對於其它情況,這個工具更好一些。這些工具的優點在於斷言失敗時它們能夠顯示參數值。

The first parameter is the predicate itself. The second parameter is the list of predicate arguments each wrapped in round brackets (BOOST_PP sequence format).
第一個參數是斷言本身。第二個參數是斷言參數列表,使用圓括號外覆 (BOOST_PP 序列格式)。

Example 53. BOOST_<level>_PREDICATE usage

Note difference in error log from 注意測試日誌的不同

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>
using namespace boost::unit_test;

//____________________________________________________________________________//

bool moo( int arg1, int arg2, int mod ) { return ((arg1+arg2) % mod) == 0; }

BOOST_AUTO_TEST_CASE( test )
{
    int i = 17;
    int j = 15;
    unit_test_log.set_threshold_level( log_warnings );
    BOOST_WARN( moo( 12,i,j ) );
    BOOST_WARN_PREDICATE( moo, (12)(i)(j) );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(14): warning in "test": condition moo( 12,i,j ) is not satisfied
test.cpp(15): warning in "test": condition moo( 12, i, j ) is not satisfied for ( 12, 17, 15 )

*** No errors detected

See also: BOOST_<level>
BOOST_WARN_SMALL(value, tolerance)
BOOST_CHECK_SMALL(value, tolerance)
BOOST_REQUIRE_SMALL(value, tolerance)

These tools are used to check that supplied value is small enough. The "smallness" is defined by absolute value of the tolerance supplied as a second argument. Use these tools with caution. To compare to values on closeness it's preferable to use BOOST_<level>_CLOSE tools instead.
這些工具用於檢查給定的值足夠小。"小" 由第二個參數提供的限度的絕對值定義。 小心使用這些工具。要比較值是否接近,最後使用 BOOST_<level>_CLOSE 工具。

The first parameter is the value to check. The second parameter is the tolerance.
第一個參數是要檢查的值。第二個參數是限度。

[Note] Note

Note that to use these tools you need to include additional header floating_point_comparison.hpp.
要使用這些工具,需要包含額外的頭文件 floating_point_comparison.hpp。

Example 54. BOOST_<level>_SMALL usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    double v = -1.23456e-3;

    BOOST_CHECK_SMALL( v, 0.000001 );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(11): error in "test": absolute value of v{-0.00123456} exceeds 1e-006

*** 1 failure detected in test suite "example"

See also: BOOST_<level>_CLOSE, BOOST_<level>_CLOSE_FRACTION, Floating point comparison algorithms
BOOST_WARN_THROW(expression, exception)
BOOST_CHECK_THROW(expression, exception)
BOOST_REQUIRE_THROW(expression, exception)

These tools are used to perform an exception detection check. Tools execute the supplied expression and validate that it throws an exception of supplied class (or the one derived from it) or it's child. If the statement throws any other unrelated exception or doesn't throw at all, check fails.
這些工具用於執行異常檢測。工具執行給定的表達式,並驗證其拋出給定類型 (或其子類型) 的異常。 如果語句拋出任意無關的異常或不拋出異常,檢查失敗。

If check is successful, the tool produces a confirmation message, in other case it produces an error message in a form "error in test case name: exception exception expected.
如果檢查成功,工具生成確認信息,否則它生成 "error in test case name: exception exception expected." 格式的錯誤信息。

The first parameter is the expression to execute. Use do-while(0) block if you want to execute more than one statement. The second parameter is an expected exception.
第一個參數是要執行的表達式。如果想要執行多於一條語句,使用 do-while(0) 塊。第二個參數是期望的異常。

Example 55. BOOST_<level>_THROW usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

class my_exception{};

BOOST_AUTO_TEST_CASE( test )
{
    int i =  0;
    BOOST_CHECK_THROW( i++, my_exception );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(11): error in "test": exception my_exception is expected

*** 1 failure detected in test suite "example"

See also: BOOST_<level>NO_THROW
BOOST_ERROR(message)

BOOST_ERROR tool behave the same way as BOOST_CHECK_MESSAGE( false, message ). This tool is used for an unconditional error counter increasing and message logging.
BOOST_ERROR 工具行為和 BOOST_CHECK_MESSAGE( false, message ) 相同。 這個工具用於非條件性錯誤計數,和信息日誌。

The only tool's parameter is an error message to log.
工具唯一的參數是要日誌記錄的錯誤信息。

Example 56. BOOST_ERROR usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    BOOST_ERROR( "Nothing to test" );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(8): error in "test": Nothing to test

*** 1 failure detected in test suite "example"

See also: BOOST_<level>
BOOST_FAIL(message)

BOOST_FAIL behave the same way as BOOST_REQUIRE_MESSAGE( false, message ). This tool is used for an unconditional error counter increasing, message logging and the current test case aborting.
BOOST_FAIL 行為和 BOOST_REQUIRE_MESSAGE( false, message ) 相同。 這個工具用於非條件性錯誤計數,信息日誌記錄,中斷當前測試用例。

The only tool's parameter is an error message to log.
工具唯一的參數是要進行日誌記錄的錯誤信息。

Example 57. BOOST_FAIL usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    BOOST_FAIL( "Test is not ready yet" );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(8): fatal error in "test": Test is not ready yet

*** 1 failure detected in test suite "example"

See also: BOOST_<level>
BOOST_IS_DEFINED(symbol)

Unlike the rest of the tools in the toolbox this tool does not perform the logging itself. Its only purpose is to check at runtime whether or not the supplied preprocessor symbol is defined. Use it in combination with BOOST_<level> to perform and log validation. Macros of any arity could be checked. To check the macro definition with non-zero arity specify dummy arguments for it. See below for example.
不同於工具箱中的其它工具,這個工具本身不進行日誌記錄。 它唯一的目的是在運行時檢查給定的預定義符號是否被定義。 與 BOOST_<level> 組合使用運行並記錄驗證。 任意元數的宏都可以檢查。要檢查 0 元數的宏定義,指定啞元參數。參見下面的示例。

The only tool's parameter is a preprocessor symbol that gets validated.

Example 58. BOOST_IS_DEFINED usage

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    BOOST_CHECK( BOOST_IS_DEFINED(SYMBOL1) );
    BOOST_CHECK( BOOST_IS_DEFINED(SYMBOL2(arg)) );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(8): error in "test": check ::boost::test_tools::tt_detail::is_defined_impl( "SYMBOL1", "= SYMBOL1" ) failed
test.cpp(9): error in "test": check ::boost::test_tools::tt_detail::is_defined_impl( "SYMBOL2(arg)", "= SYMBOL2(arg)" ) failed

*** 2 failures detected in test suite "example"

See also: BOOST_<level>

PrevUpHomeNext