Testing assertions - Design by Contract

17 views
Skip to first unread message

Denis Koshenkov

unread,
Jul 28, 2025, 12:31:29 AMJul 28
to cpputest
Hi all,

I would like to test whether an assertion is called in the production code.

In this older post, Miro Samek wrote:

"Please note that when an assertion fails the test cannot
continue, because executing the code past assertion failure makes no
sense. But it is not sufficient to place FAIL() in the assertion
handler, because sometimes you exactly want to hit the assertion, so
hitting it means actually success. Conversely, when you expect an
assertion and you don't hit one, the test should fail."

Is there a way to get the behavior described by Miro?

Ryan Hartlage

unread,
Jul 28, 2025, 10:01:54 AMJul 28
to cppu...@googlegroups.com
Hi Denis,

We accomplish this by using C++ exceptions. Our code uses a shared assert function and the test version of this function throws an error when an assertion fails. When an assertion is expected in a test, we handle the thrown exception and treat this as success. Note that the code under test is normal C code -- it does not need to be C++ for this to work.

Ryan

--
You received this message because you are subscribed to the Google Groups "cpputest" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cpputest+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/cpputest/e10fb5a1-90b2-48dd-aff3-8280a5dc9e4dn%40googlegroups.com.

Denis Koshenkov

unread,
Aug 2, 2025, 4:11:21 AMAug 2
to cpputest
Hi Ryan,

Thank you for the fast response! In this scenario, can you know which assert fired (e.g. by throwing a different exception), or can you only know that an assert fired?

I implemented a test assert plugin to test my asserts. During testing, I found that it is really helpful to know which assert fired, in case the function has several. Otherwise, it is possible that a different assert than the one you expected fires, but the test will still pass.

Denis

Ryan Hartlage

unread,
Aug 2, 2025, 8:12:16 AMAug 2
to cpputest
Hey Denis,

One thing to note first is that you shouldn't have to modify CppUTest to do this. My implementation is separate from CppUTest so that we're able to use the upstream releases of CppUTest. My implementation is contained in separate files.

In my implementation, we don't check which assertion failed. However, this should be pretty easy to do because C++ exceptions throw objects that you can catch. One possible route is to wrap your assert function in a macro and convert the assertion condition to a string that you can compare to later. Something like this:

#define assert(condition) __assert(condition, #condition)

void __assert(bool condition, const char *conditionString) {
   if(!condition) {
      throw std::runtime_error(conditionString);
   }
}

Then in your "this should assert" function you can catch the thrown error and compare the error string to what you expect.

Another approach is to just add another argument to your assert function that you can use in the thrown error. The advantage of using a macro and stringifying the condition is that you can provide an alternative version of the macro on your embedded builds that does not store a string in flash for every assertion. In our applications, we found that this increased our flash usage by about 10%. Using a macro and omitting the string in embedded builds lets us use assertions with very little memory impact.

Hope this helps,
Ryan

James W Grenning

unread,
Aug 5, 2025, 12:28:00 PMAug 5
to cpputest

Hi Ryna and Denis

There is another way to implement checks for assert. There is a TEST_EXIT macro you could use to fake an assert. Probably use TEST_EXIT along with UT_PRINT.

Cheers, James

Reply all
Reply to author
Forward
0 new messages