silentbicycle/greatest — pull request #23 by pmj (2014-12-18 10:54:01+00:00)
Title: "long" failures and asserts

Greatest is very nice for testing simple functions, especially where checking return values is sufficient for pass/fail decisions. I've had a bit of a harder time making it work nicely with mock functions and checking that tested functions call out to functions in other libraries or modules correctly, or that a provided callback is used correctly. This is mainly down to the restriction that ASSERT/PASS/FAIL/etc. macros must be used within a test function itself. In some cases, you can work around it by setting some global state within a called function and then checking that state once control returns to the test function itself. Another option is to use stdlib or other assert() macros that just abort() on failure. Neither option is ideal, so I wondered if it would be practical to use C's setjmp/longjmp facilities for unwinding the stack on test failure. The patch implements a minimal prototype demonstrating exactly that, with a GREATEST_ASSERT_LONG macro that works like GREATEST_ASSERT (ASSERT) except you can also use it in functions called by your test, not just the test function itself.

Obviously, this isn't fully fleshed out, and comes with the usual longjmp issues, but if you're only going to use it for test **failure** then I think the risk is pretty small compared to the reward. What do you think?