Is there a function in boost :: test that can return an error value?

Boost Test Library is a very useful unit test framework. However, one thing that I don't like is that during the unit test, when errors occur, it will inform the user, but not about the program itself. Let me clarify using BOOST_CHECK as an example:

i=3;
j=4;
BOOST_CHECK(i==j);

The above test case will fail. So checking the details to find out why this test fails will be very interesting. In this case, it will be necessary to print some variables or perform more complex operations, such as writing a file to disk in the program, if it knows that the unit test failed. However, BOOST_CHECK will not return a value to indicate whether the test is successful or not. An ideal function should work as follows:

    i=3;
    j=4;
    if(Enhanced_BOOST_CHECK(i==j) == failed)
    {

        // print some internal varaibles.
        // or write some data to a file in the disk
     }

So my question is: Does the BOOST Test Library support this feature? Thank you

+4
source share
2 answers

Boost provides a macro BOOST_WARN_MESSAGE(and BOOST_CHECK_MESSAGEand BOOST_REQUIRE_MESSAGE). In your case, this can be used as follows:

i=3;
j=4;

bool isEqual = i==j;

BOOST_CHECK(isEqual);
BOOST_WARN_MESSAGE(isEqual, "Failure since i = " << i << " and j = " << j);

.

+3

Boost Test , BOOST_CHECK.

:

UTF

BOOST_<level>
BOOST_<level>_BITWISE_EQUAL
BOOST_<level>_CLOSE
BOOST_<level>_CLOSE_FRACTION
BOOST_<level>_EQUAL
BOOST_<level>_EQUAL_COLLECTION
BOOST_<level>_EXCEPTION
BOOST_<level>_GE
BOOST_<level>_GT
BOOST_<level>_LE
BOOST_<level>_LT
BOOST_<level>_MESSAGE
BOOST_<level>_NE
BOOST_<level>_NO_THROW
BOOST_<level>_PREDICATE
BOOST_<level>_SMALL
BOOST_<level>_THROW
BOOST_ERROR
BOOST_FAIL
BOOST_IS_DEFINED

BOOST_CHECK_EQUAL(i, j) .

, , :

if (!(i==j)) {
  // Complex condition failed - report to boost test and add custom message
  std::string message = ...;
  BOOST_CHECK_MESSAGE(false, message);
}
+1

Source: https://habr.com/ru/post/1548475/


All Articles