[CMake] Tests with assert and Release build type

Dave Flogeras dflogeras2 at gmail.com
Sat Dec 19 12:48:49 EST 2015


On Fri, Dec 18, 2015 at 9:55 AM, Magnus Therning <magnus at therning.org> wrote:
>
> I'm sure I'm not the first one to run into the conflict between a desire
> to test the code shipped to customers and the desire to at the same time
> define NDEBUG to make sure tests don't have their `assert` turned into
> NOOPs.
>
> Is there some nice way of handling this?

I've also wondered this.  My solution of choice has always been to
keep my tests in a subdirectory with a test header like you described:

....
#fidef NDEBUG
 #undef NDEBUG
 #include <cassert> // or assert.h for C
 #define NDEBUG
#else
 #include <cassert>
#endif
....

And include this in each of the unit tests.  However, I have found
sometimes this is not foolproof.  In particular I recall on one
version of OSX one of the system headers would undo this and make
assert() disappear again, requiring me to play the #include shell
game.  I usually place my "test_assert.hpp" include after all others
in the unit test, but this is brittle.

I have always just left it as a "good enough" solution, but I too
would love to hear if someone has a more robust solution.


More information about the CMake mailing list