I want to test a function:
#include <cassert>
int my_func(int myParam) {
int ret = 0
if (myParam >= 10) {
assert(false);
// Logger call like `qCritical << "myParam is too big. Returning 0";`
} else {
ret = myParam * 2;
}
return ret;
}
assert()
s are only enabled in the debug build.
I want to write tests that will pass whether the tests were built with the debug configuration or not (I assume this is the right way? Or is it sensible to write tests only for a specific build configuration?). So with a test input of, for example, 999:
assert()
).0
.I'm aware of EXPECT_DEBUG_DEATH
, which only expects a crash if built with the debug configuration. But I don't see a test macro for expecting a specific return value only if built without the debug configuration.
Like others mentioned above, how about just using NDEBUG
pre-processor directive? NDEBUG
disables standard C assertions.
You could do it like
#ifdef NDEBUG
TEST_F(FooTest, DISABLED_MyFuncAbortsOnInputBigger10)
#else
TEST_F(FooTest, MyFuncAbortsOnInputBigger10)
#endif
{
// ...
EXPECT_DEATH( /* .. */ );
}
#ifdef NDEBUG
TEST_F(FooTest, MyFuncLogsInvalidInput)
#else
TEST_F(FooTest, DISABLED_MyFuncLogsInvalidInput)
#endif
{
// ...
}
This way, your death test is disabled when you compile and run your test suite in release mode. In debug mode, the death test is active.
The DISABLED_
prefix in the test name allows you to disable a test but keep compiling the test code. You can read about it in the AdvancedGuide.