[CMake] Tests with assert and Release build type
Magnus Therning
magnus at therning.org
Sat Dec 19 13:59:01 EST 2015
Dave Flogeras writes:
> 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.
I did a bit of testing and found that
1. CMAKE_C_FLAGS_* is, very irritatingly, *not* a list, so it's not easy
to remove '-DNDEBUG' from them in the CMakeLists.txt for the tests.
2. Compile options added by target_compile_options() are added *after*
CMAKE_C_FLAGS_* so I settled on using it to pass -UNDEBUG to gcc.
Far from nice, but it works for now (which feels quite typical for CMake).
/M
--
Magnus Therning OpenPGP: 0x927912051716CE39
email: magnus at therning.org jabber: magnus at therning.org
twitter: magthe http://therning.org/magnus
If you can explain how you do something, then you're very very bad at
it.
-- John Hopfield
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 800 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/cmake/attachments/20151219/ca35ebc1/attachment.sig>
More information about the CMake
mailing list