3

Or Can I use it in regular code?

If the answer is "no", then is there C++ library that will provide me with all the macros like CHECK_EQUAL, CHECK_CLOSE, etc.?

Łukasz Lew
  • 48,526
  • 41
  • 139
  • 208
  • I am looking for the same things. It is a pity that neither Boost.Test or Catch2 seem to provide a simple macro that can be used use as a replacement for standard assert with nice error message. It is useful to know that an assert failed but it is even more useful to know how it failed. https://www.boost.org/doc/libs/1_71_0/libs/test/doc/html/boost_test/testing_tools/boost_test_universal_macro.html . Google Test seems to provide something like this but I didn't try it yet https://github.com/google/googletest/blob/master/googletest/docs/advanced.md. – alfC Nov 13 '19 at 07:28

3 Answers3

4

It is only meaningful in unit tests, since its purpose is to alert the unit testing framework that a test failed, and then continue. If the unit testing framework isn't running, that won't work.

Outside unit tests, you'll usually want to use some flavor of assert instead.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • 1
    What does it exactly mean that "unit testing framework is running". Does it mean that it is lined or that we are in BOOST_AUTO_TEST_CASE? What will happen if I call BOOST_CHECK_EQUAL outside of BOOST_AUTO_TEST_CASE? (with or without test framework linked?) – Łukasz Lew Oct 18 '09 at 13:48
3

If the answer is "no", then is there C++ library that will provide me with all the macros like CHECK_EQUAL, CHECK_CLOSE, etc.?

The short answer is no. The longer answer: These macros are part of Boost.Test. So, if you are not using Boost.Test you will have to roll your own.

dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • I do use boost test. The first question is whether I cant use them outside unit test case. – Łukasz Lew Oct 18 '09 at 13:45
  • Import the definitions (i.e. copy-paste the specific portion of the headers that define these macros in a separate header and add it to your project, you wouldn't want the whole of Boost.Test to be shipped with your code.) – dirkgently Oct 18 '09 at 13:54
0

It's fairly easy to write this functionality based on boost/assert or cassert.
Note, however, that assertions may require some definitions (such as DEBUG)

Oren S
  • 1,820
  • 1
  • 19
  • 33