Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Handling asserts

26 views
Skip to first unread message

Wilmer Suarez

unread,
May 17, 2024, 5:52:37 PM5/17/24
to ThrowTheSwitch Forums
Is there any way, using unity and/or cmock, to handle when asserts occur within the code under test? E.g., passing in a null pointer to an assert check.

Mark Vander Voord

unread,
May 17, 2024, 9:25:55 PM5/17/24
to throwth...@googlegroups.com
I believe I saw a PR recently for handling this natively in unity. I don't believe it's been merged yet.

CMock can definitely mock assert as a function, but instead of #include <assert.h> directly, you'd have to create a wrapper header. It could be as simple as this:

// assertion_api.h

#ifdef TEST
void assert(int);
#else
#include <assert.h>
#endif

Then you would include assertion_api.h anywhere that you would normally include assert.h

The assert "function" could then be used like so:

assert_Expect(0); // expecting assertion to be false
assert_Expect(1); // expecting assertion to be true

It's not perfect, but it'd work.

Down the road, we plan to have ceedling automatically take care of mocking this for you and giving access to these expectations.

I hope this helps.

Mark

On Fri, May 17, 2024 at 5:52 PM Wilmer Suarez <wilmer.s...@gmail.com> wrote:
Is there any way, using unity and/or cmock, to handle when asserts occur within the code under test? E.g., passing in a null pointer to an assert check.

--
You received this message because you are subscribed to the Google Groups "ThrowTheSwitch Forums" group.
To unsubscribe from this group and stop receiving emails from it, send an email to throwtheswitc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/throwtheswitch/432d42f0-b0e9-40fc-96ca-8c7d32cf4112n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages