Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is assert and unit-testing incompatible?

I have some concerns related to the fact of testing some functions containing the assert macro from assert.h.

If the assert fails the test fails also. This leaves me with some test cases that will never work.

For example a function instead of indicating failure (return false or something similar) asserts.

Is there a solution for this (unit-testing functions containing assert)?

like image 780
INS Avatar asked Jul 12 '09 13:07

INS


1 Answers

You could be testing the fact that that assert aborts when you expect it to (on bad input).

The test framework Google Test as an ASSERT_DEATH macro that will test that the program aborts where you expect it to (like an assert).

You could also compile with NDEBUG defined ( -DNDEBUG with gcc) to disable assertions for your unit tests.

like image 179
crmoore Avatar answered Sep 23 '22 10:09

crmoore