the Compartmented Robust Posix C++ Unit Test system

FAIL

Immediately terminate a test with an customized error report. The macro FAIL is used as an output stream object.

Used in: A test function body, the constructor or destructor of a fixture, or a function called from them. See TEST(name, ...).

To fail a test with a customized error report, use the FAIL macro as any output stream. All information desired in the error report must fit in one single statement.

If the data to be printed has an output stream operator, it will be used for the formatting, otherwise a hex-dump of the object memory will be shown.

[Note]Note
Since FAIL uses a templated operator<<, it only works with types defined in global/namespace scope.

Example: The test program

     
     #include <crpcut.hpp>
     #include <cstring>
     
     const char *expected_string = 0;
     
     void assert_string(const char *p)
     {
       if (!p) FAIL << "assert_string called with 0";
       if (!expected_string) return;
       if (std::strcmp(p, expected_string) != 0)
         {
           FAIL << "assert_string called with " << p
                << "\nwhen the expected string was " << expected_string;
         }
     }
     
     TEST(fail_null)
     {
       assert_string(0);
     }
     
     TEST(pass_no_expect)
     {
       assert_string("rope");
     }
     
     TEST(fail_wrong_string)
     {
       expected_string = "wire";
       assert_string("rope");
     }
     
     int main(int argc, char *argv[])
     {
       return crpcut::run(argc, argv);
     }

        

provides two detailed failure reports:


     FAILED!: fail_null
     phase="running"  --------------------------------------------------------------
     samples/fail.cpp:35
     assert_string called with 0
     -------------------------------------------------------------------------------
     ===============================================================================
     FAILED!: fail_wrong_string
     phase="running"  --------------------------------------------------------------
     samples/fail.cpp:39
     assert_string called with rope
     when the expected string was wire
     -------------------------------------------------------------------------------
     ===============================================================================
     3 test cases selected
     
                    Sum   Critical   Non-critical
     PASSED   :       1          1              0
     FAILED   :       2          2              0